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

Commit 18f2225

Browse files
committed
Adds generic type T
1 parent 8d65f59 commit 18f2225

397 files changed

Lines changed: 3806 additions & 1931 deletions

File tree

Some content is hidden

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

modules/openapi-json-schema-generator/src/main/resources/python/api_client.hbs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,12 +1367,13 @@ class RequestBody(StyleFormSerializer, JSONDetector):
13671367
- encode_multipart and fields for multipart/form-data
13681368
"""
13691369
media_type = cls.content[content_type]
1370-
if isinstance(in_data, media_type.schema):
1370+
schema = schemas._get_class(media_type.schema)
1371+
if isinstance(in_data, schema):
13711372
cast_in_data = in_data
13721373
elif isinstance(in_data, (dict, frozendict.frozendict)) and in_data:
1373-
cast_in_data = media_type.schema(**in_data)
1374+
cast_in_data = schema(**in_data)
13741375
else:
1375-
cast_in_data = media_type.schema(in_data)
1376+
cast_in_data = schema(in_data)
13761377
# TODO check for and use encoding if it exists
13771378
# and content_type is multipart or application/x-www-form-urlencoded
13781379
if cls._content_type_is_json(content_type):

modules/openapi-json-schema-generator/src/main/resources/python/components/_helper_param_or_header_instance.hbs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,24 @@ class {{jsonPathPiece.camelCase}}(api_client.{{#if noName}}Header{{/if}}{{#eq in
3434
{{#each content}}
3535

3636

37-
class __{{@key.camelCase}}MediaType(api_client.MediaType):
37+
class {{@key.camelCase}}MediaType(api_client.MediaType):
3838
{{#with this}}
3939
{{#with schema}}
4040
schema: typing.Type[{{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}] = {{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}
4141
{{/with}}
4242
{{/with}}
4343
{{/each}}
44-
__Content = typing_extensions.TypedDict(
45-
'__Content',
44+
Content = typing_extensions.TypedDict(
45+
'Content',
4646
{
4747
{{#each content}}
48-
'{{{@key.original}}}': typing.Type[__{{@key.camelCase}}MediaType],
48+
'{{{@key.original}}}': typing.Type[{{@key.camelCase}}MediaType],
4949
{{/each}}
5050
}
5151
)
52-
content: __Content = {
52+
content: Content = {
5353
{{#each content}}
54-
'{{{@key.original}}}': __{{@key.camelCase}}MediaType,
54+
'{{{@key.original}}}': {{@key.camelCase}}MediaType,
5555
{{/each}}
5656
}
5757
{{/if}}

modules/openapi-json-schema-generator/src/main/resources/python/components/request_bodies/request_body.hbs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ class {{jsonPathPiece.camelCase}}(api_client.RequestBody):
2424
{{#each content}}
2525

2626

27-
class __{{@key.camelCase}}MediaType(api_client.MediaType):
27+
class {{@key.camelCase}}MediaType(api_client.MediaType):
2828
{{#with this}}
2929
{{#with schema}}
3030
schema: typing.Type[{{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}] = {{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}
3131
{{/with}}
3232
{{/with}}
3333
{{/each}}
34-
__Content = typing_extensions.TypedDict(
35-
'__Content',
34+
Content = typing_extensions.TypedDict(
35+
'Content',
3636
{
3737
{{#each content}}
38-
'{{{@key.original}}}': typing.Type[__{{@key.camelCase}}MediaType],
38+
'{{{@key.original}}}': typing.Type[{{@key.camelCase}}MediaType],
3939
{{/each}}
4040
}
4141
)
42-
content: __Content = {
42+
content: Content = {
4343
{{#each content}}
44-
'{{{@key.original}}}': __{{@key.camelCase}}MediaType,
44+
'{{{@key.original}}}': {{@key.camelCase}}MediaType,
4545
{{/each}}
4646
}
4747
{{#if required}}

modules/openapi-json-schema-generator/src/main/resources/python/components/responses/response.hbs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class {{jsonPathPiece.camelCase}}(api_client.OpenApiResponse[Api{{jsonPathPiece.
9696
{{#each content}}
9797

9898

99-
class __{{@key.camelCase}}MediaType(api_client.MediaType):
99+
class {{@key.camelCase}}MediaType(api_client.MediaType):
100100
{{#with this}}
101101
{{#with schema}}
102102
schema: typing.Type[{{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}] = {{../@key.snakeCase}}_{{jsonPathPiece.snakeCase}}.{{jsonPathPiece.camelCase}}
@@ -106,17 +106,17 @@ class {{jsonPathPiece.camelCase}}(api_client.OpenApiResponse[Api{{jsonPathPiece.
106106
{{/with}}
107107
{{/each}}
108108
{{#if content}}
109-
__Content = typing_extensions.TypedDict(
110-
'__Content',
109+
Content = typing_extensions.TypedDict(
110+
'Content',
111111
{
112112
{{#each content}}
113-
'{{{@key.original}}}': typing.Type[__{{@key.camelCase}}MediaType],
113+
'{{{@key.original}}}': typing.Type[{{@key.camelCase}}MediaType],
114114
{{/each}}
115115
}
116116
)
117-
content: __Content = {
117+
content: Content = {
118118
{{#each content}}
119-
'{{{@key.original}}}': __{{@key.camelCase}}MediaType,
119+
'{{{@key.original}}}': {{@key.camelCase}}MediaType,
120120
{{/each}}
121121
}
122122
{{/if}}

modules/openapi-json-schema-generator/src/main/resources/python/components/schemas/_helper_getitem.hbs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{{#if types}}
2+
{{#eq types.size 1}}
3+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}]:{{#if overload}} ...{{/if}}
4+
{{else}}
5+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[typing.Union[
6+
{{> components/schemas/_helper_schema_python_base_types_newline }}
7+
]]:{{#if overload}} ...{{/if}}
8+
{{/eq}}
9+
{{else}}
10+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[typing.Union[
11+
{{> components/schemas/_helper_schema_python_base_types_newline }}
12+
]]:{{#if overload}} ...{{/if}}
13+
{{/if}}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{{#with getDeepestRef}}
2+
{{#if types}}
3+
{{#eq types.size 1}}
4+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{#if ../refInfo.refModule}}{{../refInfo.refModule}}.{{/if}}{{../refInfo.refClass}}[{{> components/schemas/_helper_schema_python_base_types }}]:{{#if overload}} ...{{/if}}
5+
{{else}}
6+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{#if ../refInfo.refModule}}{{../refInfo.refModule}}.{{/if}}{{../refInfo.refClass}}[typing.Union[
7+
{{> components/schemas/_helper_schema_python_base_types_newline }}
8+
]]:{{#if overload}} ...{{/if}}
9+
{{/eq}}
10+
{{else}}
11+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{#if ../refInfo.refModule}}{{../refInfo.refModule}}.{{/if}}{{../refInfo.refClass}}[typing.Union[
12+
{{> components/schemas/_helper_schema_python_base_types_newline }}
13+
]]:{{#if overload}} ...{{/if}}
14+
{{/if}}
15+
{{/with}}

modules/openapi-json-schema-generator/src/main/resources/python/components/schemas/_helper_getitems.hbs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,93 @@
44

55
@typing.overload
66
{{#if refInfo.refClass}}
7-
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> '{{> components/schemas/_helper_refclass_with_module }}': ...
7+
{{> components/schemas/_helper_getitem_refclass literal=true key=@key.original overload=true }}
88
{{else}}
99
{{#if jsonPathPiece}}
1010
{{#if schemaIsFromAdditionalProperties}}
11-
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> Schema_.{{jsonPathPiece.camelCase}}: ...
11+
{{> components/schemas/_helper_getitem_property propertyClass="" literal=true key=@key.original overload=true }}
1212
{{else}}
13-
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> Schema_.Properties.{{jsonPathPiece.camelCase}}: ...
13+
{{> components/schemas/_helper_getitem_property propertyClass=".Properties" literal=true key=@key.original overload=true }}
1414
{{/if}}
1515
{{else}}
16-
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> schemas.AnyTypeSchema: ...
16+
{{! for when additionalProperties is unset }}
17+
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> schemas.UnsetAnyTypeSchema[typing.Union[
18+
{{> components/schemas/_helper_schema_python_base_types_newline }}
19+
]]: ...
1720
{{/if}}
1821
{{/if}}
1922
{{/with}}
2023
{{/each}}
2124
{{/if}}
2225
{{#if optionalProperties}}
23-
{{#each optionalProperties}}
26+
{{#each optionalProperties}}
2427

2528
@typing.overload
26-
{{#if refInfo.refClass}}
27-
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> '{{> components/schemas/_helper_refclass_with_module }}': ...
28-
{{else}}
29-
def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) -> Schema_.Properties.{{jsonPathPiece.camelCase}}: ...
30-
{{/if}}
31-
{{/each}}
29+
{{#if refInfo.refClass}}
30+
{{> components/schemas/_helper_getitem_refclass literal=true key=@key.original overload=true }}
31+
{{else}}
32+
{{> components/schemas/_helper_getitem_property propertyClass=".Properties" literal=true key=@key.original overload=true }}
33+
{{/if}}
34+
{{/each}}
3235
{{/if}}
3336
{{#or properties requiredProperties}}
3437
{{#with additionalProperties}}
3538
{{#unless isBooleanSchemaFalse}}
3639

3740
@typing.overload
38-
def __getitem__(self, name: str) -> {{#if refInfo.refClass}}'{{> components/schemas/_helper_refclass_with_module }}'{{else}}Schema_.{{jsonPathPiece.camelCase}}{{/if}}: ...
41+
{{#if refInfo.refClass}}
42+
{{> components/schemas/_helper_getitem_refclass literal=false key="str" overload=true }}
43+
{{else}}
44+
{{> components/schemas/_helper_getitem_property propertyClass="" literal=false key="str" overload=true }}
45+
{{/if}}
3946
{{/unless}}
4047
{{else}}
4148

4249
@typing.overload
43-
def __getitem__(self, name: str) -> schemas.UnsetAnyTypeSchema: ...
50+
def __getitem__(self, name: str) -> schemas.UnsetAnyTypeSchema[typing.Union[
51+
frozendict.frozendict,
52+
str,
53+
decimal.Decimal,
54+
schemas.BoolClass,
55+
schemas.NoneClass,
56+
tuple,
57+
bytes,
58+
schemas.FileIO
59+
]]: ...
4460
{{/with}}
4561

46-
{{> components/schemas/_helper_getitem methodName="__getitem__" }}
62+
def __getitem__(
63+
self,
64+
name: typing.Union[
65+
{{#each requiredProperties}}
66+
{{#if this}}
67+
typing_extensions.Literal["{{{@key.original}}}"],
68+
{{/if}}
69+
{{/each}}
70+
{{#each optionalProperties}}
71+
typing_extensions.Literal["{{{@key.original}}}"],
72+
{{/each}}
73+
{{#with additionalProperties}}
74+
{{#unless isBooleanSchemaFalse}}
75+
str
76+
{{/unless}}
77+
{{else}}
78+
str
79+
{{/with}}
80+
]
81+
):
82+
# dict_instance[name] accessor
83+
return super().__getitem__(name)
4784
{{else}}
85+
{{! no properties or requiredProperties }}
4886
{{#with additionalProperties}}
4987
{{#unless isBooleanSchemaFalse}}
5088

51-
def __getitem__(self, name: str) -> {{#if refInfo.refClass}}'{{> components/schemas/_helper_refclass_with_module }}'{{else}}Schema_.{{jsonPathPiece.camelCase}}{{/if}}:
89+
{{#if refInfo.refClass}}
90+
{{> components/schemas/_helper_getitem_refclass literal=false key="str" overload=false }}
91+
{{else}}
92+
{{> components/schemas/_helper_getitem_property propertyClass="" literal=false key="str" overload=false }}
93+
{{/if}}
5294
# dict_instance[name] accessor
5395
return super().__getitem__(name)
5496
{{/unless}}

modules/openapi-json-schema-generator/src/main/resources/python/components/schemas/_helper_new.hbs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,23 @@ def __new__(
9292
{{/contains}}
9393
{{/eq}}
9494
{{/with}}
95-
) -> '{{#each identifierPieces}}{{#if this.camelCase}}{{this.camelCase}}{{else}}{{this}}{{/if}}{{#unless @last}}.{{/unless}}{{/each}}':
95+
{{#if types}}
96+
{{#eq types.size 1}}
97+
) -> {{#each identifierPieces}}{{#if this.camelCase}}{{this.camelCase}}{{else}}{{this}}{{/if}}{{#unless @last}}.{{/unless}}{{/each}}[{{> components/schemas/_helper_schema_python_base_types }}]:
98+
{{else}}
99+
) -> {{#each identifierPieces}}{{#if this.camelCase}}{{this.camelCase}}{{else}}{{this}}{{/if}}{{#unless @last}}.{{/unless}}{{/each}}[
100+
typing.Union[
101+
{{> components/schemas/_helper_schema_python_base_types_newline }}
102+
]
103+
]:
104+
{{/eq}}
105+
{{else}}
106+
) -> {{#each identifierPieces}}{{#if this.camelCase}}{{this.camelCase}}{{else}}{{this}}{{/if}}{{#unless @last}}.{{/unless}}{{/each}}[
107+
typing.Union[
108+
{{> components/schemas/_helper_schema_python_base_types_newline }}
109+
]
110+
]:
111+
{{/if}}
96112
return super().__new__(
97113
cls,
98114
{{#eq types null}}

modules/openapi-json-schema-generator/src/main/resources/python/components/schemas/_helper_property_type_hints.hbs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,57 @@
44
{{#if @key.isValid}}
55
{{#with this}}
66
{{#if refInfo.refClass}}
7-
{{@key.original}}: '{{> components/schemas/_helper_refclass_with_module }}'
7+
{{#with getDeepestRef}}
8+
{{#if types}}
9+
{{#eq types.size 1}}
10+
{{../@key.original}}: {{#if ../refInfo.refModule}}{{../refInfo.refModule}}.{{/if}}{{../refInfo.refClass}}[{{> components/schemas/_helper_schema_python_base_types }}]
11+
{{else}}
12+
{{../@key.original}}: {{#if ../refInfo.refModule}}{{../refInfo.refModule}}.{{/if}}{{../refInfo.refClass}}[typing.Union[
13+
{{> components/schemas/_helper_schema_python_base_types_newline }}
14+
]]
15+
{{/eq}}
16+
{{else}}
17+
{{../@key.original}}: {{#if ../refInfo.refModule}}{{../refInfo.refModule}}.{{/if}}{{../refInfo.refClass}}[typing.Union[
18+
{{> components/schemas/_helper_schema_python_base_types_newline }}
19+
]]
20+
{{/if}}
21+
{{/with}}
822
{{else}}
923
{{#if jsonPathPiece}}
1024
{{#if schemaIsFromAdditionalProperties}}
11-
{{@key.original}}: Schema_.{{jsonPathPiece.camelCase}}
25+
{{#if types}}
26+
{{#eq types.size 1}}
27+
{{@key.original}}: Schema_.{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}]
28+
{{else}}
29+
{{@key.original}}: Schema_.{{jsonPathPiece.camelCase}}[typing.Union[
30+
{{> components/schemas/_helper_schema_python_base_types_newline }}
31+
]]
32+
{{/eq}}
33+
{{else}}
34+
{{@key.original}}: Schema_.{{jsonPathPiece.camelCase}}[typing.Union[
35+
{{> components/schemas/_helper_schema_python_base_types_newline }}
36+
]]
37+
{{/if}}
1238
{{else}}
13-
{{@key.original}}: Schema_.Properties.{{jsonPathPiece.camelCase}}
39+
{{#if types}}
40+
{{#eq types.size 1}}
41+
{{@key.original}}: Schema_.Properties.{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}]
42+
{{else}}
43+
{{@key.original}}: Schema_.Properties.{{jsonPathPiece.camelCase}}[typing.Union[
44+
{{> components/schemas/_helper_schema_python_base_types_newline }}
45+
]]
46+
{{/eq}}
47+
{{else}}
48+
{{@key.original}}: Schema_.Properties.{{jsonPathPiece.camelCase}}[typing.Union[
49+
{{> components/schemas/_helper_schema_python_base_types_newline }}
50+
]]
51+
{{/if}}
1452
{{/if}}
1553
{{else}}
16-
{{@key.original}}: schemas.AnyTypeSchema
54+
{{! for when additionalProperties is unset }}
55+
{{@key.original}}: schemas.UnsetAnyTypeSchema[typing.Union[
56+
{{> components/schemas/_helper_schema_python_base_types_newline }}
57+
]]
1758
{{/if}}
1859
{{/if}}
1960
{{/with}}

0 commit comments

Comments
 (0)