Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generation of oneOf interfaces for oneOf usage in properties #5400

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,34 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
}
}

// 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)) {
addOneOfNameExtension((ComposedSchema) s, n);
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)) {
Expand Down Expand Up @@ -5709,15 +5730,19 @@ public void addOneOfNameExtension(ComposedSchema s, String name) {
}

/**
* 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
Expand Down