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

Commit 2913d07

Browse files
authored
v2 improves schema __new__ return type (#147)
* Templates updated * Regnerates samples * Adds missing Schema_ additions in identifierPieces * Samples regen * Adds missing composed schema class names in new signature
1 parent d678771 commit 2913d07

File tree

114 files changed

+234
-241
lines changed

Some content is hidden

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

114 files changed

+234
-241
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ private void generateSchema(List<File> files, CodegenSchema schema, String jsonP
384384
Map<String, Object> schemaData = new HashMap<>();
385385
schemaData.put("packageName", config.packageName());
386386
schemaData.put("schema", schema);
387+
schemaData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
387388
schemaData.putAll(config.additionalProperties());
388389
generateXs(files, jsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.SCHEMA, CodegenConstants.MODELS, schemaData, generateModels);
389390
}
@@ -475,7 +476,6 @@ private void generatePathItem(List<File> files, CodegenKey pathKey, CodegenPathI
475476
endpointInfo.put("headerSize", "#");
476477
endpointInfo.put("complexTypePrefix", "../../components/schema/");
477478
endpointInfo.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
478-
endpointInfo.put("identifierToHeadingQty", new HashMap<>());
479479
generateXDocs(files, operationJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.OPERATION, CodegenConstants.APIS, endpointInfo, true);
480480

481481
// paths.some_path.security.security_requirement_0.py
@@ -671,7 +671,6 @@ private TreeMap<String, CodegenResponse> generateResponses(List<File> files) {
671671
templateData.put("complexTypePrefix", "../../components/schema/");
672672
templateData.put("headerSize", "#");
673673
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
674-
templateData.put("identifierToHeadingQty", new HashMap<>());
675674
templateData.put("response", response);
676675
// TODO make this a property that can be turned off and on
677676
generateXDocs(files, sourceJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.RESPONSE, CodegenConstants.RESPONSE_DOCS, templateData, true);
@@ -723,7 +722,6 @@ private TreeMap<String, CodegenSecurityScheme> generateSecuritySchemes(List<File
723722
templateData.put("securityScheme", securityScheme);
724723
templateData.put("headerSize", "#");
725724
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
726-
templateData.put("identifierToHeadingQty", new HashMap<>());
727725
templateData.put("complexTypePrefix", "../../components/schema/");
728726
// TODO add a flag to turn this off
729727
generateXDocs(files, sourceJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.SECURITY_SCHEME, CodegenConstants.SECURITY_SCHEME_DOCS, templateData, true);
@@ -757,7 +755,6 @@ private TreeMap<String, CodegenRequestBody> generateRequestBodies(List<File> fil
757755
templateData.put("requestBody", requestBody);
758756
templateData.put("headerSize", "#");
759757
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
760-
templateData.put("identifierToHeadingQty", new HashMap<>());
761758
templateData.put("complexTypePrefix", "../../components/schema/");
762759
// todo add flag to turn this off
763760
generateXDocs(files, sourceJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.REQUEST_BODY, CodegenConstants.REQUEST_BODY_DOCS, templateData, true);
@@ -807,7 +804,6 @@ private TreeMap<String, CodegenParameter> generateParameters(List<File> files) {
807804
templateData.put("parameter", parameter);
808805
templateData.put("headerSize", "#");
809806
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
810-
templateData.put("identifierToHeadingQty", new HashMap<>());
811807
templateData.put("complexTypePrefix", "../../components/schema/");
812808
// todo add flag to turn this off
813809
generateXDocs(files, parameterJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.PARAMETER, CodegenConstants.PARAMETER_DOCS, templateData, true);
@@ -921,7 +917,6 @@ private TreeMap<String, CodegenHeader> generateHeaders(List<File> files) {
921917
templateData.put("complexTypePrefix", "../../components/schema/");
922918
templateData.put("docRoot", "../../");
923919
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
924-
templateData.put("identifierToHeadingQty", new HashMap<>());
925920
// TODO add flag to turn this off
926921
generateXDocs(files, sourceJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.HEADER, CodegenConstants.HEADER_DOCS, templateData, true);
927922
}
@@ -999,7 +994,6 @@ protected TreeMap<String, CodegenSchema> generateSchemas(List<File> files) {
999994
// to generate model documentation files
1000995
if (generateModelDocumentation) {
1001996
schemaData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
1002-
schemaData.put("identifierToHeadingQty", new HashMap<>());
1003997
generateSchemaDocumentation(files, schemaData, jsonPath);
1004998
}
1005999

@@ -1338,7 +1332,6 @@ private void generateServers(List<File> files, List<CodegenServer> servers, Stri
13381332
if (server.rootServer) {
13391333
templateData.put("headerSize", "#");
13401334
templateData.put("identifierPieces", Collections.unmodifiableList(new ArrayList<>()));
1341-
templateData.put("identifierToHeadingQty", new HashMap<>());
13421335
generateXDocs(files, serverJsonPath, CodegenConstants.JSON_PATH_LOCATION_TYPE.SERVER, CodegenConstants.SERVERS, templateData, true);
13431336
}
13441337
i++;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class AllOf:
66

77
{{> components/schemas/_helper_refclass_staticmethod }}
88
{{else}}
9-
{{> components/schemas/_helper_schema_switch_case }}
9+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "AllOf" jsonPathPiece) }}
1010
{{/if}}
1111
{{/each}}
1212
classes = [
@@ -27,7 +27,7 @@ class OneOf:
2727

2828
{{> components/schemas/_helper_refclass_staticmethod }}
2929
{{else}}
30-
{{> components/schemas/_helper_schema_switch_case }}
30+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "OneOf" jsonPathPiece) }}
3131
{{/if}}
3232
{{/each}}
3333
classes = [
@@ -48,7 +48,7 @@ class AnyOf:
4848

4949
{{> components/schemas/_helper_refclass_staticmethod }}
5050
{{else}}
51-
{{> components/schemas/_helper_schema_switch_case }}
51+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "AnyOf" jsonPathPiece) }}
5252
{{/if}}
5353
{{/each}}
5454
classes = [
@@ -67,7 +67,7 @@ class AnyOf:
6767

6868
{{> components/schemas/_helper_refclass_staticmethod }}
6969
{{else}}
70-
{{> components/schemas/_helper_schema_switch_case }}
70+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
7171
{{/if}}
7272
{{/with}}
7373
{{/if}}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Properties:
3131

3232
{{> components/schemas/_helper_refclass_staticmethod }}
3333
{{else}}
34-
{{> components/schemas/_helper_schema_switch_case }}
34+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "Properties" jsonPathPiece.camelCase) }}
3535
{{/if}}
3636
{{/each}}
3737
__annotations__ = {
@@ -49,6 +49,6 @@ class Properties:
4949

5050
{{> components/schemas/_helper_refclass_staticmethod }}
5151
{{else}}
52-
{{> components/schemas/_helper_schema_switch_case }}
52+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
5353
{{/if}}
5454
{{/with}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44
{{> components/schemas/_helper_refclass_staticmethod }}
55
{{else}}
6-
{{> components/schemas/_helper_schema_switch_case }}
6+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
77
{{/if}}
88
{{/with}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __new__(
9292
{{/contains}}
9393
{{/eq}}
9494
{{/with}}
95-
) -> '{{jsonPathPiece.camelCase}}':
95+
) -> '{{#each identifierPieces}}{{#if this.camelCase}}{{this.camelCase}}{{else}}{{this}}{{/if}}{{#unless @last}}.{{/unless}}{{/each}}':
9696
return super().__new__(
9797
cls,
9898
{{#eq types null}}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ class {{jsonPathPiece.camelCase}}(
5050
{{> components/schemas/_helper_schema_enum }}
5151
{{/if}}
5252
{{#if items}}
53-
{{> components/schemas/_helper_list_partial }}
53+
{{> components/schemas/_helper_list_partial identifierPieces=(append identifierPieces "Schema_") }}
5454
{{/if}}
5555
{{#or additionalProperties requiredProperties hasDiscriminatorWithNonEmptyMapping properties}}
56-
{{> components/schemas/_helper_dict_partial }}
56+
{{> components/schemas/_helper_dict_partial identifierPieces=(append identifierPieces "Schema_") }}
5757
{{/or}}
5858
{{#unless isStub}}
5959
{{#if hasValidation}}
6060
{{> components/schemas/_helper_validations }}
6161
{{/if}}
6262
{{/unless}}
6363
{{#or allOf anyOf oneOf not}}
64-
{{> components/schemas/_helper_composed_schemas }}
64+
{{> components/schemas/_helper_composed_schemas identifierPieces=(append identifierPieces "Schema_") }}
6565
{{/or}}
6666
{{#if enumValueToName}}
6767
{{> components/schemas/_helper_enums }}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class {{jsonPathPiece.camelCase}}(
1919

2020

2121
class Schema_:
22-
{{> components/schemas/_helper_dict_partial }}
22+
{{> components/schemas/_helper_dict_partial identifierPieces=(append identifierPieces "Schema_") }}
2323
{{/or}}
2424
{{else}}
2525
{{#or additionalProperties requiredProperties hasDiscriminatorWithNonEmptyMapping properties hasValidation}}
2626

2727

2828
class Schema_:
2929
types = {frozendict.frozendict}
30-
{{> components/schemas/_helper_dict_partial }}
30+
{{> components/schemas/_helper_dict_partial identifierPieces=(append identifierPieces "Schema_") }}
3131
{{> components/schemas/_helper_validations }}
3232
{{/or}}
3333
{{/if}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class {{jsonPathPiece.camelCase}}(
2222
{{#if hasValidation}}
2323
{{> components/schemas/_helper_validations }}
2424
{{/if}}
25-
{{> components/schemas/_helper_list_partial }}
25+
{{> components/schemas/_helper_list_partial identifierPieces=(append identifierPieces "Schema_") }}
2626
{{/or}}
2727

2828
{{> components/schemas/_helper_new }}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
{{/if}}
1010
{{> _helper_imports }}
11-
{{> components/schemas/_helper_schema_switch_case }}
11+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
1212
{{else}}
1313
{{> _helper_imports_schema_types }}
14-
{{> components/schemas/_helper_schema_switch_case }}
14+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces jsonPathPiece) }}
1515
{{#if imports}}
1616

1717
{{/if}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from {{packageName}} import server{{#if variables}}, schemas{{/if}}
1414

1515
class _Variables:
1616
{{#each variables}}
17-
{{> components/schemas/_helper_schema_switch_case }}
17+
{{> components/schemas/_helper_schema_switch_case identifierPieces=(append identifierPieces "_Variables" jsonPathPiece.camelCase) }}
1818
{{/each}}
1919

2020
_VariablesSchemas = typing_extensions.TypedDict(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def __new__(
9292
foo: typing.Union[Schema_.Properties.Foo, dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, schemas.Unset] = schemas.unset,
9393
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
9494
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
95-
) -> '_0':
95+
) -> 'AdditionalpropertiesShouldNotLookInApplicators.Schema_.AllOf._0':
9696
return super().__new__(
9797
cls,
9898
*args_,

samples/openapi3/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/additionalproperties_should_not_look_in_applicators.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class AdditionalpropertiesShouldNotLookInApplicators(
9292
foo: typing.Union[Schema_.Properties.Foo, dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader, schemas.Unset] = schemas.unset,
9393
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
9494
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
95-
) -> '_0':
95+
) -> 'AdditionalpropertiesShouldNotLookInApplicators.Schema_.AllOf._0':
9696
return super().__new__(
9797
cls,
9898
*args_,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __new__(
9595
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
9696
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
9797
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
98-
) -> '_0':
98+
) -> 'Allof.Schema_.AllOf._0':
9999
return super().__new__(
100100
cls,
101101
*args_,
@@ -160,7 +160,7 @@ def __new__(
160160
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
161161
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
162162
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
163-
) -> '_1':
163+
) -> 'Allof.Schema_.AllOf._1':
164164
return super().__new__(
165165
cls,
166166
*args_,

samples/openapi3/client/3_0_3_unit_test/python/src/unit_test_api/components/schema/allof.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Allof(
9595
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
9696
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
9797
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
98-
) -> '_0':
98+
) -> 'Allof.Schema_.AllOf._0':
9999
return super().__new__(
100100
cls,
101101
*args_,
@@ -160,7 +160,7 @@ class Allof(
160160
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
161161
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
162162
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
163-
) -> '_1':
163+
) -> 'Allof.Schema_.AllOf._1':
164164
return super().__new__(
165165
cls,
166166
*args_,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __new__(
5454
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
5555
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
5656
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
57-
) -> '_0':
57+
) -> 'AllofCombinedWithAnyofOneof.Schema_.AllOf._0':
5858
return super().__new__(
5959
cls,
6060
*args_,
@@ -83,7 +83,7 @@ def __new__(
8383
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
8484
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
8585
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
86-
) -> '_0':
86+
) -> 'AllofCombinedWithAnyofOneof.Schema_.OneOf._0':
8787
return super().__new__(
8888
cls,
8989
*args_,
@@ -112,7 +112,7 @@ def __new__(
112112
*args_: typing.Union[dict, frozendict.frozendict, str, datetime.date, datetime.datetime, uuid.UUID, int, float, decimal.Decimal, bool, None, list, tuple, bytes, io.FileIO, io.BufferedReader],
113113
configuration_: typing.Optional[schemas.schema_configuration.SchemaConfiguration] = None,
114114
**kwargs: typing.Union[dict, frozendict.frozendict, list, tuple, decimal.Decimal, float, int, str, datetime.date, datetime.datetime, uuid.UUID, bool, None, bytes, io.FileIO, io.BufferedReader, schemas.Schema],
115-
) -> '_0':
115+
) -> 'AllofCombinedWithAnyofOneof.Schema_.AnyOf._0':
116116
return super().__new__(
117117
cls,
118118
*args_,

0 commit comments

Comments
 (0)