Skip to content

Commit

Permalink
Add min/max params for numbers (#80)
Browse files Browse the repository at this point in the history
* Add min/max params for numbers
  • Loading branch information
artsmolin authored Mar 20, 2024
1 parent 36592c0 commit 20ce3d4
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ repos:
stages: [ commit ]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.961
rev: v1.9.0
hooks:
- id: mypy
additional_dependencies: [ types-PyYAML ]
args: [--python-version=3.10, --ignore-missing-imports]
2 changes: 1 addition & 1 deletion examples/petstore/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore/client_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pythogen"
version = "0.2.39"
version = "0.2.40"
description = "Generator of python HTTP-clients from OpenApi specification."
homepage = "https://github.com/artsmolin/pythogen"
repository = "https://github.com/artsmolin/pythogen"
Expand Down
6 changes: 6 additions & 0 deletions pythogen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ class SchemaObject:
any_of: list["SchemaObject"] = field(default_factory=list)
discriminator: Discriminator | None = None

# numbers
minimum: int | None = None
maximum: int | None = None
exclusive_minimum: bool = False
exclusive_maximum: bool = False

# Технические поля
discriminator_base_class_schema: DiscriminatorBaseClassSchema | None = None
is_fake: bool = False
Expand Down
Empty file added pythogen/parsers/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions pythogen/parsers/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


if TYPE_CHECKING:
from parsers import SchemaParser
from references import RefResolver
from pythogen.parsers.references import RefResolver
from pythogen.parsers.schemas import SchemaParser


logger = logging.getLogger(__name__)
Expand Down
8 changes: 8 additions & 0 deletions pythogen/parsers/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def parse_item(
any_of=any_of,
is_inline=is_inline,
discriminator=self._parse_discriminator(schema_data),
minimum=schema_data.get("minimum"),
maximum=schema_data.get("maximum"),
exclusive_minimum=schema_data.get("exclusiveMinimum", False),
exclusive_maximum=schema_data.get("exclusiveMaximum", False),
)

discr_schema = self._get_discriminator_base_class_schema(schema_data)
Expand All @@ -133,6 +137,10 @@ def parse_item(
any_of=any_of,
is_inline=is_inline,
discriminator=self._parse_discriminator(schema_data),
minimum=schema_data.get("minimum"),
maximum=schema_data.get("maximum"),
exclusive_minimum=schema_data.get("exclusiveMinimum", False),
exclusive_maximum=schema_data.get("exclusiveMaximum", False),
)

def _parse_all_of(self, parent_id: str, parent_data: dict[str, Any]) -> list[models.SchemaObject]:
Expand Down
12 changes: 12 additions & 0 deletions pythogen/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,18 @@ def propertyfield(property: models.SchemaProperty, parent_schema: models.SchemaO
else:
args.append("...")

if property.schema.minimum is not None:
if property.schema.exclusive_minimum:
args.append(f"gt={property.schema.minimum}")
else:
args.append(f"ge={property.schema.minimum}")

if property.schema.maximum is not None:
if property.schema.exclusive_maximum:
args.append(f"lt={property.schema.maximum}")
else:
args.append(f"le={property.schema.maximum}")

if property.safety_key and property.safety_key != property.orig_key:
args.append(f'alias="{property.orig_key}"')

Expand Down
6 changes: 5 additions & 1 deletion tests/clients/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -389,6 +389,8 @@ class GetObjectNoRefSchemaResponse200(BaseModel):
)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
integer_data_all_params: int | None = Field(None, gt=1, lt=20)
integer_data_min_max: int | None = Field(None, ge=1, le=20)
array_data: list[str] | None = None
boolean_data: bool | None = None
array_of_dicts_data: list[dict[Any, Any]] | None = None
Expand Down Expand Up @@ -695,6 +697,7 @@ class GetObjectResp(BaseModel):
model_config = ConfigDict(
populate_by_name=True, # Addressing by field name, even if there is an alias.
)
integer_data_all_params: int = Field(..., gt=1, lt=20)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
array_data: list[str] | None = None
Expand All @@ -706,6 +709,7 @@ class GetObjectResp(BaseModel):
childs: list[GetObjectResp] | None = None
animal: AnimalObj | None = None
dictOdArrayOfDicts: DictOdArrayOfDictsObj | None = Field(None, alias="dictOdArrayOfDicts")
integer_data_min_max: int | None = Field(None, ge=1, le=20)


class GetMessageResp(BaseModel):
Expand Down
6 changes: 5 additions & 1 deletion tests/clients/async_client_with_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -389,6 +389,8 @@ class GetObjectNoRefSchemaResponse200(BaseModel):
)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
integer_data_all_params: int | None = Field(None, gt=1, lt=20)
integer_data_min_max: int | None = Field(None, ge=1, le=20)
array_data: list[str] | None = None
boolean_data: bool | None = None
array_of_dicts_data: list[dict[Any, Any]] | None = None
Expand Down Expand Up @@ -695,6 +697,7 @@ class GetObjectResp(BaseModel):
model_config = ConfigDict(
populate_by_name=True, # Addressing by field name, even if there is an alias.
)
integer_data_all_params: int = Field(..., gt=1, lt=20)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
array_data: list[str] | None = None
Expand All @@ -706,6 +709,7 @@ class GetObjectResp(BaseModel):
childs: list[GetObjectResp] | None = None
animal: AnimalObj | None = None
dictOdArrayOfDicts: DictOdArrayOfDictsObj | None = Field(None, alias="dictOdArrayOfDicts")
integer_data_min_max: int | None = Field(None, ge=1, le=20)


