Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6a8769e
Fix OpenAPITools#5381
codermoderlife Mar 21, 2020
049359c
Fix OpenAPITools#5381
codermoderlife Mar 22, 2020
49f13eb
Fix OpenAPITools#5381
codermoderlife Mar 22, 2020
1a5a484
Fix OpenAPITools#5381
codermoderlife Mar 22, 2020
66a09d2
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
codermoderlife Mar 22, 2020
48a1a4a
Fix OpenAPITools#5381
codermoderlife Mar 24, 2020
a811330
Fix OpenAPITools#5381
codermoderlife Mar 24, 2020
299cdb3
Fix OpenAPITools#5381
codermoderlife Mar 24, 2020
461cbc8
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
codermoderlife Mar 24, 2020
dd0118d
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
codermoderlife Mar 25, 2020
26a95f6
Fix OpenAPITools#5381
codermoderlife Mar 25, 2020
9af7db6
Fix OpenAPITools#5381
codermoderlife Mar 30, 2020
fbfd57d
Fix OpenAPITools#5381
codermoderlife Mar 21, 2020
bfab304
Fix OpenAPITools#5381
codermoderlife Mar 22, 2020
29003d8
Fix OpenAPITools#5381
codermoderlife Apr 1, 2020
d864831
Fix OpenAPITools#5381
codermoderlife Apr 1, 2020
6d54799
Fix OpenAPITools#5381
codermoderlife Apr 2, 2020
fe7fd56
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
codermoderlife Feb 3, 2021
9b586fa
[Spring] codegen : oneOf support for spring (#5381)
codermoderlife Feb 3, 2021
07df40c
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
codermoderlife Feb 4, 2021
15b362a
[Spring] codegen : oneOf support for spring (#5381) avoid npe
codermoderlife Feb 4, 2021
f0aeb8c
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
codermoderlife Jul 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ public void postProcessParameter(CodegenParameter parameter) {
//override with any special handling of the entire OpenAPI spec document
@SuppressWarnings("unused")
public void preprocessOpenAPI(OpenAPI openAPI) {
if (useOneOfInterfaces) {
if (useOneOfInterfaces && openAPI.getComponents() != null) {
Comment thread
alexsuperdev marked this conversation as resolved.
Outdated
// we process the openapi schema here to find oneOf schemas and create interface models for them
Map<String, Schema> schemas = new HashMap<String, Schema>(openAPI.getComponents().getSchemas());
if (schemas == null) {
Expand All @@ -733,11 +733,15 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
schemas.put(opId, requestSchema);
}
// process all response bodies
for (Map.Entry<String, ApiResponse> ar : op.getValue().getResponses().entrySet()) {
ApiResponse a = ModelUtils.getReferencedApiResponse(openAPI, ar.getValue());
Schema responseSchema = ModelUtils.getSchemaFromResponse(a);
if (responseSchema != null) {
schemas.put(opId + ar.getKey(), responseSchema);
if (op.getValue().getResponses() != null) {
for (Map.Entry<String, ApiResponse> ar : op.getValue().getResponses()
.entrySet()) {
ApiResponse a = ModelUtils
.getReferencedApiResponse(openAPI, ar.getValue());
Schema responseSchema = ModelUtils.getSchemaFromResponse(a);
if (responseSchema != null) {
schemas.put(opId + ar.getKey(), responseSchema);
}
}
}
}
Expand All @@ -764,14 +768,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
Schema s = e.getValue();
String nOneOf = toModelName(n + "OneOf");
if (ModelUtils.isComposedSchema(s)) {
if (e.getKey().contains("/")) {
// if this is property schema, we also need to generate the oneOf interface model
addOneOfNameExtension((ComposedSchema) s, nOneOf);
addOneOfInterfaceModel((ComposedSchema) s, nOneOf);
} else {
// else this is a component schema, so we will just use that as the oneOf interface model
addOneOfNameExtension((ComposedSchema) s, n);
}
addOneOfForComposedSchema(e, n, (ComposedSchema) s, nOneOf);
} else if (ModelUtils.isArraySchema(s)) {
Schema items = ((ArraySchema) s).getItems();
if (ModelUtils.isComposedSchema(items)) {
Expand All @@ -789,6 +786,18 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}
}

protected void addOneOfForComposedSchema(Entry<String, Schema> stringSchemaEntry, String modelName, ComposedSchema composedSchema,
String nOneOf) {
if (stringSchemaEntry.getKey().contains("/")) {
// if this is property schema, we also need to generate the oneOf interface model
addOneOfNameExtension(composedSchema, nOneOf);
addOneOfInterfaceModel(composedSchema, nOneOf);
} else {
// else this is a component schema, so we will just use that as the oneOf interface model
addOneOfNameExtension(composedSchema, modelName);
}
}

// override with any special handling of the entire OpenAPI spec document
@SuppressWarnings("unused")
public void processOpenAPI(OpenAPI openAPI) {
Expand Down Expand Up @@ -5444,8 +5453,8 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
"'application/x-www-form-urlencoded' or 'multipart/?'");
LOGGER.warn("schema: " + schema);
LOGGER.warn("codegenModel is null. Default to UNKNOWN_BASE_TYPE");
codegenModelName = "UNKNOWN_BASE_TYPE";
codegenModelDescription = "UNKNOWN_DESCRIPTION";
codegenModelName = getCodegenModelName(codegenProperty);
}

if (StringUtils.isEmpty(bodyParameterName)) {
Expand All @@ -5461,7 +5470,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
imports.add(codegenParameter.baseType);

if (codegenProperty.complexType != null) {
imports.add(codegenProperty.complexType);
addAdditionalImports(imports, codegenProperty.complexType);
}
}
}
Expand Down Expand Up @@ -5510,6 +5519,14 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
return codegenParameter;
}

protected void addAdditionalImports(Set<String> imports, String complexType) {
imports.add(complexType);
}

protected String getCodegenModelName(CodegenProperty codegenProperty) {
return "UNKNOWN_BASE_TYPE";
}

protected void addOption(String key, String description, String defaultValue) {
CliOption option = new CliOption(key, description);
if (defaultValue != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.media.ComposedSchema;
import io.swagger.v3.oas.models.media.Schema;
import java.util.Map.Entry;
import org.apache.commons.lang3.tuple.Pair;
import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
Expand Down Expand Up @@ -233,7 +236,7 @@ public void processOpts() {
}

super.processOpts();

useOneOfInterfaces = true;
// clear model and api doc template as this codegen
// does not support auto-generated markdown doc at the moment
//TODO: add doc templates
Expand Down Expand Up @@ -581,6 +584,34 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}
}

@Override
protected void addOneOfForComposedSchema(Entry<String, Schema> stringSchemaEntry, String modelName, ComposedSchema composedSchema,
String nOneOf){
if (useOneOfInterfaces) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useOneOfInterfaces is always true?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are right useOneOfInterfaces is always true, i will change the code, that there is no if cases anymore.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scorpioncik please review this pull request again.

// if this is property schema, we also need to generate the oneOf interface model
addOneOfNameExtension(composedSchema, nOneOf);
addOneOfInterfaceModel(composedSchema, nOneOf);
} else {
super.addOneOfForComposedSchema(stringSchemaEntry, modelName, composedSchema, nOneOf);
}
}

@Override
protected String getCodegenModelName(CodegenProperty codegenProperty) {
if (useOneOfInterfaces){
return codegenProperty.getComplexType();
} else {
return super.getCodegenModelName(codegenProperty);
}
}

@Override
protected void addAdditionalImports(Set<String> imports, String complexType) {
if (!useOneOfInterfaces){
super.addAdditionalImports(imports, complexType);
}
}

@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
Expand Down Expand Up @@ -898,4 +929,16 @@ public void postProcessParameter(CodegenParameter p) {
}
}

}
@Override
public void addImportsToOneOfInterface(List<Map<String, String>> imports) {
for (String i : Arrays.asList("JsonSubTypes", "JsonTypeInfo")) {
Map<String, String> oneImport = new HashMap<String, String>() {{
put("import", importMapping.get(i));
}};
if (!imports.contains(oneImport)) {
imports.add(oneImport);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import org.springframework.hateoas.RepresentationModel;
{{>enumOuterClass}}
{{/isEnum}}
{{^isEnum}}
{{>pojo}}
{{#vendorExtensions.x-is-one-of-interface}}{{>oneof_interface}}{{/vendorExtensions.x-is-one-of-interface}}{{^vendorExtensions.x-is-one-of-interface}}{{>pojo}}{{/vendorExtensions.x-is-one-of-interface}}
{{/isEnum}}
{{/model}}
{{/models}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{>additionalModelTypeAnnotations}}{{>generatedAnnotation}}{{>typeInfoAnnotation}}{{>xmlAnnotation}}
public interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {
Comment thread
alexsuperdev marked this conversation as resolved.
{{#discriminator}}
public {{propertyType}} {{propertyGetter}}();
{{/discriminator}}
}