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

Commit

Permalink
Writes nested schemas earlier and uses them later
Browse files Browse the repository at this point in the history
  • Loading branch information
spacether committed May 31, 2023
1 parent e6bf84a commit 0c98150
Show file tree
Hide file tree
Showing 260 changed files with 8,008 additions and 7,369 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -126,6 +127,68 @@ public CodegenSchema getSelfOrDeepestRef() {
return refObject;
}

/**
* Returns all schemas in post order traversal, used by templates to write schema classes
* @param schemas the input list that stores this and all required schemas
* @return the list that stores this and all required schemas
*/
private ArrayList<CodegenSchema> getAllSchemas(ArrayList<CodegenSchema> schemas, int level) {
/*
post order traversal using alphabetic json schema keywords as the order
keywords with schemas:
additionalProperties
allOf
anyOf
items
not
oneOf
properties
excluded:
$ref (because it is an import)
*/
if (additionalProperties != null) {
additionalProperties.getAllSchemas(schemas, level + 1);
}
if (allOf != null) {
for (CodegenSchema someSchema: allOf) {
someSchema.getAllSchemas(schemas, level + 1);
}
}
if (anyOf != null) {
for (CodegenSchema someSchema: anyOf) {
someSchema.getAllSchemas(schemas, level + 1);
}
}
if (items != null) {
items.getAllSchemas(schemas, level + 1);
}
if (not != null) {
not.getAllSchemas(schemas, level + 1);
}
if (oneOf != null) {
for (CodegenSchema someSchema: oneOf) {
someSchema.getAllSchemas(schemas, level + 1);
}
}
if (properties != null) {
for (CodegenSchema someSchema: properties.values()) {
someSchema.getAllSchemas(schemas, level + 1);
}
}
if (refInfo != null && level > 0) {
// do not add ref to schemas
return schemas;
}
schemas.add(this);
return schemas;
}

public ArrayList<CodegenSchema> getSchemas() {
ArrayList<CodegenSchema> schemas = new ArrayList<>();
return getAllSchemas(schemas, 0);
}

