Skip to content

Commit

Permalink
Support unique_items (#746)
Browse files Browse the repository at this point in the history
* Support unique_items

* Fix format
  • Loading branch information
koxudaxi authored Apr 16, 2022
1 parent e15e74d commit 42e99fe
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions datamodel_code_generator/model/pydantic/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Constraints(ConstraintsBase):
min_length: Optional[int] = Field(None, alias='minLength')
max_length: Optional[int] = Field(None, alias='maxLength')
regex: Optional[str] = Field(None, alias='pattern')
unique_items: Optional[bool] = Field(None, alias='uniqueItems')


class DataModelField(DataModelFieldBase):
Expand Down
3 changes: 2 additions & 1 deletion datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class JsonSchemaObject(BaseModel):
'minLength',
'maxLength',
'pattern',
'uniqueItems',
}
__extra_key__: str = SPECIAL_PATH_FORMAT.format('extras')

Expand Down Expand Up @@ -157,7 +158,7 @@ def validate_ref(cls, value: Any) -> Any:
return value

items: Union[List['JsonSchemaObject'], 'JsonSchemaObject', None]
uniqueItem: Optional[bool]
uniqueItems: Optional[bool]
type: Union[str, List[str], None]
format: Optional[str]
pattern: Optional[str]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Pet(BaseModel):


class Pets(BaseModel):
__root__: List[Pet] = Field(..., max_items=10, min_items=1)
__root__: List[Pet] = Field(..., max_items=10, min_items=1, unique_items=True)


class UID(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Pet(BaseModel):


class Pets(BaseModel):
__root__: Annotated[List[Pet], Field(max_items=10, min_items=1)]
__root__: Annotated[List[Pet], Field(max_items=10, min_items=1, unique_items=True)]


class UID(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Pet(BaseModel):


class Pets(BaseModel):
__root__: List[Pet] = Field(..., max_items=10, min_items=1)
__root__: List[Pet] = Field(..., max_items=10, min_items=1, unique_items=True)


class UID(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Pet(BaseModel):


class Pets(BaseModel):
__root__: List[Pet] = Field(..., max_items=10, min_items=1)
__root__: List[Pet] = Field(..., max_items=10, min_items=1, unique_items=True)


class UID(BaseModel):
Expand Down
1 change: 1 addition & 0 deletions tests/data/openapi/api_constrained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ components:
$ref: "#/components/schemas/Pet"
maxItems: 10
minItems: 1
uniqueItems: true
UID:
type: integer
minimum: 0
Expand Down

0 comments on commit 42e99fe

Please sign in to comment.