Skip to content

Commit cd551a7

Browse files
author
Slavek Kabrda
committed
Fix generation of oneOf interfaces for oneOf usage in properties
1 parent 67e3bf5 commit cd551a7

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,34 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
740740
}
741741
}
742742

743+
// also add all properties of all schemas to be checked for oneOf
744+
Map<String, Schema> propertySchemas = new HashMap<String, Schema>();
745+
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
746+
Schema s = e.getValue();
747+
Map<String, Schema> props = s.getProperties();
748+
if (props == null) {
749+
props = new HashMap<String, Schema>();
750+
}
751+
for (Map.Entry<String, Schema> p : props.entrySet()) {
752+
propertySchemas.put(e.getKey() + "/" + p.getKey(), p.getValue());
753+
}
754+
}
755+
schemas.putAll(propertySchemas);
756+
743757
// go through all gathered schemas and add them as interfaces to be created
744758
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
745759
String n = toModelName(e.getKey());
746760
Schema s = e.getValue();
747761
String nOneOf = toModelName(n + "OneOf");
748762
if (ModelUtils.isComposedSchema(s)) {
749-
addOneOfNameExtension((ComposedSchema) s, n);
763+
if (e.getKey().contains("/")) {
764+
// if this is property schema, we also need to generate the oneOf interface model
765+
addOneOfNameExtension((ComposedSchema) s, nOneOf);
766+
addOneOfInterfaceModel((ComposedSchema) s, nOneOf);
767+
} else {
768+
// else this is a component schema, so we will just use that as the oneOf interface model
769+
addOneOfNameExtension((ComposedSchema) s, n);
770+
}
750771
} else if (ModelUtils.isArraySchema(s)) {
751772
Schema items = ((ArraySchema) s).getItems();
752773
if (ModelUtils.isComposedSchema(items)) {
@@ -5717,7 +5738,8 @@ public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
57175738
CodegenModel cm = new CodegenModel();
57185739

57195740
cm.discriminator = createDiscriminator("", (Schema) cs);
5720-
for (Schema o : cs.getOneOf()) {
5741+
5742+
for (Schema o : Optional.ofNullable(cs.getOneOf()).orElse(Collections.emptyList())) {
57215743
if (o.get$ref() == null) {
57225744
if (cm.discriminator != null && o.get$ref() == null) {
57235745
// OpenAPI spec states that inline objects should not be considered when discriminator is used

0 commit comments

Comments
 (0)