Skip to content

Commit c456de4

Browse files
authored
Adds ability to turn inline model resolver on or off and uses it in python-experimental (OpenAPITools#12198)
* Adds getUseInlineModelResolver and uses it * Regenerates python-exp samples * Regenerates docs * Samples regenerated * Moves codegenProperty.complexType setting * Fixes python-experimental tests * Reverts vesion file change * Improves type setting code for python-exp * Fixes AnyType type hint * Samples regenerated
1 parent 20c37b5 commit c456de4

File tree

224 files changed

+684
-2038
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

+684
-2038
lines changed

docs/generators/python-experimental.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2727
|packageVersion|python package version.| |1.0.0|
2828
|projectName|python project name in setup.py (e.g. petstore-api).| |null|
2929
|recursionLimit|Set the recursion limit. If not set, use the system default value.| |null|
30+
|useInlineModelResolver|use the inline model resolver, if true inline complex models will be extracted into components and $refs to them will be used| |false|
3031
|useNose|use the nose test framework| |false|
3132

3233
## IMPORT MAPPING

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,6 @@ public interface CodegenConfig {
319319
String generatorLanguageVersion();
320320

321321
List<VendorExtension> getSupportedVendorExtensions();
322+
323+
boolean getUseInlineModelResolver();
322324
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,12 +4713,12 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports)
47134713
String parameterDataType = this.getParameterDataType(parameter, parameterSchema);
47144714
if (parameterDataType != null) {
47154715
codegenParameter.dataType = parameterDataType;
4716+
if (ModelUtils.isObjectSchema(parameterSchema)) {
4717+
codegenProperty.complexType = codegenParameter.dataType;
4718+
}
47164719
} else {
47174720
codegenParameter.dataType = codegenProperty.dataType;
47184721
}
4719-
if (ModelUtils.isObjectSchema(parameterSchema)) {
4720-
codegenProperty.complexType = codegenParameter.dataType;
4721-
}
47224722
if (ModelUtils.isSet(parameterSchema)) {
47234723
imports.add(codegenProperty.baseType);
47244724
}
@@ -7429,4 +7429,7 @@ public String generatorLanguageVersion() {
74297429
public List<VendorExtension> getSupportedVendorExtensions() {
74307430
return new ArrayList<>();
74317431
}
7432+
7433+
@Override
7434+
public boolean getUseInlineModelResolver() { return true; }
74327435
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,10 @@ public List<File> generate() {
872872
}
873873

874874
// resolve inline models
875-
InlineModelResolver inlineModelResolver = new InlineModelResolver();
876-
inlineModelResolver.flatten(openAPI);
875+
if (config.getUseInlineModelResolver()) {
876+
InlineModelResolver inlineModelResolver = new InlineModelResolver();
877+
inlineModelResolver.flatten(openAPI);
878+
}
877879

878880
configureGeneratorProperties();
879881
configureOpenAPIInfo();

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonExperimentalClientCodegen.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ public class PythonExperimentalClientCodegen extends AbstractPythonCodegen {
7070
// nose is a python testing framework, we use pytest if USE_NOSE is unset
7171
public static final String USE_NOSE = "useNose";
7272
public static final String RECURSION_LIMIT = "recursionLimit";
73+
public static final String USE_INLINE_MODEL_RESOLVER = "useInlineModelResolver";
7374

7475
protected String packageUrl;
7576
protected String apiDocPath = "docs/";
7677
protected String modelDocPath = "docs/";
77-
protected boolean useNose = Boolean.FALSE;
78+
protected boolean useNose = false;
79+
protected boolean useInlineModelResolver = false;
7880

7981
protected Map<Character, String> regexModifiers;
8082

@@ -192,6 +194,8 @@ public PythonExperimentalClientCodegen() {
192194
cliOptions.add(CliOption.newBoolean(USE_NOSE, "use the nose test framework").
193195
defaultValue(Boolean.FALSE.toString()));
194196
cliOptions.add(new CliOption(RECURSION_LIMIT, "Set the recursion limit. If not set, use the system default value."));
197+
cliOptions.add(CliOption.newBoolean(USE_INLINE_MODEL_RESOLVER, "use the inline model resolver, if true inline complex models will be extracted into components and $refs to them will be used").
198+
defaultValue(Boolean.FALSE.toString()));
195199

196200
supportedLibraries.put("urllib3", "urllib3-based client");
197201
CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use: urllib3");
@@ -260,7 +264,7 @@ public void processOpts() {
260264
}
261265

262266
modelTemplateFiles.put("model." + templateExtension, ".py");
263-
apiTemplateFiles.put("api." + templateExtension, ".py");
267+
apiTemplateFiles.put("api." + templateExtension, ".py");
264268
modelTestTemplateFiles.put("model_test." + templateExtension, ".py");
265269
apiTestTemplateFiles.put("api_test." + templateExtension, ".py");
266270
modelDocTemplateFiles.put("model_doc." + templateExtension, ".md");
@@ -321,6 +325,10 @@ public void processOpts() {
321325
setUseNose((String) additionalProperties.get(USE_NOSE));
322326
}
323327

328+
if (additionalProperties.containsKey(USE_INLINE_MODEL_RESOLVER)) {
329+
setUseInlineModelResolver((String) additionalProperties.get(USE_INLINE_MODEL_RESOLVER));
330+
}
331+
324332
// check to see if setRecursionLimit is set and whether it's an integer
325333
if (additionalProperties.containsKey(RECURSION_LIMIT)) {
326334
try {
@@ -1254,20 +1262,31 @@ private String getTypeString(Schema p, String prefix, String suffix, List<String
12541262
return prefix + modelName + fullSuffix;
12551263
}
12561264
}
1257-
if (isAnyTypeSchema(p)) {
1265+
if (ModelUtils.isAnyType(p)) {
12581266
return prefix + "bool, date, datetime, dict, float, int, list, str, none_type" + suffix;
12591267
}
12601268
// Resolve $ref because ModelUtils.isXYZ methods do not automatically resolve references.
12611269
if (ModelUtils.isNullable(ModelUtils.getReferencedSchema(this.openAPI, p))) {
12621270
fullSuffix = ", none_type" + suffix;
12631271
}
1264-
if (isFreeFormObject(p) && getAdditionalProperties(p) == null) {
1265-
return prefix + "bool, date, datetime, dict, float, int, list, str" + fullSuffix;
1266-
} else if (ModelUtils.isNumberSchema(p)) {
1272+
if (ModelUtils.isNumberSchema(p)) {
12671273
return prefix + "int, float" + fullSuffix;
1268-
} else if ((ModelUtils.isMapSchema(p) || "object".equals(p.getType())) && getAdditionalProperties(p) != null) {
1269-
Schema inner = getAdditionalProperties(p);
1270-
return prefix + "{str: " + getTypeString(inner, "(", ")", referencedModelNames) + "}" + fullSuffix;
1274+
} else if (ModelUtils.isTypeObjectSchema(p)) {
1275+
if (p.getAdditionalProperties() != null && p.getAdditionalProperties().equals(false)) {
1276+
if (p.getProperties() == null) {
1277+
// type object with no properties and additionalProperties = false, empty dict only
1278+
return prefix + "{str: typing.Any}" + fullSuffix;
1279+
} else {
1280+
// properties only
1281+
// TODO add type hints for those properties only as values
1282+
return prefix + "{str: typing.Any}" + fullSuffix;
1283+
}
1284+
} else {
1285+
// additionalProperties exists
1286+
Schema inner = getAdditionalProperties(p);
1287+
return prefix + "{str: " + getTypeString(inner, "(", ")", referencedModelNames) + "}" + fullSuffix;
1288+
// TODO add code here to add property values too if they exist
1289+
}
12711290
} else if (ModelUtils.isArraySchema(p)) {
12721291
ArraySchema ap = (ArraySchema) p;
12731292
Schema inner = ap.getItems();
@@ -1284,8 +1303,7 @@ private String getTypeString(Schema p, String prefix, String suffix, List<String
12841303
} else {
12851304
return prefix + getTypeString(inner, "[", "]", referencedModelNames) + fullSuffix;
12861305
}
1287-
}
1288-
if (ModelUtils.isFileSchema(p)) {
1306+
} else if (ModelUtils.isFileSchema(p)) {
12891307
return prefix + "file_type" + fullSuffix;
12901308
}
12911309
String baseType = getSchemaType(p);
@@ -1302,7 +1320,7 @@ private String getTypeString(Schema p, String prefix, String suffix, List<String
13021320
public String getTypeDeclaration(Schema p) {
13031321
// this is used to set dataType, which defines a python tuple of classes
13041322
// in Python we will wrap this in () to make it a tuple but here we
1305-
// will omit the parens so the generated documentaion will not include
1323+
// will omit the parens so the generated documentation will not include
13061324
// them
13071325
return getTypeString(p, "", "", null);
13081326
}
@@ -2237,6 +2255,13 @@ public void setUseNose(String val) {
22372255
this.useNose = Boolean.parseBoolean(val);
22382256
}
22392257

2258+
@Override
2259+
public boolean getUseInlineModelResolver() { return useInlineModelResolver; }
2260+
2261+
public void setUseInlineModelResolver(String val) {
2262+
this.useInlineModelResolver = Boolean.parseBoolean(val);
2263+
}
2264+
22402265
public void setPackageUrl(String packageUrl) {
22412266
this.packageUrl = packageUrl;
22422267
}

modules/openapi-generator/src/main/resources/python-experimental/endpoint.handlebars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import re # noqa: F401
77
import sys # noqa: F401
88
import typing
99
import urllib3
10+
import functools # noqa: F401
1011
{{#with operation}}
1112
{{#or headerParams bodyParam produces}}
1213
from urllib3._collections import HTTPHeaderDict

modules/openapi-generator/src/main/resources/python-experimental/model.handlebars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re # noqa: F401
66
import sys # noqa: F401
77
import typing # noqa: F401
8+
import functools # noqa: F401
89

910
from frozendict import frozendict # noqa: F401
1011

modules/openapi-generator/src/main/resources/python-experimental/model_templates/composed_schemas.handlebars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@classmethod
22
@property
3+
@functools.cache
34
def _composed_schemas(cls):
45
# we need this here to make our import statements work
56
# we must store _composed_schemas in here so the code is only run

samples/openapi3/client/petstore/python-experimental/.openapi-generator/FILES

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ docs/Boolean.md
2626
docs/BooleanEnum.md
2727
docs/Capitalization.md
2828
docs/Cat.md
29-
docs/CatAllOf.md
3029
docs/Category.md
3130
docs/ChildCat.md
32-
docs/ChildCatAllOf.md
3331
docs/ClassModel.md
3432
docs/Client.md
3533
docs/ComplexQuadrilateral.md
36-
docs/ComplexQuadrilateralAllOf.md
3734
docs/ComposedAnyOfDifferentTypesNoValidations.md
3835
docs/ComposedArray.md
3936
docs/ComposedBool.md
@@ -42,7 +39,6 @@ docs/ComposedNumber.md
4239
docs/ComposedObject.md
4340
docs/ComposedOneOfDifferentTypes.md
4441
docs/ComposedString.md
45-
docs/CompositionInProperty.md
4642
docs/Currency.md
4743
docs/DanishPig.md
4844
docs/DateTimeTest.md
@@ -51,13 +47,11 @@ docs/DateWithValidations.md
5147
docs/DecimalPayload.md
5248
docs/DefaultApi.md
5349
docs/Dog.md
54-
docs/DogAllOf.md
5550
docs/Drawing.md
5651
docs/EnumArrays.md
5752
docs/EnumClass.md
5853
docs/EnumTest.md
5954
docs/EquilateralTriangle.md
60-
docs/EquilateralTriangleAllOf.md
6155
docs/FakeApi.md
6256
docs/FakeClassnameTags123Api.md
6357
docs/File.md
@@ -70,17 +64,14 @@ docs/GmFruit.md
7064
docs/GrandparentAnimal.md
7165
docs/HasOnlyReadOnly.md
7266
docs/HealthCheckResult.md
73-
docs/InlineResponseDefault.md
7467
docs/IntegerEnum.md
7568
docs/IntegerEnumBig.md
7669
docs/IntegerEnumOneValue.md
7770
docs/IntegerEnumWithDefaultValue.md
7871
docs/IntegerMax10.md
7972
docs/IntegerMin15.md
8073
docs/IsoscelesTriangle.md
81-
docs/IsoscelesTriangleAllOf.md
8274
docs/Mammal.md
83-
docs/MapBean.md
8475
docs/MapTest.md
8576
docs/MixedPropertiesAndAdditionalPropertiesClass.md
8677
docs/Model200Response.md
@@ -110,11 +101,9 @@ docs/Quadrilateral.md
110101
docs/QuadrilateralInterface.md
111102
docs/ReadOnlyFirst.md
112103
docs/ScaleneTriangle.md
113-
docs/ScaleneTriangleAllOf.md
114104
docs/Shape.md
115105
docs/ShapeOrNull.md
116106
docs/SimpleQuadrilateral.md
117-
docs/SimpleQuadrilateralAllOf.md
118107
docs/SomeObject.md
119108
docs/SpecialModelName.md
120109
docs/StoreApi.md
@@ -169,14 +158,11 @@ petstore_api/model/boolean.py
169158
petstore_api/model/boolean_enum.py
170159
petstore_api/model/capitalization.py
171160
petstore_api/model/cat.py
172-
petstore_api/model/cat_all_of.py
173161
petstore_api/model/category.py
174162
petstore_api/model/child_cat.py
175-
petstore_api/model/child_cat_all_of.py
176163
petstore_api/model/class_model.py
177164
petstore_api/model/client.py
178165
petstore_api/model/complex_quadrilateral.py
179-
petstore_api/model/complex_quadrilateral_all_of.py
180166
petstore_api/model/composed_any_of_different_types_no_validations.py
181167
petstore_api/model/composed_array.py
182168
petstore_api/model/composed_bool.py
@@ -185,21 +171,18 @@ petstore_api/model/composed_number.py
185171
petstore_api/model/composed_object.py
186172
petstore_api/model/composed_one_of_different_types.py
187173
petstore_api/model/composed_string.py
188-
petstore_api/model/composition_in_property.py
189174
petstore_api/model/currency.py
190175
petstore_api/model/danish_pig.py
191176
petstore_api/model/date_time_test.py
192177
petstore_api/model/date_time_with_validations.py
193178
petstore_api/model/date_with_validations.py
194179
petstore_api/model/decimal_payload.py
195180
petstore_api/model/dog.py
196-
petstore_api/model/dog_all_of.py
197181
petstore_api/model/drawing.py
198182
petstore_api/model/enum_arrays.py
199183
petstore_api/model/enum_class.py
200184
petstore_api/model/enum_test.py
201185
petstore_api/model/equilateral_triangle.py
202-
petstore_api/model/equilateral_triangle_all_of.py
203186
petstore_api/model/file.py
204187
petstore_api/model/file_schema_test_class.py
205188
petstore_api/model/foo.py
@@ -210,17 +193,14 @@ petstore_api/model/gm_fruit.py
210193
petstore_api/model/grandparent_animal.py
211194
petstore_api/model/has_only_read_only.py
212195
petstore_api/model/health_check_result.py
213-
petstore_api/model/inline_response_default.py
214196
petstore_api/model/integer_enum.py
215197
petstore_api/model/integer_enum_big.py
216198
petstore_api/model/integer_enum_one_value.py
217199
petstore_api/model/integer_enum_with_default_value.py
218200
petstore_api/model/integer_max10.py
219201
petstore_api/model/integer_min15.py
220202
petstore_api/model/isosceles_triangle.py
221-
petstore_api/model/isosceles_triangle_all_of.py
222203
petstore_api/model/mammal.py
223-
petstore_api/model/map_bean.py
224204
petstore_api/model/map_test.py
225205
petstore_api/model/mixed_properties_and_additional_properties_class.py
226206
petstore_api/model/model200_response.py
@@ -249,11 +229,9 @@ petstore_api/model/quadrilateral.py
249229
petstore_api/model/quadrilateral_interface.py
250230
petstore_api/model/read_only_first.py
251231
petstore_api/model/scalene_triangle.py
252-
petstore_api/model/scalene_triangle_all_of.py
253232
petstore_api/model/shape.py
254233
petstore_api/model/shape_or_null.py
255234
petstore_api/model/simple_quadrilateral.py
256-
petstore_api/model/simple_quadrilateral_all_of.py
257235
petstore_api/model/some_object.py
258236
petstore_api/model/special_model_name.py
259237
petstore_api/model/string.py

samples/openapi3/client/petstore/python-experimental/README.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,11 @@ Class | Method | HTTP request | Description
162162
- [BooleanEnum](docs/BooleanEnum.md)
163163
- [Capitalization](docs/Capitalization.md)
164164
- [Cat](docs/Cat.md)
165-
- [CatAllOf](docs/CatAllOf.md)
166165
- [Category](docs/Category.md)
167166
- [ChildCat](docs/ChildCat.md)
168-
- [ChildCatAllOf](docs/ChildCatAllOf.md)
169167
- [ClassModel](docs/ClassModel.md)
170168
- [Client](docs/Client.md)
171169
- [ComplexQuadrilateral](docs/ComplexQuadrilateral.md)
172-
- [ComplexQuadrilateralAllOf](docs/ComplexQuadrilateralAllOf.md)
173170
- [ComposedAnyOfDifferentTypesNoValidations](docs/ComposedAnyOfDifferentTypesNoValidations.md)
174171
- [ComposedArray](docs/ComposedArray.md)
175172
- [ComposedBool](docs/ComposedBool.md)
@@ -178,21 +175,18 @@ Class | Method | HTTP request | Description
178175
- [ComposedObject](docs/ComposedObject.md)
179176
- [ComposedOneOfDifferentTypes](docs/ComposedOneOfDifferentTypes.md)
180177
- [ComposedString](docs/ComposedString.md)
181-
- [CompositionInProperty](docs/CompositionInProperty.md)
182178
- [Currency](docs/Currency.md)
183179
- [DanishPig](docs/DanishPig.md)
184180
- [DateTimeTest](docs/DateTimeTest.md)
185181
- [DateTimeWithValidations](docs/DateTimeWithValidations.md)
186182
- [DateWithValidations](docs/DateWithValidations.md)
187183
- [DecimalPayload](docs/DecimalPayload.md)
188184
- [Dog](docs/Dog.md)
189-
- [DogAllOf](docs/DogAllOf.md)
190185
- [Drawing](docs/Drawing.md)
191186
- [EnumArrays](docs/EnumArrays.md)
192187
- [EnumClass](docs/EnumClass.md)
193188
- [EnumTest](docs/EnumTest.md)
194189
- [EquilateralTriangle](docs/EquilateralTriangle.md)
195-
- [EquilateralTriangleAllOf](docs/EquilateralTriangleAllOf.md)
196190
- [File](docs/File.md)
197191
- [FileSchemaTestClass](docs/FileSchemaTestClass.md)
198192
- [Foo](docs/Foo.md)
@@ -203,17 +197,14 @@ Class | Method | HTTP request | Description
203197
- [GrandparentAnimal](docs/GrandparentAnimal.md)
204198
- [HasOnlyReadOnly](docs/HasOnlyReadOnly.md)
205199
- [HealthCheckResult](docs/HealthCheckResult.md)
206-
- [InlineResponseDefault](docs/InlineResponseDefault.md)
207200
- [IntegerEnum](docs/IntegerEnum.md)
208201
- [IntegerEnumBig](docs/IntegerEnumBig.md)
209202
- [IntegerEnumOneValue](docs/IntegerEnumOneValue.md)
210203
- [IntegerEnumWithDefaultValue](docs/IntegerEnumWithDefaultValue.md)
211204
- [IntegerMax10](docs/IntegerMax10.md)
212205
- [IntegerMin15](docs/IntegerMin15.md)
213206
- [IsoscelesTriangle](docs/IsoscelesTriangle.md)
214-
- [IsoscelesTriangleAllOf](docs/IsoscelesTriangleAllOf.md)
215207
- [Mammal](docs/Mammal.md)
216-
- [MapBean](docs/MapBean.md)
217208
- [MapTest](docs/MapTest.md)
218209
- [MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md)
219210
- [Model200Response](docs/Model200Response.md)
@@ -242,11 +233,9 @@ Class | Method | HTTP request | Description
242233
- [QuadrilateralInterface](docs/QuadrilateralInterface.md)
243234
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
244235
- [ScaleneTriangle](docs/ScaleneTriangle.md)
245-
- [ScaleneTriangleAllOf](docs/ScaleneTriangleAllOf.md)
246236
- [Shape](docs/Shape.md)
247237
- [ShapeOrNull](docs/ShapeOrNull.md)
248238
- [SimpleQuadrilateral](docs/SimpleQuadrilateral.md)
249-
- [SimpleQuadrilateralAllOf](docs/SimpleQuadrilateralAllOf.md)
250239
- [SomeObject](docs/SomeObject.md)
251240
- [SpecialModelName](docs/SpecialModelName.md)
252241
- [String](docs/String.md)

0 commit comments

Comments
 (0)