Skip to content

Commit bfab304

Browse files
committed
fixed name of model from UNKNOWN_BASE_TYPE to right one in api: operationId + OneOf Fix OpenAPITools#5381 removed not needed methods Fix OpenAPITools#5381 fixed generation of oneOf Models Fix OpenAPITools#5381 addOneOfInterfaceModel only for cases when useOneOfInterfaces is true and for spring Fix OpenAPITools#5381 NPE fix for tests
1 parent fbfd57d commit bfab304

File tree

2 files changed

+7
-34
lines changed

2 files changed

+7
-34
lines changed

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

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -748,27 +748,16 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
748748
}
749749
}
750750

751-
// also add all properties of all schemas to be checked for oneOf
752-
Map<String, Schema> propertySchemas = new HashMap<String, Schema>();
753-
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
754-
Schema s = e.getValue();
755-
Map<String, Schema> props = s.getProperties();
756-
if (props == null) {
757-
props = new HashMap<String, Schema>();
758-
}
759-
for (Map.Entry<String, Schema> p : props.entrySet()) {
760-
propertySchemas.put(e.getKey() + "/" + p.getKey(), p.getValue());
761-
}
762-
}
763-
schemas.putAll(propertySchemas);
764-
765751
// go through all gathered schemas and add them as interfaces to be created
766752
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
767753
String n = toModelName(e.getKey());
768754
Schema s = e.getValue();
769755
String nOneOf = toModelName(n + "OneOf");
770756
if (ModelUtils.isComposedSchema(s)) {
771-
addOneOfForComposedSchema(e, n, (ComposedSchema) s, nOneOf);
757+
addOneOfNameExtension((ComposedSchema) s, n);
758+
if (useOneOfInterfaces && "JavaSpring".equals(templateDir)){
759+
addOneOfInterfaceModel((ComposedSchema) s, nOneOf);
760+
}
772761
} else if (ModelUtils.isArraySchema(s)) {
773762
Schema items = ((ArraySchema) s).getItems();
774763
if (ModelUtils.isComposedSchema(items)) {
@@ -786,18 +775,6 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
786775
}
787776
}
788777

789-
protected void addOneOfForComposedSchema(Entry<String, Schema> stringSchemaEntry, String modelName, ComposedSchema composedSchema,
790-
String nOneOf) {
791-
if (stringSchemaEntry.getKey().contains("/")) {
792-
// if this is property schema, we also need to generate the oneOf interface model
793-
addOneOfNameExtension(composedSchema, nOneOf);
794-
addOneOfInterfaceModel(composedSchema, nOneOf);
795-
} else {
796-
// else this is a component schema, so we will just use that as the oneOf interface model
797-
addOneOfNameExtension(composedSchema, modelName);
798-
}
799-
}
800-
801778
// override with any special handling of the entire OpenAPI spec document
802779
@SuppressWarnings("unused")
803780
public void processOpenAPI(OpenAPI openAPI) {
@@ -5764,19 +5741,15 @@ public void addOneOfNameExtension(ComposedSchema s, String name) {
57645741
}
57655742

57665743
/**
5767-
* Add a given ComposedSchema as an interface model to be generated, assuming it has `oneOf` defined
5744+
* Add a given ComposedSchema as an interface model to be generated
57685745
* @param cs ComposedSchema object to create as interface model
57695746
* @param type name to use for the generated interface model
57705747
*/
57715748
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
5772-
if (cs.getOneOf() == null) {
5773-
return;
5774-
}
57755749
CodegenModel cm = new CodegenModel();
57765750

57775751
cm.discriminator = createDiscriminator("", (Schema) cs);
5778-
5779-
for (Schema o : Optional.ofNullable(cs.getOneOf()).orElse(Collections.emptyList())) {
5752+
for (Schema o : cs.getOneOf()) {
57805753
if (o.get$ref() == null) {
57815754
if (cm.discriminator != null && o.get$ref() == null) {
57825755
// OpenAPI spec states that inline objects should not be considered when discriminator is used

modules/openapi-generator/src/main/resources/JavaSpring/oneof_interface.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ public interface {{classname}} {{#vendorExtensions.x-implements}}{{#-first}}exte
33
{{#discriminator}}
44
public {{propertyType}} {{propertyGetter}}();
55
{{/discriminator}}
6-
}
6+
}

0 commit comments

Comments
 (0)