Skip to content

Commit

Permalink
Change to use min_length and max_length for Pydantic Field for list p…
Browse files Browse the repository at this point in the history
…aram type
  • Loading branch information
yugokato committed Sep 25, 2024
1 parent 34a052a commit e194158
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ Here are some comparisons between regular models and pydantic models:
>>> model = client.USERS.create_user.endpoint.model
<class 'types.UsersAPICreateUserEndpointModel'>
>>> pprint(model.__dataclass_fields__, sort_dicts=False)
{'first_name': Field(name='first_name',type=typing.Annotated[str, Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, min_items=None, max_items=None, nullable=None, exclusive_minimum=None, exclusive_maximum=None)],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD),
'last_name': Field(name='last_name',type=typing.Annotated[str, Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, min_items=None, max_items=None, nullable=None, exclusive_minimum=None, exclusive_maximum=None)],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD),
{'first_name': Field(name='first_name',type=typing.Annotated[str, Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, nullable=None, exclusive_minimum=None, exclusive_maximum=None)],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD),
'last_name': Field(name='last_name',type=typing.Annotated[str, Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, nullable=None, exclusive_minimum=None, exclusive_maximum=None)],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD),
'email': Field(name='email',type=typing.Annotated[str, Format(value='email')],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD),
'role': Field(name='role',type=typing.Literal['admin', 'viewer', 'support'],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD),
'metadata': Field(name='metadata',type=typing.Optional[openapi_test_client.clients.demo_app.models.users.Metadata],default=None,default_factory=<dataclasses._MISSING_TYPE object at 0x104b81f10>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=True,_field_type=_FIELD)}
Expand Down Expand Up @@ -873,8 +873,8 @@ Here are some comparisons between regular models and pydantic models:
>>> pydantic_model = client.USERS.create_user.endpoint.model.to_pydantic()
<class 'types.UsersAPICreateUserEndpointModel'>
>>> pprint(pydantic_model.model_fields)
{'first_name': FieldInfo(annotation=str, required=True, metadata=[MinLen(min_length=1), MaxLen(max_length=255), Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, min_items=None, max_items=None, nullable=None, exclusive_minimum=None, exclusive_maximum=None)]),
'last_name': FieldInfo(annotation=str, required=True, metadata=[MinLen(min_length=1), MaxLen(max_length=255), Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, min_items=None, max_items=None, nullable=None, exclusive_minimum=None, exclusive_maximum=None)]),
{'first_name': FieldInfo(annotation=str, required=True, metadata=[MinLen(min_length=1), MaxLen(max_length=255), Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, nullable=None, exclusive_minimum=None, exclusive_maximum=None)]),
'last_name': FieldInfo(annotation=str, required=True, metadata=[MinLen(min_length=1), MaxLen(max_length=255), Constraint(min=None, max=None, multiple_of=None, min_len=1, max_len=255, nullable=None, exclusive_minimum=None, exclusive_maximum=None)]),
'email': FieldInfo(annotation=EmailStr, required=True, metadata=[Format(value='email')]),
'role': FieldInfo(annotation=Literal['admin', 'viewer', 'support'], required=True),
'metadata': FieldInfo(annotation=Union[Metadata, NoneType], required=False)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def generate_pydantic_model_fields(
const.update(multiple_of=constraint.multiple_of)
field_value = Field(default_value, **const)
elif param_type_util.is_type_of(field_type, list):
field_value = Field(default_value, min_items=constraint.min_items, max_items=constraint.max_items)
field_value = Field(default_value, min_length=constraint.min_len, max_length=constraint.max_len)

if default_value is not None and constraint.nullable:
# Required and nullable = Optional
Expand Down
6 changes: 2 additions & 4 deletions src/openapi_test_client/libraries/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def __init__(self, obj: dict[str, Any]):
super().__init__(obj)
if self.is_array:
self.constraint = Constraint(
min_items=self.get("minItems"),
max_items=self.get("maxItems"),
min_len=self.get("minItems"),
max_len=self.get("maxItems"),
nullable=self.get("nullable"),
)
else:
Expand Down Expand Up @@ -440,8 +440,6 @@ class Constraint(ParamAnnotationType):
multiple_of: int = None
min_len: int = None
max_len: int = None
min_items: int = None
max_items: int = None
nullable: bool = None
pattern: str = None

Expand Down

0 comments on commit e194158

Please sign in to comment.