forked from loafle/openapi-generator-original
Fix generation of oneOf interfaces for oneOf usage in properties (#5400)
* Fix generation of oneOf interfaces for oneOf usage in properties * Only generate oneOf interface models when oneOf is defined
This commit is contained in:
parent
f740379cfa
commit
bb38bb0626
@ -744,13 +744,34 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// also add all properties of all schemas to be checked for oneOf
|
||||
Map<String, Schema> propertySchemas = new HashMap<String, Schema>();
|
||||
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
|
||||
Schema s = e.getValue();
|
||||
Map<String, Schema> props = s.getProperties();
|
||||
if (props == null) {
|
||||
props = new HashMap<String, Schema>();
|
||||
}
|
||||
for (Map.Entry<String, Schema> p : props.entrySet()) {
|
||||
propertySchemas.put(e.getKey() + "/" + p.getKey(), p.getValue());
|
||||
}
|
||||
}
|
||||
schemas.putAll(propertySchemas);
|
||||
|
||||
// go through all gathered schemas and add them as interfaces to be created
|
||||
for (Map.Entry<String, Schema> e : schemas.entrySet()) {
|
||||
String n = toModelName(e.getKey());
|
||||
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);
|
||||
}
|
||||
} else if (ModelUtils.isArraySchema(s)) {
|
||||
Schema items = ((ArraySchema) s).getItems();
|
||||
if (ModelUtils.isComposedSchema(items)) {
|
||||
@ -5726,15 +5747,19 @@ public class DefaultCodegen implements CodegenConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a given ComposedSchema as an interface model to be generated
|
||||
* Add a given ComposedSchema as an interface model to be generated, assuming it has `oneOf` defined
|
||||
* @param cs ComposedSchema object to create as interface model
|
||||
* @param type name to use for the generated interface model
|
||||
*/
|
||||
public void addOneOfInterfaceModel(ComposedSchema cs, String type) {
|
||||
if (cs.getOneOf() == null) {
|
||||
return;
|
||||
}
|
||||
CodegenModel cm = new CodegenModel();
|
||||
|
||||
cm.discriminator = createDiscriminator("", (Schema) cs);
|
||||
for (Schema o : cs.getOneOf()) {
|
||||
|
||||
for (Schema o : Optional.ofNullable(cs.getOneOf()).orElse(Collections.emptyList())) {
|
||||
if (o.get$ref() == null) {
|
||||
if (cm.discriminator != null && o.get$ref() == null) {
|
||||
// OpenAPI spec states that inline objects should not be considered when discriminator is used
|
||||
|
Loading…
x
Reference in New Issue
Block a user