Skip to content

Commit 252c3e5

Browse files
spacetherwing328
authored andcommitted
[python-experimental] generate model if type != object if enums/validations exist (#2757)
* Python-experimental adds model_utils module, refactors python api class * Fixes python-experimental so the sample sare generated in the petstore_api folder * FIxes python samples tests * Updates python and python-experimental tests * Fixes python-experimental tests * Adds newlines back to python templates + samples * Reverts files with newline tweaks back to master branch versions * Fixes indentation errors in python-experimental api_client * Removes unused files * Python files now generated in correct folders * Adds logging when the user tries to set generateAliasAsModel in python-experimental * Fixes typo
1 parent 002da8d commit 252c3e5

File tree

106 files changed

+9442
-7619
lines changed

Some content is hidden

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

106 files changed

+9442
-7619
lines changed

bin/python-experimental-petstore.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ fi
2727

2828
# if you've executed sbt assembly previously it will use that instead.
2929
export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties"
30-
ags="generate -t modules/openapi-generator/src/main/resources/python -i modules/openapi-generator/src/test/resources/2_0/python-client-experimental/petstore-with-fake-endpoints-models-for-testing.yaml -g python-experimental -o samples/client/petstore/python-experimental -DpackageName=petstore_api $@"
30+
ags="generate -t modules/openapi-generator/src/main/resources/python -i modules/openapi-generator/src/test/resources/2_0/python-client-experimental/petstore-with-fake-endpoints-models-for-testing.yaml -g python-experimental -o samples/client/petstore/python-experimental --additional-properties packageName=petstore_api $@"
3131

3232
java $JAVA_OPTS -jar $executable $ags

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

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,6 +2493,76 @@ protected ApiResponse findMethodResponse(ApiResponses responses) {
24932493
return responses.get(code);
24942494
}
24952495

2496+
/**
2497+
* Set op's returnBaseType, returnType, examples etc.
2498+
*
2499+
* @param operation endpoint Operation
2500+
* @param schemas a map of the schemas in the openapi spec
2501+
* @param op endpoint CodegenOperation
2502+
* @param methodResponse the default ApiResponse for the endpoint
2503+
*/
2504+
protected void handleMethodResponse(Operation operation,
2505+
Map<String, Schema> schemas,
2506+
CodegenOperation op,
2507+
ApiResponse methodResponse) {
2508+
Schema responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(methodResponse));
2509+
2510+
if (responseSchema != null) {
2511+
CodegenProperty cm = fromProperty("response", responseSchema);
2512+
2513+
if (ModelUtils.isArraySchema(responseSchema)) {
2514+
ArraySchema as = (ArraySchema) responseSchema;
2515+
CodegenProperty innerProperty = fromProperty("response", getSchemaItems(as));
2516+
op.returnBaseType = innerProperty.baseType;
2517+
} else if (ModelUtils.isMapSchema(responseSchema)) {
2518+
CodegenProperty innerProperty = fromProperty("response", ModelUtils.getAdditionalProperties(responseSchema));
2519+
op.returnBaseType = innerProperty.baseType;
2520+
} else {
2521+
if (cm.complexType != null) {
2522+
op.returnBaseType = cm.complexType;
2523+
} else {
2524+
op.returnBaseType = cm.baseType;
2525+
}
2526+
}
2527+
2528+
// generate examples
2529+
String exampleStatusCode = "200";
2530+
for (String key : operation.getResponses().keySet()) {
2531+
if (operation.getResponses().get(key) == methodResponse && !key.equals("default")) {
2532+
exampleStatusCode = key;
2533+
}
2534+
}
2535+
op.examples = new ExampleGenerator(schemas, this.openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(this.openAPI, operation));
2536+
op.defaultResponse = toDefaultValue(responseSchema);
2537+
op.returnType = cm.dataType;
2538+
op.hasReference = schemas.containsKey(op.returnBaseType);
2539+
2540+
// lookup discriminator
2541+
Schema schema = schemas.get(op.returnBaseType);
2542+
if (schema != null) {
2543+
CodegenModel cmod = fromModel(op.returnBaseType, schema);
2544+
op.discriminator = cmod.discriminator;
2545+
}
2546+
2547+
if (cm.isContainer) {
2548+
op.returnContainer = cm.containerType;
2549+
if ("map".equals(cm.containerType)) {
2550+
op.isMapContainer = true;
2551+
} else if ("list".equalsIgnoreCase(cm.containerType)) {
2552+
op.isListContainer = true;
2553+
} else if ("array".equalsIgnoreCase(cm.containerType)) {
2554+
op.isListContainer = true;
2555+
}
2556+
} else {
2557+
op.returnSimpleType = true;
2558+
}
2559+
if (languageSpecificPrimitives().contains(op.returnBaseType) || op.returnBaseType == null) {
2560+
op.returnTypeIsPrimitive = true;
2561+
}
2562+
}
2563+
addHeaders(methodResponse, op.responseHeaders);
2564+
}
2565+
24962566
/**
24972567
* Convert OAS Operation object to Codegen Operation object
24982568
*
@@ -2585,62 +2655,7 @@ public CodegenOperation fromOperation(String path,
25852655
op.responses.get(op.responses.size() - 1).hasMore = false;
25862656

25872657
if (methodResponse != null) {
2588-
Schema responseSchema = ModelUtils.unaliasSchema(this.openAPI, ModelUtils.getSchemaFromResponse(methodResponse));
2589-
2590-
if (responseSchema != null) {
2591-
CodegenProperty cm = fromProperty("response", responseSchema);
2592-
2593-
if (ModelUtils.isArraySchema(responseSchema)) {
2594-
ArraySchema as = (ArraySchema) responseSchema;
2595-
CodegenProperty innerProperty = fromProperty("response", getSchemaItems(as));
2596-
op.returnBaseType = innerProperty.baseType;
2597-
} else if (ModelUtils.isMapSchema(responseSchema)) {
2598-
CodegenProperty innerProperty = fromProperty("response", ModelUtils.getAdditionalProperties(responseSchema));
2599-
op.returnBaseType = innerProperty.baseType;
2600-
} else {
2601-
if (cm.complexType != null) {
2602-
op.returnBaseType = cm.complexType;
2603-
} else {
2604-
op.returnBaseType = cm.baseType;
2605-
}
2606-
}
2607-
2608-
// generate examples
2609-
String exampleStatusCode = "200";
2610-
for (String key : operation.getResponses().keySet()) {
2611-
if (operation.getResponses().get(key) == methodResponse && !key.equals("default")) {
2612-
exampleStatusCode = key;
2613-
}
2614-
}
2615-
op.examples = new ExampleGenerator(schemas, this.openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(this.openAPI, operation));
2616-
op.defaultResponse = toDefaultValue(responseSchema);
2617-
op.returnType = cm.dataType;
2618-
op.hasReference = schemas.containsKey(op.returnBaseType);
2619-
2620-
// lookup discriminator
2621-
Schema schema = schemas.get(op.returnBaseType);
2622-
if (schema != null) {
2623-
CodegenModel cmod = fromModel(op.returnBaseType, schema);
2624-
op.discriminator = cmod.discriminator;
2625-
}
2626-
2627-
if (cm.isContainer) {
2628-
op.returnContainer = cm.containerType;
2629-
if ("map".equals(cm.containerType)) {
2630-
op.isMapContainer = true;
2631-
} else if ("list".equalsIgnoreCase(cm.containerType)) {
2632-
op.isListContainer = true;
2633-
} else if ("array".equalsIgnoreCase(cm.containerType)) {
2634-
op.isListContainer = true;
2635-
}
2636-
} else {
2637-
op.returnSimpleType = true;
2638-
}
2639-
if (languageSpecificPrimitives().contains(op.returnBaseType) || op.returnBaseType == null) {
2640-
op.returnTypeIsPrimitive = true;
2641-
}
2642-
}
2643-
addHeaders(methodResponse, op.responseHeaders);
2658+
handleMethodResponse(operation, schemas, op, methodResponse);
26442659
}
26452660
}
26462661

@@ -3511,7 +3526,7 @@ protected List<Map<String, Object>> toExamples(Map<String, Object> examples) {
35113526
* @param response API response
35123527
* @param properties list of codegen property
35133528
*/
3514-
private void addHeaders(ApiResponse response, List<CodegenProperty> properties) {
3529+
protected void addHeaders(ApiResponse response, List<CodegenProperty> properties) {
35153530
if (response.getHeaders() != null) {
35163531
for (Map.Entry<String, Header> headerEntry : response.getHeaders().entrySet()) {
35173532
String description = headerEntry.getValue().getDescription();
@@ -4316,7 +4331,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
43164331
}
43174332
}
43184333

4319-
private void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
4334+
protected void updateEnumVarsWithExtensions(List<Map<String, Object>> enumVars, Map<String, Object> vendorExtensions) {
43204335
if (vendorExtensions != null) {
43214336
updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-varnames", "name");
43224337
updateEnumVarsWithExtensions(enumVars, vendorExtensions, "x-enum-descriptions", "enumDescription");

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,9 @@ private Model getParent(Model model) {
490490

491491
// TODO revise below as we've already performed unaliasing so that the isAlias check may be removed
492492
Map<String, Object> modelTemplate = (Map<String, Object>) ((List<Object>) models.get("models")).get(0);
493-
// Special handling of aliases only applies to Java
494493
if (modelTemplate != null && modelTemplate.containsKey("model")) {
495494
CodegenModel m = (CodegenModel) modelTemplate.get("model");
496-
if (m.isAlias) {
495+
if (m.isAlias && !ModelUtils.isGenerateAliasAsModel()) {
497496
continue; // Don't create user-defined classes for aliases
498497
}
499498
}

0 commit comments

Comments
 (0)