Skip to content

Commit 86ecb9c

Browse files
ran-isenbergRan Isenberg
andauthored
feature: fix openapi schema (ran-isenberg#800)
--------- Co-authored-by: Ran Isenberg <ran.isenberg@ranthebuilder.cloud>
1 parent f5cb4f5 commit 86ecb9c

File tree

9 files changed

+130
-111
lines changed

9 files changed

+130
-111
lines changed

docs/swagger/openapi.json

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
},
7777
"item_count": {
7878
"type": "integer",
79-
"exclusiveMinimum": 0.0,
8079
"title": "Item Count",
8180
"description": "Amount of items in order"
8281
},
@@ -107,7 +106,6 @@
107106
},
108107
"order_item_count": {
109108
"type": "integer",
110-
"exclusiveMinimum": 0.0,
111109
"title": "Order Item Count",
112110
"description": "Amount of items in order"
113111
}
@@ -171,23 +169,16 @@
171169
"description": "Error type"
172170
},
173171
"msg": {
174-
"anyOf": [
175-
{
176-
"type": "string"
177-
},
178-
{
179-
"type": "null"
180-
}
181-
],
172+
"type": "string",
182173
"title": "Msg",
183-
"description": "Error message"
174+
"description": "Error message",
175+
"default": ""
184176
}
185177
},
186178
"type": "object",
187179
"required": [
188180
"loc",
189-
"type",
190-
"msg"
181+
"type"
191182
],
192183
"title": "PydanticError"
193184
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"dependencies": {
3-
"aws-cdk": "2.125.0"
3+
"aws-cdk": "2.126.0"
44
}
55
}

poetry.lock

Lines changed: 84 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/models/input.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
from typing import Annotated
22

3-
from pydantic import BaseModel, Field
3+
from pydantic import BaseModel, Field, field_validator
44

55

66
class CreateOrderRequest(BaseModel):
77
customer_name: Annotated[str, Field(min_length=1, max_length=20, description='Customer name')]
8-
order_item_count: Annotated[int, Field(gt=0, description='Amount of items in order')]
8+
order_item_count: Annotated[int, Field(strict=True, description='Amount of items in order')]
9+
10+
@field_validator('order_item_count')
11+
@classmethod
12+
def check_order_item_count(cls, v):
13+
# we don't use Field(gt=0) because pydantic exports it incorrectly to openAPI doc
14+
# see https://github.com/tiangolo/fastapi/issues/240
15+
if v <= 0:
16+
raise ValueError('order_item_count must be larger than 0')
17+
return v

service/models/order.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Annotated
22
from uuid import UUID
33

4-
from pydantic import BaseModel, Field
4+
from pydantic import BaseModel, Field, field_validator
55
from pydantic.functional_validators import AfterValidator
66

77

@@ -36,5 +36,14 @@ def validate_product_id(product_id: str) -> str:
3636

3737
class Order(BaseModel):
3838
name: Annotated[str, Field(min_length=1, max_length=20, description='Customer name')]
39-
item_count: Annotated[int, Field(gt=0, description='Amount of items in order')]
39+
item_count: Annotated[int, Field(strict=True, description='Amount of items in order')]
4040
id: OrderId
41+
42+
@field_validator('item_count')
43+
@classmethod
44+
def check_item_count(cls, v):
45+
# we don't use Field(gt=0) because pydantic exports it incorrectly to openAPI doc
46+
# see https://github.com/tiangolo/fastapi/issues/240
47+
if v <= 0:
48+
raise ValueError('item_count must be larger than 0')
49+
return v

service/models/output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Annotated, List, Optional, Union
1+
from typing import Annotated, List, Union
22

33
from pydantic import BaseModel, Field
44

@@ -19,7 +19,7 @@ class InternalServerErrorOutput(BaseModel):
1919
class PydanticError(BaseModel):
2020
loc: Annotated[List[Union[str, int]], Field(description='Error location')]
2121
type: Annotated[str, Field(description='Error type')]
22-
msg: Annotated[Optional[str], Field(description='Error message')]
22+
msg: Annotated[str, Field(description='Error message')] = ''
2323

2424

2525
class InvalidRestApiRequest(BaseModel):

tests/unit/test_create_order_input.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,17 @@ def test_missing_mandatory_fields():
3636
def test_invalid_order_number():
3737
# Given: An invalid negative order_item_count
3838
customer_name = 'a'
39-
order_item_count = -1
39+
order_item_count = 0
40+
41+
# When & Then: CreateOrderRequest is initialized, expect a ValidationError
42+
with pytest.raises(ValidationError):
43+
CreateOrderRequest(customer_name=customer_name, order_item_count=order_item_count)
44+
45+
46+
def test_invalid_order_number_float():
47+
# Given: An invalid float number order_item_count
48+
customer_name = 'a'
49+
order_item_count = 3.14
4050

4151
# When & Then: CreateOrderRequest is initialized, expect a ValidationError
4252
with pytest.raises(ValidationError):

tests/unit/test_dal_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import uuid
2-
from datetime import datetime
2+
from datetime import datetime, timezone
33

44
import pytest
55
from aws_lambda_powertools.utilities.parser import ValidationError
66

77
from service.dal.models.db import OrderEntry
88

99
order_id = str(uuid.uuid4())
10-
created_at = int(datetime.utcnow().timestamp())
10+
created_at = int(datetime.now(timezone.utc).timestamp())
1111

1212

1313
def test_invalid_items_type():

0 commit comments

Comments
 (0)