class GetMessageResp(BaseModel):
Expand Down
6 changes: 5 additions & 1 deletion tests/clients/async_client_with_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -421,6 +421,8 @@ class GetObjectNoRefSchemaResponse200(BaseModel):
)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
integer_data_all_params: int | None = Field(None, gt=1, lt=20)
integer_data_min_max: int | None = Field(None, ge=1, le=20)
array_data: list[str] | None = None
boolean_data: bool | None = None
array_of_dicts_data: list[dict[Any, Any]] | None = None
Expand Down Expand Up @@ -727,6 +729,7 @@ class GetObjectResp(BaseModel):
model_config = ConfigDict(
populate_by_name=True, # Addressing by field name, even if there is an alias.
)
integer_data_all_params: int = Field(..., gt=1, lt=20)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
array_data: list[str] | None = None
Expand All @@ -738,6 +741,7 @@ class GetObjectResp(BaseModel):
childs: list[GetObjectResp] | None = None
animal: AnimalObj | None = None
dictOdArrayOfDicts: DictOdArrayOfDictsObj | None = Field(None, alias="dictOdArrayOfDicts")
integer_data_min_max: int | None = Field(None, ge=1, le=20)


class GetMessageResp(BaseModel):
Expand Down
6 changes: 5 additions & 1 deletion tests/clients/sync_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -389,6 +389,8 @@ class GetObjectNoRefSchemaResponse200(BaseModel):
)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
integer_data_all_params: int | None = Field(None, gt=1, lt=20)
integer_data_min_max: int | None = Field(None, ge=1, le=20)
array_data: list[str] | None = None
boolean_data: bool | None = None
array_of_dicts_data: list[dict[Any, Any]] | None = None
Expand Down Expand Up @@ -695,6 +697,7 @@ class GetObjectResp(BaseModel):
model_config = ConfigDict(
populate_by_name=True, # Addressing by field name, even if there is an alias.
)
integer_data_all_params: int = Field(..., gt=1, lt=20)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
array_data: list[str] | None = None
Expand All @@ -706,6 +709,7 @@ class GetObjectResp(BaseModel):
childs: list[GetObjectResp] | None = None
animal: AnimalObj | None = None
dictOdArrayOfDicts: DictOdArrayOfDictsObj | None = Field(None, alias="dictOdArrayOfDicts")
integer_data_min_max: int | None = Field(None, ge=1, le=20)


