Skip to content

Commit 2db8477

Browse files
committed
generate oneOf Class that has all properties from inherited classes
1 parent 08d0eb1 commit 2db8477

File tree

1 file changed

+54
-24
lines changed
  • modules/openapi-generator/src/main/java/org/openapitools/codegen/languages

1 file changed

+54
-24
lines changed

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

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.swagger.v3.oas.models.media.Schema;
2727
import io.swagger.v3.oas.models.parameters.RequestBody;
2828
import io.swagger.v3.oas.models.responses.ApiResponse;
29+
import org.apache.commons.lang3.StringUtils;
2930
import org.apache.commons.lang3.tuple.Pair;
3031
import org.openapitools.codegen.*;
3132
import org.openapitools.codegen.languages.features.BeanValidationFeatures;
@@ -621,19 +622,25 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
621622

622623
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
623624
String n = toModelName(e.getKey());
624-
Schema s = e.getValue();
625+
Schema schema = e.getValue();
625626
String nOneOf = toModelName(n + "OneOf");
626-
if (ModelUtils.isComposedSchema(s)) {
627-
addOneOfNameExtension(s, n);
628-
addOneOfInterfaceModel((ComposedSchema) s, nOneOf);
629-
} else if (ModelUtils.isArraySchema(s)) {
630-
Schema items = ((ArraySchema) s).getItems();
627+
if (ModelUtils.isComposedSchema(schema)) {
628+
List<String> names = new ArrayList<>();
629+
ComposedSchema composedSchema = (ComposedSchema) schema;
630+
for (Schema oneOfSchema : composedSchema.getOneOf()) {
631+
names.add(getSingleSchemaType(oneOfSchema));
632+
}
633+
String name = "OneOf" + String.join("", names);
634+
addOneOfNameExtension(schema, name);
635+
addOneOfInterfaceModel((ComposedSchema) schema, name);
636+
} else if (ModelUtils.isArraySchema(schema)) {
637+
Schema items = ((ArraySchema) schema).getItems();
631638
if (ModelUtils.isComposedSchema(items)) {
632639
addOneOfNameExtension(items, nOneOf);
633640
addOneOfInterfaceModel((ComposedSchema) items, nOneOf);
634641
}
635-
} else if (ModelUtils.isMapSchema(s)) {
636-
Schema addProps = ModelUtils.getAdditionalProperties(s);
642+
} else if (ModelUtils.isMapSchema(schema)) {
643+
Schema addProps = ModelUtils.getAdditionalProperties(schema);
637644
if (addProps != null && ModelUtils.isComposedSchema(addProps)) {
638645
addOneOfNameExtension(addProps, nOneOf);
639646
addOneOfInterfaceModel((ComposedSchema) addProps, nOneOf);
@@ -649,22 +656,41 @@ public void addOneOfNameExtension(Schema s, String name) {
649656
}
650657
}
651658

652-
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
659+
public void addOneOfInterfaceModel(ComposedSchema cs, String name) {
653660
CodegenModel cm = new CodegenModel();
654661

655662
for (Schema o : cs.getOneOf()) {
656663
// TODO: inline objects
657664
cm.oneOf.add(toModelName(ModelUtils.getSimpleRef(o.get$ref())));
658665
}
659-
cm.name = type;
660-
cm.classname = type;
666+
cm.name = name;
667+
cm.classname = name;
661668
cm.vendorExtensions.put("isOneOfInterface", true);
662669
cm.discriminator = createDiscriminator("", (Schema) cs);
663670
cm.interfaceModels = new ArrayList<CodegenModel>();
664671

672+
for(Schema schema : cs.getOneOf()){
673+
cm.vars.add(fromProperty("property", schema));
674+
}
665675
addOneOfInterfaces.add(cm);
666676
}
667677

678+
private String getSingleSchemaType(Schema schema) {
679+
Schema unaliasSchema = ModelUtils.unaliasSchema(this.openAPI, schema);
680+
681+
if (StringUtils.isNotBlank(unaliasSchema.get$ref())) { // reference to another definition/schema
682+
// get the schema/model name from $ref
683+
String schemaName = ModelUtils.getSimpleRef(unaliasSchema.get$ref());
684+
if (StringUtils.isNotEmpty(schemaName)) {
685+
return getAlias(schemaName);
686+
} else {
687+
LOGGER.warn("Error obtaining the datatype from ref:" + unaliasSchema.get$ref() + ". Default to 'object'");
688+
return "object";
689+
}
690+
}
691+
return null;
692+
}
693+
668694
private class OneOfImplementorAdditionalData {
669695
private String implementorName;
670696
private List<String> additionalInterfaces = new ArrayList<String>();
@@ -812,24 +838,28 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
812838
}
813839
}
814840
}
815-
816-
for (Map.Entry modelsEntry : objs.entrySet()) {
817-
Map<String, Object> modelsAttrs = (Map<String, Object>) modelsEntry.getValue();
818-
List<Object> models = (List<Object>) modelsAttrs.get("models");
819-
List<Map<String, String>> imports = (List<Map<String, String>>) modelsAttrs.get("imports");
820-
for (Object _implmo : models) {
821-
Map<String, Object> implmo = (Map<String, Object>) _implmo;
822-
CodegenModel implcm = (CodegenModel) implmo.get("model");
823-
if (additionalDataMap.containsKey(implcm.name)) {
824-
additionalDataMap.get(implcm.name).addToImplementor(implcm, imports);
825-
}
826-
}
827-
}
828841
}
829842

830843
return objs;
831844
}
832845

846+
@Override
847+
public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, String bodyParameterName) {
848+
CodegenParameter codegenParameter = super.fromRequestBody(body, imports, bodyParameterName);
849+
Schema schema = ModelUtils.getSchemaFromRequestBody(body);
850+
CodegenProperty codegenProperty = fromProperty("property", schema);
851+
if (codegenProperty != null && codegenProperty.getComplexType() != null && schema instanceof ComposedSchema) {
852+
String codegenModelName = codegenProperty.getComplexType();
853+
codegenParameter.baseName = codegenModelName;
854+
codegenParameter.paramName = toParamName(codegenParameter.baseName);
855+
codegenParameter.baseType = codegenParameter.baseName;
856+
codegenParameter.dataType = getTypeDeclaration(codegenModelName);
857+
codegenParameter.description = codegenProperty.getDescription();
858+
codegenProperty.setComplexType(codegenModelName);
859+
}
860+
return codegenParameter;
861+
}
862+
833863
@Override
834864
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
835865
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");

0 commit comments

Comments
 (0)