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

Commit 413fbd6

Browse files
committed
v3 adds new overload (#184)
* Adds allTypes method in CodegenSchema * Adds and uses new args template * Updates new to use overloads when there are 2 or more types * Adds new overloads when type is unset * Fixes AnyType type hint for array items * Samples regenerated
1 parent 4b13be5 commit 413fbd6

File tree

224 files changed

+8762
-5559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

224 files changed

+8762
-5559
lines changed

modules/openapi-json-schema-generator/src/main/java/org/openapijsonschematools/codegen/model/CodegenSchema.java

+21
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,27 @@ public String toString() {
374374
return sb.toString();
375375
}
376376

377+
/**
378+
* A method to get all possible types
379+
* Also returns additional bytes and file as types when type is unset
380+
* @return the allowed types
381+
*/
382+
public LinkedHashSet<String> allTypes() {
383+
if (types != null) {
384+
return types;
385+
}
386+
LinkedHashSet<String> allTypes = new LinkedHashSet<>();
387+
allTypes.add("null");
388+
allTypes.add("boolean");
389+
allTypes.add("number");
390+
allTypes.add("string");
391+
allTypes.add("array");
392+
allTypes.add("object");
393+
allTypes.add("bytes");
394+
allTypes.add("file");
395+
return allTypes;
396+
}
397+
377398
@Override
378399
public boolean equals(Object o) {
379400
if (this == o) return true;
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,28 @@
1+
{{#if types}}
2+
{{#gt types.size 1}}
3+
{{#each types}}
4+
@typing.overload
5+
def __new__(
6+
{{> components/schemas/_helper_new_args }}
7+
) -> {{../jsonPathPiece.camelCase}}{{> components/schemas/_helper_new_generic_type }}
8+
{{/each}}
9+
{{/gt}}
10+
{{else}}
11+
{{#each allTypes}}
12+
@typing.overload
13+
def __new__(
14+
{{> components/schemas/_helper_new_args }}
15+
) -> {{../jsonPathPiece.camelCase}}{{> components/schemas/_helper_new_generic_type }}
16+
{{/each}}
17+
{{/if}}
118
def __new__(
2-
cls,
319
{{#if types}}
420
{{#eq types.size 1}}
5-
{{#contains types "array"}}
6-
arg: typing.Sequence[
7-
{{#with ../items}}
8-
{{#if refInfo.refClass}}
9-
typing.Union[
10-
{{> components/schemas/_helper_new_ref_property_value_type optional=false }}
11-
]
12-
{{else}}
13-
typing.Union[
14-
{{> components/schemas/_helper_new_property_value_type optional=false }}
15-
]
16-
{{/if}}
17-
{{/with}}
18-
],
19-
{{/contains}}
20-
{{#contains types "object"}}
21-
arg: typing.Union[
22-
{{mapInputJsonPathPiece.camelCase}},
23-
{{jsonPathPiece.camelCase}}[frozendict.frozendict],
24-
],
25-
{{/contains}}
26-
{{#contains types "string"}}
27-
arg: {{> _helper_schema_python_types }},
28-
{{/contains}}
29-
{{#contains types "number"}}
30-
arg: typing.Union[{{> _helper_schema_python_types }}],
31-
{{/contains}}
32-
{{#contains types "integer"}}
33-
arg: {{> _helper_schema_python_types }},
34-
{{/contains}}
35-
{{#contains types "boolean"}}
36-
arg: {{> _helper_schema_python_types }},
37-
{{/contains}}
38-
{{#contains types "null"}}
39-
arg: {{> _helper_schema_python_types }},
40-
{{/contains}}
21+
{{#each types}}
22+
{{> components/schemas/_helper_new_args }}
23+
{{/each}}
4124
{{else}}
25+
cls,
4226
arg: typing.Union[
4327
{{#each types}}
4428
{{#eq this "object"}}
@@ -49,58 +33,24 @@ def __new__(
4933
{{/eq}}
5034
{{/each}}
5135
],
36+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
5237
{{/eq}}
5338
{{else}}
54-
{{#if mapInputJsonPathPiece}}
55-
arg: typing.Union[
56-
{{mapInputJsonPathPiece.camelCase}},
57-
schemas.INPUT_TYPES_ALL_INCL_SCHEMA
58-
],
59-
{{else}}
39+
cls,
6040
arg: schemas.INPUT_TYPES_ALL_INCL_SCHEMA,
61-
{{/if}}
62-
{{/if}}
6341
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
42+
{{/if}}
6443
{{#if types}}
6544
{{#eq types.size 1}}
6645
) -> {{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}]:
6746
{{else}}
68-
) -> {{jsonPathPiece.camelCase}}[
69-
typing.Union[
70-
{{> components/schemas/_helper_schema_python_base_types_newline }}
71-
]
72-
]:
47+
):
7348
{{/eq}}
7449
{{else}}
75-
) -> {{jsonPathPiece.camelCase}}[
76-
typing.Union[
77-
{{> components/schemas/_helper_schema_python_base_types_newline }}
78-
]
79-
]:
50+
):
8051
{{/if}}
81-
inst = super().__new__(
52+
return super().__new__(
8253
cls,
8354
arg,
8455
configuration=configuration,
8556
)
86-
inst = typing.cast(
87-
{{#if types}}
88-
{{#eq types.size 1}}
89-
{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}],
90-
{{else}}
91-
{{jsonPathPiece.camelCase}}[
92-
typing.Union[
93-
{{> components/schemas/_helper_schema_python_base_types_newline }}
94-
]
95-
],
96-
{{/eq}}
97-
{{else}}
98-
{{jsonPathPiece.camelCase}}[
99-
typing.Union[
100-
{{> components/schemas/_helper_schema_python_base_types_newline }}
101-
]
102-
],
103-
{{/if}}
104-
inst
105-
)
106-
return inst
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
cls,
2+
{{#eq this "array"}}
3+
{{#if ../items}}
4+
arg: typing.Sequence[
5+
{{#with ../items}}
6+
{{#if refInfo.refClass}}
7+
typing.Union[
8+
{{> components/schemas/_helper_new_ref_property_value_type optional=false }}
9+
]
10+
{{else}}
11+
typing.Union[
12+
{{> components/schemas/_helper_new_property_value_type optional=false }}
13+
]
14+
{{/if}}
15+
{{/with}}
16+
],
17+
{{else}}
18+
arg: typing.Sequence[schemas.INPUT_TYPES_ALL_INCL_SCHEMA],
19+
{{/if}}
20+
{{else}}
21+
{{#eq this "object"}}
22+
arg: typing.Union[
23+
{{mapInputJsonPathPiece.camelCase}},
24+
{{jsonPathPiece.camelCase}}[frozendict.frozendict],
25+
],
26+
{{else}}
27+
{{#eq this "string"}}
28+
arg: typing.Union[str, datetime.date, datetime.datetime, uuid.UUID],
29+
{{else}}
30+
{{#eq this "number"}}
31+
arg: typing.Union[decimal.Decimal, float, int],
32+
{{else}}
33+
{{#eq this "integer"}}
34+
arg: typing.Union[decimal.Decimal, int],
35+
{{else}}
36+
{{#eq this "boolean"}}
37+
arg: typing.Union[bool, schemas.BoolClass],
38+
{{else}}
39+
{{#eq this "null"}}
40+
arg: typing.Union[None, schemas.NoneClass],
41+
{{else}}
42+
{{#eq this "bytes"}}
43+
arg: bytes,
44+
{{else}}
45+
{{#eq this "file"}}
46+
arg: io.FileIO,
47+
{{/eq}}
48+
{{/eq}}
49+
{{/eq}}
50+
{{/eq}}
51+
{{/eq}}
52+
{{/eq}}
53+
{{/eq}}
54+
{{/eq}}
55+
{{/eq}}
56+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{{#eq this "array"}}
2+
[tuple]: ...
3+
{{else}}
4+
{{#eq this "object"}}
5+
[frozendict.frozendict]: ...
6+
{{else}}
7+
{{#eq this "string"}}
8+
[str]: ...
9+
{{else}}
10+
{{#eq this "number"}}
11+
[decimal.Decimal]: ...
12+
{{else}}
13+
{{#eq this "integer"}}
14+
[decimal.Decimal]: ...
15+
{{else}}
16+
{{#eq this "boolean"}}
17+
[schemas.BoolClass]: ...
18+
{{else}}
19+
{{#eq this "null"}}
20+
[schemas.NoneClass]: ...
21+
{{else}}
22+
{{#eq this "bytes"}}
23+
[bytes]: ...
24+
{{else}}
25+
{{#eq this "file"}}
26+
[schemas.FileIO]: ...
27+
{{/eq}}
28+
{{/eq}}
29+
{{/eq}}
30+
{{/eq}}
31+
{{/eq}}
32+
{{/eq}}
33+
{{/eq}}
34+
{{/eq}}
35+
{{/eq}}

samples/openapi3/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/_not.py

+59-30
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,73 @@ class Schema_(metaclass=schemas.SingletonMeta):
3030
not_: typing.Type[Not2] = dataclasses.field(default_factory=lambda: Not2) # type: ignore
3131

3232

33+
@typing.overload
34+
def __new__(
35+
cls,
36+
arg: typing.Union[None, schemas.NoneClass],
37+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
38+
) -> _Not[schemas.NoneClass]: ...
39+
40+
@typing.overload
41+
def __new__(
42+
cls,
43+
arg: typing.Union[bool, schemas.BoolClass],
44+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
45+
) -> _Not[schemas.BoolClass]: ...
46+
47+
@typing.overload
48+
def __new__(
49+
cls,
50+
arg: typing.Union[decimal.Decimal, float, int],
51+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
52+
) -> _Not[decimal.Decimal]: ...
53+
54+
@typing.overload
55+
def __new__(
56+
cls,
57+
arg: typing.Union[str, datetime.date, datetime.datetime, uuid.UUID],
58+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
59+
) -> _Not[str]: ...
60+
61+
@typing.overload
62+
def __new__(
63+
cls,
64+
arg: typing.Sequence[schemas.INPUT_TYPES_ALL_INCL_SCHEMA],
65+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
66+
) -> _Not[tuple]: ...
67+
68+
@typing.overload
3369
def __new__(
3470
cls,
3571
arg: typing.Union[
3672
DictInput,
37-
schemas.INPUT_TYPES_ALL_INCL_SCHEMA
73+
_Not[frozendict.frozendict],
3874
],
3975
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
40-
) -> _Not[
41-
typing.Union[
42-
frozendict.frozendict,
43-
str,
44-
decimal.Decimal,
45-
schemas.BoolClass,
46-
schemas.NoneClass,
47-
tuple,
48-
bytes,
49-
schemas.FileIO
50-
]
51-
]:
52-
inst = super().__new__(
76+
) -> _Not[frozendict.frozendict]: ...
77+
78+
@typing.overload
79+
def __new__(
80+
cls,
81+
arg: bytes,
82+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
83+
) -> _Not[bytes]: ...
84+
85+
@typing.overload
86+
def __new__(
87+
cls,
88+
arg: io.FileIO,
89+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
90+
) -> _Not[schemas.FileIO]: ...
91+
92+
def __new__(
93+
cls,
94+
arg: schemas.INPUT_TYPES_ALL_INCL_SCHEMA,
95+
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
96+
):
97+
return super().__new__(
5398
cls,
5499
arg,
55100
configuration=configuration,
56101
)
57-
inst = typing.cast(
58-
_Not[
59-
typing.Union[
60-
frozendict.frozendict,
61-
str,
62-
decimal.Decimal,
63-
schemas.BoolClass,
64-
schemas.NoneClass,
65-
tuple,
66-
bytes,
67-
schemas.FileIO
68-
]
69-
],
70-
inst
71-
)
72-
return inst
73102

samples/openapi3/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/additionalproperties_allows_a_schema_which_should_validate.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,9 @@ def __new__(
137137
],
138138
configuration: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None
139139
) -> AdditionalpropertiesAllowsASchemaWhichShouldValidate[frozendict.frozendict]:
140-
inst = super().__new__(
140+
return super().__new__(
141141
cls,
142142
arg,
143143
configuration=configuration,
144144
)
145-
inst = typing.cast(
146-
AdditionalpropertiesAllowsASchemaWhichShouldValidate[frozendict.frozendict],
147-
inst
148-
)
149-
return inst
150145

0 commit comments

Comments
 (0)