public boolean isComplicated() {
// used by templates

Expand Down
Original file line number Diff line number Diff line change
@@ -1,73 +1,54 @@
{{#if allOf}}

class AllOf:
{{#each allOf}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "AllOf" jsonPathPiece) }}
{{/if}}
{{/each}}
classes = [
{{#each allOf}}
{{#if refInfo.refClass}}
{{jsonPathPiece.snakeCase}},
{{else}}
@staticmethod
def all_of():
return (
{{#each allOf}}
{{#if refInfo.refClass}}
{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
{{else}}
{{jsonPathPiece.camelCase}},
{{/if}}
{{/each}}
]
{{/if}}
{{/each}}
)
{{/if}}
{{#if oneOf}}

class OneOf:
{{#each oneOf}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "OneOf" jsonPathPiece) }}
{{/if}}
{{/each}}
classes = [
{{#each oneOf}}
{{#if refInfo.refClass}}
{{jsonPathPiece.snakeCase}},
{{else}}
@staticmethod
def one_of():
return (
{{#each oneOf}}
{{#if refInfo.refClass}}
{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
{{else}}
{{jsonPathPiece.camelCase}},
{{/if}}
{{/each}}
]
{{/if}}
{{/each}}
)
{{/if}}
{{#if anyOf}}

class AnyOf:
{{#each anyOf}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "AnyOf" jsonPathPiece) }}
{{/if}}
{{/each}}
classes = [
{{#each anyOf}}
{{#if refInfo.refClass}}
{{jsonPathPiece.snakeCase}},
{{else}}
@staticmethod
def any_of():
return (
{{#each anyOf}}
{{#if refInfo.refClass}}
{{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
{{else}}
{{jsonPathPiece.camelCase}},
{{/if}}
{{/each}}
]
{{/if}}
{{/each}}
)
{{/if}}
{{#if not}}
{{#with not}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
{{/if}}
@staticmethod
def not_():
{{#if refInfo.refClass}}
return {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}
{{else}}
return {{jsonPathPiece.camelCase}}
{{/if}}
{{/with}}
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,25 @@ def discriminator():
{{/if}}
{{#if properties}}

class Properties:
{{#each properties}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "Properties" jsonPathPiece.camelCase) }}
{{/if}}
{{/each}}
__annotations__ = {
{{#each properties}}
{{#if refInfo.refClass}}
"{{{@key.original}}}": {{jsonPathPiece.snakeCase}},
{{else}}
@staticmethod
def properties():
return {
{{#each properties}}
{{#if refInfo.refClass}}
"{{{@key.original}}}": {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}},
{{else}}
"{{{@key.original}}}": {{jsonPathPiece.camelCase}},
{{/if}}
{{/each}}
{{/if}}
{{/each}}
}
{{/if}}
{{#with additionalProperties}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
{{/if}}
@staticmethod
def additional_properties():
{{#if refInfo.refClass}}
return {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}
{{else}}
return {{jsonPathPiece.camelCase}}
{{/if}}
{{/with}}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{#if types}}
{{#eq types.size 1}}
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}}
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}}
{{else}}
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[typing.Union[
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{jsonPathPiece.camelCase}}[typing.Union[
{{> components/schemas/_helper_schema_python_base_types_newline }}
]]:{{#if overload}} ...{{/if}}
{{/eq}}
{{else}}
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> Schema_{{propertyClass}}.{{jsonPathPiece.camelCase}}[typing.Union[
def __getitem__(self, name: {{#if literal}}typing_extensions.Literal["{{{key}}}"]{{else}}{{key}}{{/if}}) -> {{jsonPathPiece.camelCase}}[typing.Union[
{{> components/schemas/_helper_schema_python_base_types_newline }}
]]:{{#if overload}} ...{{/if}}
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
{{else}}
{{#if jsonPathPiece}}
{{#if schemaIsFromAdditionalProperties}}
{{> components/schemas/_helper_getitem_property propertyClass="" literal=true key=@key.original overload=true }}
{{> components/schemas/_helper_getitem_property literal=true key=@key.original overload=true }}
{{else}}
{{> components/schemas/_helper_getitem_property propertyClass=".Properties" literal=true key=@key.original overload=true }}
{{> components/schemas/_helper_getitem_property literal=true key=@key.original overload=true }}
{{/if}}
{{else}}
{{! for when additionalProperties is unset, use schemas.AnyTypeSchema because val is not always schemas.UnsetAnyTypeSchema }}
Expand All @@ -29,7 +29,7 @@ def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) ->
{{#if refInfo.refClass}}
{{> components/schemas/_helper_getitem_refclass literal=true key=@key.original overload=true }}
{{else}}
{{> components/schemas/_helper_getitem_property propertyClass=".Properties" literal=true key=@key.original overload=true }}
{{> components/schemas/_helper_getitem_property literal=true key=@key.original overload=true }}
{{/if}}
{{/each}}
{{/if}}
Expand All @@ -41,7 +41,7 @@ def __getitem__(self, name: typing_extensions.Literal["{{{@key.original}}}"]) ->
{{#if refInfo.refClass}}
{{> components/schemas/_helper_getitem_refclass literal=false key="str" overload=true }}
{{else}}
{{> components/schemas/_helper_getitem_property propertyClass="" literal=false key="str" overload=true }}
{{> components/schemas/_helper_getitem_property literal=false key="str" overload=true }}
{{/if}}
{{/unless}}
{{else}}
Expand Down Expand Up @@ -90,7 +90,7 @@ def __getitem__(
{{#if refInfo.refClass}}
{{> components/schemas/_helper_getitem_refclass literal=false key="str" overload=false }}
{{else}}
{{> components/schemas/_helper_getitem_property propertyClass="" literal=false key="str" overload=false }}
{{> components/schemas/_helper_getitem_property literal=false key="str" overload=false }}
{{/if}}
# dict_instance[name] accessor
return super().__getitem__(name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{{#with items}}
{{#if refInfo.refClass}}

{{> components/schemas/_helper_refclass_staticmethod }}
{{else}}
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
{{/if}}
@staticmethod
def items():
{{#if refInfo.refClass}}
return {{#if refInfo.refModule}}{{refInfo.refModule}}.{{/if}}{{refInfo.refClass}}
{{else}}
return {{jsonPathPiece.camelCase}}
{{/if}}
{{/with}}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __new__(
]
{{else}}
typing.Union[
{{> components/schemas/_helper_new_property_value_type propertyClass=null optional=false }}
{{> components/schemas/_helper_new_property_value_type optional=false }}
]
{{/if}}
{{/with}}
Expand Down Expand Up @@ -65,11 +65,11 @@ def __new__(
{{#if jsonPathPiece}}
{{#if schemaIsFromAdditionalProperties}}
{{@key.original}}: typing.Union[
{{> components/schemas/_helper_new_property_value_type propertyClass=null optional=false }}
{{> components/schemas/_helper_new_property_value_type optional=false }}
],
{{else}}
{{@key.original}}: typing.Union[
{{> components/schemas/_helper_new_property_value_type propertyClass="Properties" optional=false }}
{{> components/schemas/_helper_new_property_value_type optional=false }}
],
{{/if}}
{{else}}
Expand All @@ -95,7 +95,7 @@ def __new__(
] = schemas.unset,
{{else}}
{{@key.original}}: typing.Union[
{{> components/schemas/_helper_new_property_value_type propertyClass="Properties" optional=true }}
{{> components/schemas/_helper_new_property_value_type optional=true }}
] = schemas.unset,
{{/if}}
{{/if}}
Expand All @@ -109,7 +109,7 @@ def __new__(
],
{{else}}
**kwargs: typing.Union[
{{> components/schemas/_helper_new_property_value_type propertyClass=null optional=false }}
{{> components/schemas/_helper_new_property_value_type optional=false }}
],
{{/if}}
{{/unless}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{{#if types}}
{{#eq types.size 1}}
Schema_.{{#if propertyClass}}{{propertyClass}}.{{/if}}{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}],
{{jsonPathPiece.camelCase}}[{{> components/schemas/_helper_schema_python_base_types }}],
{{else}}
Schema_.{{#if propertyClass}}{{propertyClass}}.{{/if}}{{jsonPathPiece.camelCase}}[typing.Union[
{{jsonPathPiece.camelCase}}[typing.Union[
{{> components/schemas/_helper_schema_python_base_types_newline }}
]],
{{/eq}}
{{else}}
Schema_.{{#if propertyClass}}{{propertyClass}}.{{/if}}{{jsonPathPiece.camelCase}}[typing.Union[
{{jsonPathPiece.camelCase}}[typing.Union[
{{> components/schemas/_helper_schema_python_base_types_newline }}
]],
{{/if}}
Expand Down
Loading

0 comments on commit 0c98150

Please sign in to comment.