class GetMessageResp(BaseModel):
Expand Down
6 changes: 5 additions & 1 deletion tests/clients/sync_client_with_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Generator info:
# GitHub Page: https://github.com/artsmolin/pythogen
# Version: 0.2.39
# Version: 0.2.40
# ==============================================================================

# jinja2: lstrip_blocks: "True"
Expand Down Expand Up @@ -421,6 +421,8 @@ class GetObjectNoRefSchemaResponse200(BaseModel):
)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
integer_data_all_params: int | None = Field(None, gt=1, lt=20)
integer_data_min_max: int | None = Field(None, ge=1, le=20)
array_data: list[str] | None = None
boolean_data: bool | None = None
array_of_dicts_data: list[dict[Any, Any]] | None = None
Expand Down Expand Up @@ -727,6 +729,7 @@ class GetObjectResp(BaseModel):
model_config = ConfigDict(
populate_by_name=True, # Addressing by field name, even if there is an alias.
)
integer_data_all_params: int = Field(..., gt=1, lt=20)
string_data: str | None = Field(None, description="String Data. [__discriminator__(BaseObjectResp.string_data)]")
integer_data: int | None = None
array_data: list[str] | None = None
Expand All @@ -738,6 +741,7 @@ class GetObjectResp(BaseModel):
childs: list[GetObjectResp] | None = None
animal: AnimalObj | None = None
dictOdArrayOfDicts: DictOdArrayOfDictsObj | None = Field(None, alias="dictOdArrayOfDicts")
integer_data_min_max: int | None = Field(None, ge=1, le=20)


class GetMessageResp(BaseModel):
Expand Down
25 changes: 25 additions & 0 deletions tests/docs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ paths:
integer_data:
title: Integer Data
type: integer
integer_data_all_params:
title: Integer Data With Params
type: integer
minimum: 1
maximum: 20
exclusiveMinimum: true
exclusiveMaximum: true
integer_data_min_max:
title: Integer Data With Params
type: integer
minimum: 1
maximum: 20
array_data:
title: Array Data
type: array
Expand Down Expand Up @@ -640,6 +652,7 @@ components:
GetObjectResp:
title: GetObjectResp
type: object
required: [integer_data_all_params]
properties:
string_data:
title: String Data
Expand Down Expand Up @@ -695,6 +708,18 @@ components:
additionalProperties: true
- type: object
additionalProperties: true
integer_data_all_params:
title: Integer Data With Params
type: integer
minimum: 1
maximum: 20
exclusiveMinimum: true
exclusiveMaximum: true
integer_data_min_max:
title: Integer Data With Params
type: integer
minimum: 1
maximum: 20
additionalProperties: false
PostObjectData:
title: PostObjectData
Expand Down
12 changes: 9 additions & 3 deletions tests/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ async def get_object(request: web.Request) -> web.json_response:
if return_error == 'true':
return web.json_response(data={'code': 'unknown_error'}, status=500)
return web.json_response(
data={'string_data': object_id, 'integer_data': 1, 'array_data': ['1', '2', '3'], 'boolean_data': True}
data={
'string_data': object_id,
'integer_data': 1,
'array_data': ['1', '2', '3'],
'boolean_data': True,
'integer_data_all_params': 2,
},
)


Expand Down Expand Up @@ -91,8 +97,8 @@ async def delete_object(request: web.Request) -> web.json_response:
async def get_list_objects(request: web.Request) -> web.json_response:
return web.json_response(
data=[
{'string_data': '1', 'integer_data': 1, 'array_data': ['1', '2', '3'], 'boolean_data': True},
{'string_data': '1', 'integer_data': 1, 'array_data': ['1', '2', '3'], 'boolean_data': True},
{'string_data': '1', 'integer_data': 1, 'array_data': ['1', '2', '3'], 'boolean_data': True, 'integer_data_all_params': 2},
{'string_data': '1', 'integer_data': 1, 'array_data': ['1', '2', '3'], 'boolean_data': True, 'integer_data_all_params': 3},
]
)

Expand Down

0 comments on commit 20ce3d4

Please sign in to comment.