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

Commit 0c98150

Browse files
committed
Writes nested schemas earlier and uses them later
1 parent e6bf84a commit 0c98150

260 files changed

Lines changed: 8008 additions & 7369 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/java/org/openapijsonschematools/codegen/model/CodegenSchema.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.swagger.v3.oas.models.ExternalDocumentation;
2121

22+
import java.util.ArrayList;
2223
import java.util.HashMap;
2324
import java.util.LinkedHashMap;
2425
import java.util.LinkedHashSet;
@@ -126,6 +127,68 @@ public CodegenSchema getSelfOrDeepestRef() {
126127
return refObject;
127128
}
128129

130+
/**
131+
* Returns all schemas in post order traversal, used by templates to write schema classes
132+
* @param schemas the input list that stores this and all required schemas
133+
* @return the list that stores this and all required schemas
134+
*/
135+
private ArrayList<CodegenSchema> getAllSchemas(ArrayList<CodegenSchema> schemas, int level) {
136+
/*
137+
post order traversal using alphabetic json schema keywords as the order
138+
keywords with schemas:
139+
additionalProperties
140+
allOf
141+
anyOf
142+
items
143+
not
144+
oneOf
145+
properties
146+
147+
excluded:
148+
$ref (because it is an import)
149+
*/
150+
if (additionalProperties != null) {
151+
additionalProperties.getAllSchemas(schemas, level + 1);
152+
}
153+
if (allOf != null) {
154+
for (CodegenSchema someSchema: allOf) {
155+
someSchema.getAllSchemas(schemas, level + 1);
156+
}
157+
}
158+
if (anyOf != null) {
159+
for (CodegenSchema someSchema: anyOf) {
160+
someSchema.getAllSchemas(schemas, level + 1);
161+
}
162+
}
163+
if (items != null) {
164+
items.getAllSchemas(schemas, level + 1);
165+
}
166+
if (not != null) {
167+
not.getAllSchemas(schemas, level + 1);
168+
}
169+
if (oneOf != null) {
170+
for (CodegenSchema someSchema: oneOf) {
171+
someSchema.getAllSchemas(schemas, level + 1);
172+
}
173+
}
174+
if (properties != null) {
175+
for (CodegenSchema someSchema: properties.values()) {
176+
someSchema.getAllSchemas(schemas, level + 1);
177+
}
178+
}
179+
if (refInfo != null && level > 0) {
180+
// do not add ref to schemas
181+
return schemas;
182+
}
183+
schemas.add(this);
184+
return schemas;
185+
}
186+
187+
public ArrayList<CodegenSchema> getSchemas() {
188+
ArrayList<CodegenSchema> schemas = new ArrayList<>();
189+
return getAllSchemas(schemas, 0);
190+
}
191+
129192
public boolean isComplicated() {
130193
// used by templates
131194

Lines changed: 37 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,54 @@
11
{{#if allOf}}
22

3-
class AllOf:
4-
{{#each allOf}}
5-
{{#if refInfo.refClass}}
6-
7-
{{> components/schemas/_helper_refclass_staticmethod }}
8-
{{else}}
9-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "AllOf" jsonPathPiece) }}
10-
{{/if}}
11-
{{/each}}
12-
classes = [
13-
{{#each allOf}}
14-
{{#if refInfo.refClass}}
15-
{{jsonPathPiece.snakeCase}},
16-
{{else}}
3+
@staticmethod
4+
def all_of():
5+
return (
6+
{{#each allOf}}
7+
{{#if refInfo.refClass}}
8+
{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
9+
{{else}}
1710
{{jsonPathPiece.camelCase}},
18-
{{/if}}
19-
{{/each}}
20-
]
11+
{{/if}}
12+
{{/each}}
13+
)
2114
{{/if}}
2215
{{#if oneOf}}
2316

24-
class OneOf:
25-
{{#each oneOf}}
26-
{{#if refInfo.refClass}}
27-
28-
{{> components/schemas/_helper_refclass_staticmethod }}
29-
{{else}}
30-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "OneOf" jsonPathPiece) }}
31-
{{/if}}
32-
{{/each}}
33-
classes = [
34-
{{#each oneOf}}
35-
{{#if refInfo.refClass}}
36-
{{jsonPathPiece.snakeCase}},
37-
{{else}}
17+
@staticmethod
18+
def one_of():
19+
return (
20+
{{#each oneOf}}
21+
{{#if refInfo.refClass}}
22+
{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
23+
{{else}}
3824
{{jsonPathPiece.camelCase}},
39-
{{/if}}
40-
{{/each}}
41-
]
25+
{{/if}}
26+
{{/each}}
27+
)
4228
{{/if}}
4329
{{#if anyOf}}
4430

45-
class AnyOf:
46-
{{#each anyOf}}
47-
{{#if refInfo.refClass}}
48-
49-
{{> components/schemas/_helper_refclass_staticmethod }}
50-
{{else}}
51-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "AnyOf" jsonPathPiece) }}
52-
{{/if}}
53-
{{/each}}
54-
classes = [
55-
{{#each anyOf}}
56-
{{#if refInfo.refClass}}
57-
{{jsonPathPiece.snakeCase}},
58-
{{else}}
31+
@staticmethod
32+
def any_of():
33+
return (
34+
{{#each anyOf}}
35+
{{#if refInfo.refClass}}
36+
{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
37+
{{else}}
5938
{{jsonPathPiece.camelCase}},
60-
{{/if}}
61-
{{/each}}
62-
]
39+
{{/if}}
40+
{{/each}}
41+
)
6342
{{/if}}
6443
{{#if not}}
6544
{{#with not}}
66-
{{#if refInfo.refClass}}
6745

68-
{{> components/schemas/_helper_refclass_staticmethod }}
69-
{{else}}
70-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
71-
{{/if}}
46+
@staticmethod
47+
def not_():
48+
{{#if refInfo.refClass}}
49+
return {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}
50+
{{else}}
51+
return {{jsonPathPiece.camelCase}}
52+
{{/if}}
7253
{{/with}}
7354
{{/if}}

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

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,25 @@ def discriminator():
2525
{{/if}}
2626
{{#if properties}}
2727

28-
class Properties:
29-
{{#each properties}}
30-
{{#if refInfo.refClass}}
31-
32-
{{> components/schemas/_helper_refclass_staticmethod }}
33-
{{else}}
34-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "Properties" jsonPathPiece.camelCase) }}
35-
{{/if}}
36-
{{/each}}
37-
__annotations__ = {
38-
{{#each properties}}
39-
{{#if refInfo.refClass}}
40-
"{{{@key.original}}}": {{jsonPathPiece.snakeCase}},
41-
{{else}}
28+
@staticmethod
29+
def properties():
30+
return {
31+
{{#each properties}}
32+
{{#if refInfo.refClass}}
33+
"{{{@key.original}}}": {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
34+
{{else}}
4235
"{{{@key.original}}}": {{jsonPathPiece.camelCase}},
43-
{{/if}}
44-
{{/each}}
36+
{{/if}}
37+
{{/each}}
4538
}
4639
{{/if}}
4740
{{#with additionalProperties}}
48-
{{#if refInfo.refClass}}
4941

50-
{{> components/schemas/_helper_refclass_staticmethod }}
51-
{{else}}
52-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
53-
{{/if}}
42+
@staticmethod
43+
def additional_properties():
44+
{{#if refInfo.refClass}}
45+
return {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}
46+
{{else}}
47+
return {{jsonPathPiece.camelCase}}
48+
{{/if}}
5449
{{/with}}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{{#if types}}
22
{{#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}}
3+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}]:{{#if overload}} ...{{/if}}
44
{{else}}
5-
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[typing.Union[
5+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{jsonPathPiece.camelCase}}[typing.Union[
66
{{> components/schemas/_helper_schema_python_base_types_newline }}
77
]]:{{#if overload}} ...{{/if}}
88
{{/eq}}
99
{{else}}
10-
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[typing.Union[
10+
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{jsonPathPiece.camelCase}}[typing.Union[
1111
{{> components/schemas/_helper_schema_python_base_types_newline }}
1212
]]:{{#if overload}} ...{{/if}}
1313
{{/if}}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
{{else}}
99
{{#if jsonPathPiece}}
1010
{{#if schemaIsFromAdditionalProperties}}
11-
{{> components/schemas/_helper_getitem_property propertyClass="" literal=true key=@key.original overload=true }}
11+
{{> components/schemas/_helper_getitem_property literal=true key=@key.original overload=true }}
1212
{{else}}
13-
{{> components/schemas/_helper_getitem_property propertyClass=".Properties" literal=true key=@key.original overload=true }}
13+
{{> components/schemas/_helper_getitem_property literal=true key=@key.original overload=true }}
1414
{{/if}}
1515
{{else}}
1616
{{! for when additionalProperties is unset, use schemas.AnyTypeSchema because val is not always schemas.UnsetAnyTypeSchema }}
@@ -29,7 +29,7 @@ def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) ->
2929
{{#if refInfo.refClass}}
3030
{{> components/schemas/_helper_getitem_refclass literal=true key=@key.original overload=true }}
3131
{{else}}
32-
{{> components/schemas/_helper_getitem_property propertyClass=".Properties" literal=true key=@key.original overload=true }}
32+
{{> components/schemas/_helper_getitem_property literal=true key=@key.original overload=true }}
3333
{{/if}}
3434
{{/each}}
3535
{{/if}}
@@ -41,7 +41,7 @@ def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) ->
4141
{{#if refInfo.refClass}}
4242
{{> components/schemas/_helper_getitem_refclass literal=false key="str" overload=true }}
4343
{{else}}
44-
{{> components/schemas/_helper_getitem_property propertyClass="" literal=false key="str" overload=true }}
44+
{{> components/schemas/_helper_getitem_property literal=false key="str" overload=true }}
4545
{{/if}}
4646
{{/unless}}
4747
{{else}}
@@ -90,7 +90,7 @@ def __getitem__(
9090
{{#if refInfo.refClass}}
9191
{{> components/schemas/_helper_getitem_refclass literal=false key="str" overload=false }}
9292
{{else}}
93-
{{> components/schemas/_helper_getitem_property propertyClass="" literal=false key="str" overload=false }}
93+
{{> components/schemas/_helper_getitem_property literal=false key="str" overload=false }}
9494
{{/if}}
9595
# dict_instance[name] accessor
9696
return super().__getitem__(name)
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{{#with items}}
2-
{{#if refInfo.refClass}}
32

4-
{{> components/schemas/_helper_refclass_staticmethod }}
5-
{{else}}
6-
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
7-
{{/if}}
3+
@staticmethod
4+
def items():
5+
{{#if refInfo.refClass}}
6+
return {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}
7+
{{else}}
8+
return {{jsonPathPiece.camelCase}}
9+
{{/if}}
810
{{/with}}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __new__(
1111
]
1212
{{else}}
1313
typing.Union[
14-
{{> components/schemas/_helper_new_property_value_type propertyClass=null optional=false }}
14+
{{> components/schemas/_helper_new_property_value_type optional=false }}
1515
]
1616
{{/if}}
1717
{{/with}}
@@ -65,11 +65,11 @@ def __new__(
6565
{{#if jsonPathPiece}}
6666
{{#if schemaIsFromAdditionalProperties}}
6767
{{@key.original}}: typing.Union[
68-
{{> components/schemas/_helper_new_property_value_type propertyClass=null optional=false }}
68+
{{> components/schemas/_helper_new_property_value_type optional=false }}
6969
],
7070
{{else}}
7171
{{@key.original}}: typing.Union[
72-
{{> components/schemas/_helper_new_property_value_type propertyClass="Properties" optional=false }}
72+
{{> components/schemas/_helper_new_property_value_type optional=false }}
7373
],
7474
{{/if}}
7575
{{else}}
@@ -95,7 +95,7 @@ def __new__(
9595
] = schemas.unset,
9696
{{else}}
9797
{{@key.original}}: typing.Union[
98-
{{> components/schemas/_helper_new_property_value_type propertyClass="Properties" optional=true }}
98+
{{> components/schemas/_helper_new_property_value_type optional=true }}
9999
] = schemas.unset,
100100
{{/if}}
101101
{{/if}}
@@ -109,7 +109,7 @@ def __new__(
109109
],
110110
{{else}}
111111
**kwargs: typing.Union[
112-
{{> components/schemas/_helper_new_property_value_type propertyClass=null optional=false }}
112+
{{> components/schemas/_helper_new_property_value_type optional=false }}
113113
],
114114
{{/if}}
115115
{{/unless}}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{{#if types}}
22
{{#eq types.size 1}}
3-
Schema_.{{#if propertyClass}}{{propertyClass}}.{{/if}}{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}],
3+
{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}],
44
{{else}}
5-
Schema_.{{#if propertyClass}}{{propertyClass}}.{{/if}}{{jsonPathPiece.camelCase}}[typing.Union[
5+
{{jsonPathPiece.camelCase}}[typing.Union[
66
{{> components/schemas/_helper_schema_python_base_types_newline }}
77
]],
88
{{/eq}}
99
{{else}}
10-
Schema_.{{#if propertyClass}}{{propertyClass}}.{{/if}}{{jsonPathPiece.camelCase}}[typing.Union[
10+
{{jsonPathPiece.camelCase}}[typing.Union[
1111
{{> components/schemas/_helper_schema_python_base_types_newline }}
1212
]],
1313
{{/if}}

0 commit comments

Comments
 (0)