Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit 4f97227

Browse files
authored
Fixes issue 43, fixes new type hint for required prop that is not in properties (#44)
* Adds test sample schema and fixes template * Samples generated, test added
1 parent 4311511 commit 4f97227

15 files changed

+571
-1
lines changed

modules/openapi-json-schema-generator/src/main/resources/python/model_templates/new.handlebars

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ def __new__(
1717
{{#if complexType}}
1818
{{baseName}}: '{{complexType}}',
1919
{{else}}
20+
{{#if getSchemaIsFromAdditionalProperties}}
21+
{{#if addPropsUnset}}
22+
{{baseName}}: typing.Union[schemas.AnyTypeSchema, {{> model_templates/schema_python_types }}],
23+
{{else}}
24+
{{baseName}}: typing.Union[MetaOapg.additional_properties, {{> model_templates/schema_python_types }}],
25+
{{/if}}
26+
{{else}}
2027
{{baseName}}: typing.Union[MetaOapg.properties.{{baseName}}, {{> model_templates/schema_python_types }}],
28+
{{/if}}
2129
{{/if}}
2230
{{/unless}}
2331
{{/with}}

modules/openapi-json-schema-generator/src/main/resources/python/model_templates/schema_dict.handlebars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ class {{> model_templates/classname }}(
3232
{{/if}}
3333
{{> model_templates/property_type_hints }}
3434

35+
{{#if additionalProperties}}
3536
{{> model_templates/new }}
37+
{{else}}
38+
{{> model_templates/new addPropsUnset=true }}
39+
{{/if}}

modules/openapi-json-schema-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2978,4 +2978,17 @@ components:
29782978
$ref: "#/components/schemas/ArrayWithValidationsInItems"
29792979
required:
29802980
- from
2981-
- "!reference"
2981+
- "!reference"
2982+
ObjectWithOptionalTestProp:
2983+
type: object
2984+
properties:
2985+
test:
2986+
type: string
2987+
ObjectWithAllOfWithReqTestPropFromUnsetAddProp:
2988+
allOf:
2989+
- $ref: '#/components/schemas/ObjectWithOptionalTestProp'
2990+
- type: object
2991+
required: [ test ]
2992+
properties:
2993+
name:
2994+
type: string

samples/openapi3/client/petstore/python/.openapi-generator/FILES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ docs/models/NumberOnly.md
9797
docs/models/NumberWithValidations.md
9898
docs/models/ObjectInterface.md
9999
docs/models/ObjectModelWithRefProps.md
100+
docs/models/ObjectWithAllOfWithReqTestPropFromUnsetAddProp.md
100101
docs/models/ObjectWithDecimalProperties.md
101102
docs/models/ObjectWithDifficultlyNamedProps.md
102103
docs/models/ObjectWithInlineCompositionProperty.md
103104
docs/models/ObjectWithInvalidNamedRefedProperties.md
105+
docs/models/ObjectWithOptionalTestProp.md
104106
docs/models/ObjectWithValidations.md
105107
docs/models/Order.md
106108
docs/models/ParentPet.md
@@ -318,6 +320,8 @@ petstore_api/model/object_interface.py
318320
petstore_api/model/object_interface.pyi
319321
petstore_api/model/object_model_with_ref_props.py
320322
petstore_api/model/object_model_with_ref_props.pyi
323+
petstore_api/model/object_with_all_of_with_req_test_prop_from_unset_add_prop.py
324+
petstore_api/model/object_with_all_of_with_req_test_prop_from_unset_add_prop.pyi
321325
petstore_api/model/object_with_decimal_properties.py
322326
petstore_api/model/object_with_decimal_properties.pyi
323327
petstore_api/model/object_with_difficultly_named_props.py
@@ -326,6 +330,8 @@ petstore_api/model/object_with_inline_composition_property.py
326330
petstore_api/model/object_with_inline_composition_property.pyi
327331
petstore_api/model/object_with_invalid_named_refed_properties.py
328332
petstore_api/model/object_with_invalid_named_refed_properties.pyi
333+
petstore_api/model/object_with_optional_test_prop.py
334+
petstore_api/model/object_with_optional_test_prop.pyi
329335
petstore_api/model/object_with_validations.py
330336
petstore_api/model/object_with_validations.pyi
331337
petstore_api/model/order.py

samples/openapi3/client/petstore/python/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ Class | Method | HTTP request | Description
319319
- [NumberWithValidations](docs/models/NumberWithValidations.md)
320320
- [ObjectInterface](docs/models/ObjectInterface.md)
321321
- [ObjectModelWithRefProps](docs/models/ObjectModelWithRefProps.md)
322+
- [ObjectWithAllOfWithReqTestPropFromUnsetAddProp](docs/models/ObjectWithAllOfWithReqTestPropFromUnsetAddProp.md)
322323
- [ObjectWithDecimalProperties](docs/models/ObjectWithDecimalProperties.md)
323324
- [ObjectWithDifficultlyNamedProps](docs/models/ObjectWithDifficultlyNamedProps.md)
324325
- [ObjectWithInlineCompositionProperty](docs/models/ObjectWithInlineCompositionProperty.md)
325326
- [ObjectWithInvalidNamedRefedProperties](docs/models/ObjectWithInvalidNamedRefedProperties.md)
327+
- [ObjectWithOptionalTestProp](docs/models/ObjectWithOptionalTestProp.md)
326328
- [ObjectWithValidations](docs/models/ObjectWithValidations.md)
327329
- [Order](docs/models/Order.md)
328330
- [ParentPet](docs/models/ParentPet.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# petstore_api.model.object_with_all_of_with_req_test_prop_from_unset_add_prop.ObjectWithAllOfWithReqTestPropFromUnsetAddProp
2+
3+
## Model Type Info
4+
Input Type | Accessed Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, | frozendict.frozendict, str, decimal.Decimal, BoolClass, NoneClass, tuple, bytes, FileIO | |
7+
8+
### Composed Schemas (allOf/anyOf/oneOf/not)
9+
#### allOf
10+
Class Name | Input Type | Accessed Type | Description | Notes
11+
------------- | ------------- | ------------- | ------------- | -------------
12+
[ObjectWithOptionalTestProp](ObjectWithOptionalTestProp.md) | [**ObjectWithOptionalTestProp**](ObjectWithOptionalTestProp.md) | [**ObjectWithOptionalTestProp**](ObjectWithOptionalTestProp.md) | |
13+
[all_of_1](#all_of_1) | dict, frozendict.frozendict, | frozendict.frozendict, | |
14+
15+
# all_of_1
16+
17+
## Model Type Info
18+
Input Type | Accessed Type | Description | Notes
19+
------------ | ------------- | ------------- | -------------
20+
dict, frozendict.frozendict, | frozendict.frozendict, | |
21+
22+
### Dictionary Keys
23+
Key | Input Type | Accessed Type | Description | Notes
24+
------------ | ------------- | ------------- | ------------- | -------------
25+
**test** | dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, | frozendict.frozendict, str, decimal.Decimal, BoolClass, NoneClass, tuple, bytes, FileIO | |
26+
**name** | str, | str, | | [optional]
27+
**any_string_name** | dict, frozendict.frozendict, str, date, datetime, int, float, bool, decimal.Decimal, None, list, tuple, bytes, io.FileIO, io.BufferedReader | frozendict.frozendict, str, BoolClass, decimal.Decimal, NoneClass, tuple, bytes, FileIO | any string name can be used but the value must be the correct type | [optional]
28+
29+
[[Back to Model list]](../../README.md#documentation-for-models) [[Back to API list]](../../README.md#documentation-for-api-endpoints) [[Back to README]](../../README.md)
30+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# petstore_api.model.object_with_optional_test_prop.ObjectWithOptionalTestProp
2+
3+
## Model Type Info
4+
Input Type | Accessed Type | Description | Notes
5+
------------ | ------------- | ------------- | -------------
6+
dict, frozendict.frozendict, | frozendict.frozendict, | |
7+
8+
### Dictionary Keys
9+
Key | Input Type | Accessed Type | Description | Notes
10+
------------ | ------------- | ------------- | ------------- | -------------
11+
**test** | str, | str, | | [optional]
12+
**any_string_name** | dict, frozendict.frozendict, str, date, datetime, int, float, bool, decimal.Decimal, None, list, tuple, bytes, io.FileIO, io.BufferedReader | frozendict.frozendict, str, BoolClass, decimal.Decimal, NoneClass, tuple, bytes, FileIO | any string name can be used but the value must be the correct type | [optional]
13+
14+
[[Back to Model list]](../../README.md#documentation-for-models) [[Back to API list]](../../README.md#documentation-for-api-endpoints) [[Back to README]](../../README.md)
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# coding: utf-8
2+
3+
"""
4+
OpenAPI Petstore
5+
6+
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
7+
8+
The version of the OpenAPI document: 1.0.0
9+
Generated by: https://openapi-generator.tech
10+
"""
11+
12+
from datetime import date, datetime # noqa: F401
13+
import decimal # noqa: F401
14+
import functools # noqa: F401
15+
import io # noqa: F401
16+
import re # noqa: F401
17+
import typing # noqa: F401
18+
import typing_extensions # noqa: F401
19+
import uuid # noqa: F401
20+
21+
import frozendict # noqa: F401
22+
23+
from petstore_api import schemas # noqa: F401
24+
25+
26+
class ObjectWithAllOfWithReqTestPropFromUnsetAddProp(
27+
schemas.ComposedSchema,
28+
):
29+
"""NOTE: This class is auto generated by OpenAPI Generator.
30+
Ref: https://openapi-generator.tech
31+
32+
Do not edit the class manually.
33+
"""
34+
35+
36+
class MetaOapg:
37+
38+
39+
class all_of_1(
40+
schemas.DictSchema
41+
):
42+
43+
44+
class MetaOapg:
45+
required = {
46+
"test",
47+
}
48+
49+
class properties:
50+
name = schemas.StrSchema
51+
__annotations__ = {
52+
"name": name,
53+
}
54+
55+
test: schemas.AnyTypeSchema
56+
57+
@typing.overload
58+
def __getitem__(self, name: typing_extensions.Literal["name"]) -> MetaOapg.properties.name: ...
59+
60+
@typing.overload
61+
def __getitem__(self, name: str) -> schemas.UnsetAnyTypeSchema: ...
62+
63+
def __getitem__(self, name: typing.Union[typing_extensions.Literal["name", ], str]):
64+
# dict_instance[name] accessor
65+
return super().__getitem__(name)
66+
67+
68+
@typing.overload
69+
def get_item_oapg(self, name: typing_extensions.Literal["name"]) -> typing.Union[MetaOapg.properties.name, schemas.Unset]: ...
70+
71+
@typing.overload
72+
def get_item_oapg(self, name: str) -> typing.Union[schemas.UnsetAnyTypeSchema, schemas.Unset]: ...
73+
74+
def get_item_oapg(self, name: typing.Union[typing_extensions.Literal["name", ], str]):
75+
return super().get_item_oapg(name)
76+
77+
78+
def __new__(
79+
cls,
80+
*args: typing.Union[dict, frozendict.frozendict, ],
81+
test: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, ],
82+
name: typing.Union[MetaOapg.properties.name, str, schemas.Unset] = schemas.unset,
83+
_configuration: typing.Optional[schemas.Configuration] = None,
84+
**kwargs: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, None, list, tuple, bytes],
85+
) -> 'all_of_1':
86+
return super().__new__(
87+
cls,
88+
*args,
89+
test=test,
90+
name=name,
91+
_configuration=_configuration,
92+
**kwargs,
93+
)
94+
95+
@classmethod
96+
@functools.lru_cache()
97+
def all_of(cls):
98+
# we need this here to make our import statements work
99+
# we must store _composed_schemas in here so the code is only run
100+
# when we invoke this method. If we kept this at the class
101+
# level we would get an error because the class level
102+
# code would be run when this module is imported, and these composed
103+
# classes don't exist yet because their module has not finished
104+
# loading
105+
return [
106+
ObjectWithOptionalTestProp,
107+
cls.all_of_1,
108+
]
109+
110+
111+
def __new__(
112+
cls,
113+
*args: typing.Union[dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, ],
114+
_configuration: typing.Optional[schemas.Configuration] = None,
115+
**kwargs: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, None, list, tuple, bytes],
116+
) -> 'ObjectWithAllOfWithReqTestPropFromUnsetAddProp':
117+
return super().__new__(
118+
cls,
119+
*args,
120+
_configuration=_configuration,
121+
**kwargs,
122+
)
123+
124+
from petstore_api.model.object_with_optional_test_prop import ObjectWithOptionalTestProp
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# coding: utf-8
2+
3+
"""
4+
OpenAPI Petstore
5+
6+
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ # noqa: E501
7+
8+
The version of the OpenAPI document: 1.0.0
9+
Generated by: https://openapi-generator.tech
10+
"""
11+
12+
from datetime import date, datetime # noqa: F401
13+
import decimal # noqa: F401
14+
import functools # noqa: F401
15+
import io # noqa: F401
16+
import re # noqa: F401
17+
import typing # noqa: F401
18+
import typing_extensions # noqa: F401
19+
import uuid # noqa: F401
20+
21+
import frozendict # noqa: F401
22+
23+
from petstore_api import schemas # noqa: F401
24+
25+
26+
class ObjectWithAllOfWithReqTestPropFromUnsetAddProp(
27+
schemas.ComposedSchema,
28+
):
29+
"""NOTE: This class is auto generated by OpenAPI Generator.
30+
Ref: https://openapi-generator.tech
31+
32+
Do not edit the class manually.
33+
"""
34+
35+
36+
class MetaOapg:
37+
38+
39+
class all_of_1(
40+
schemas.DictSchema
41+
):
42+
43+
44+
class MetaOapg:
45+
required = {
46+
"test",
47+
}
48+
49+
class properties:
50+
name = schemas.StrSchema
51+
__annotations__ = {
52+
"name": name,
53+
}
54+
55+
test: schemas.AnyTypeSchema
56+
57+
@typing.overload
58+
def __getitem__(self, name: typing_extensions.Literal["name"]) -> MetaOapg.properties.name: ...
59+
60+
@typing.overload
61+
def __getitem__(self, name: str) -> schemas.UnsetAnyTypeSchema: ...
62+
63+
def __getitem__(self, name: typing.Union[typing_extensions.Literal["name", ], str]):
64+
# dict_instance[name] accessor
65+
return super().__getitem__(name)
66+
67+
68+
@typing.overload
69+
def get_item_oapg(self, name: typing_extensions.Literal["name"]) -> typing.Union[MetaOapg.properties.name, schemas.Unset]: ...
70+
71+
@typing.overload
72+
def get_item_oapg(self, name: str) -> typing.Union[schemas.UnsetAnyTypeSchema, schemas.Unset]: ...
73+
74+
def get_item_oapg(self, name: typing.Union[typing_extensions.Literal["name", ], str]):
75+
return super().get_item_oapg(name)
76+
77+
78+
def __new__(
79+
cls,
80+
*args: typing.Union[dict, frozendict.frozendict, ],
81+
test: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, ],
82+
name: typing.Union[MetaOapg.properties.name, str, schemas.Unset] = schemas.unset,
83+
_configuration: typing.Optional[schemas.Configuration] = None,
84+
**kwargs: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, None, list, tuple, bytes],
85+
) -> 'all_of_1':
86+
return super().__new__(
87+
cls,
88+
*args,
89+
test=test,
90+
name=name,
91+
_configuration=_configuration,
92+
**kwargs,
93+
)
94+
95+
@classmethod
96+
@functools.lru_cache()
97+
def all_of(cls):
98+
# we need this here to make our import statements work
99+
# we must store _composed_schemas in here so the code is only run
100+
# when we invoke this method. If we kept this at the class
101+
# level we would get an error because the class level
102+
# code would be run when this module is imported, and these composed
103+
# classes don't exist yet because their module has not finished
104+
# loading
105+
return [
106+
ObjectWithOptionalTestProp,
107+
cls.all_of_1,
108+
]
109+
110+
111+
def __new__(
112+
cls,
113+
*args: typing.Union[dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, ],
114+
_configuration: typing.Optional[schemas.Configuration] = None,
115+
**kwargs: typing.Union[schemas.AnyTypeSchema, dict, frozendict.frozendict, str, date, datetime, uuid.UUID, int, float, decimal.Decimal, None, list, tuple, bytes],
116+
) -> 'ObjectWithAllOfWithReqTestPropFromUnsetAddProp':
117+
return super().__new__(
118+
cls,
119+
*args,
120+
_configuration=_configuration,
121+
**kwargs,
122+
)
123+
124+
from petstore_api.model.object_with_optional_test_prop import ObjectWithOptionalTestProp

0 commit comments

Comments
 (0)