Skip to content

Commit fb98903

Browse files
Slavek Kabrdamichaelpro1
Slavek Kabrda
authored andcommitted
Fix generation of oneOf interfaces for oneOf usage in properties (OpenAPITools#5400)
* Fix generation of oneOf interfaces for oneOf usage in properties * Only generate oneOf interface models when oneOf is defined
1 parent 3118170 commit fb98903

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,34 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
744744
}
745745
}
746746

747+
// also add all properties of all schemas to be checked for oneOf
748+
Map<String, Schema> propertySchemas = new HashMap<String, Schema>();
749+
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
750+
Schema s = e.getValue();
751+
Map<String, Schema> props = s.getProperties();
752+
if (props == null) {
753+
props = new HashMap<String, Schema>();
754+
}
755+
for (Map.Entry<String, Schema> p : props.entrySet()) {
756+
propertySchemas.put(e.getKey() + "/" + p.getKey(), p.getValue());
757+
}
758+
}
759+
schemas.putAll(propertySchemas);
760+
747761
// go through all gathered schemas and add them as interfaces to be created
748762
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
749763
String n = toModelName(e.getKey());
750764
Schema s = e.getValue();
751765
String nOneOf = toModelName(n + "OneOf");
752766
if (ModelUtils.isComposedSchema(s)) {
753-
addOneOfNameExtension((ComposedSchema) s, n);
767+
if (e.getKey().contains("/")) {
768+
// if this is property schema, we also need to generate the oneOf interface model
769+
addOneOfNameExtension((ComposedSchema) s, nOneOf);
770+
addOneOfInterfaceModel((ComposedSchema) s, nOneOf);
771+
} else {
772+
// else this is a component schema, so we will just use that as the oneOf interface model
773+
addOneOfNameExtension((ComposedSchema) s, n);
774+
}
754775
} else if (ModelUtils.isArraySchema(s)) {
755776
Schema items = ((ArraySchema) s).getItems();
756777
if (ModelUtils.isComposedSchema(items)) {
@@ -5726,15 +5747,19 @@ public void addOneOfNameExtension(ComposedSchema s, String name) {
57265747
}
57275748

57285749
/**
5729-
* Add a given ComposedSchema as an interface model to be generated
5750+
* Add a given ComposedSchema as an interface model to be generated, assuming it has `oneOf` defined
57305751
* @param cs ComposedSchema object to create as interface model
57315752
* @param type name to use for the generated interface model
57325753
*/
57335754
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
5755+
if (cs.getOneOf() == null) {
5756+
return;
5757+
}
57345758
CodegenModel cm = new CodegenModel();
57355759

57365760
cm.discriminator = createDiscriminator("", (Schema) cs);
5737-
for (Schema o : cs.getOneOf()) {
5761+
5762+
for (Schema o : Optional.ofNullable(cs.getOneOf()).orElse(Collections.emptyList())) {
57385763
if (o.get$ref() == null) {
57395764
if (cm.discriminator != null && o.get$ref() == null) {
57405765
// OpenAPI spec states that inline objects should not be considered when discriminator is used

0 commit comments

Comments
 (0)