Adds ComposedSchema to store schema composed schemas (#10653)

* Adds ComposedSchema and the ability to set it in CodegenModel and CodegenProperty

* Adds ComposedSchemas class and adds getters and setters for it in schema implementors

* Adds and uses getComposedSchemas

* Makes method private

* Uses setComposedSchemas for CodegenParameter and CodegenResponse

* Samples regeneratoed, tweaked string representation

* Removes null default

* Removes anyOfProps, oneOfProps, allOfProps

* Removes unneeded line
This commit is contained in:
Justin Black 2021-10-26 12:49:32 -07:00 committed by GitHub
parent 8d490835b7
commit 3a667784ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 168 additions and 30 deletions

View File

@ -0,0 +1,66 @@
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen;
import java.util.*;
public class CodegenComposedSchemas {
private List<CodegenProperty> allOf;
private List<CodegenProperty> oneOf;
private List<CodegenProperty> anyOf;
public CodegenComposedSchemas(List<CodegenProperty> allOf, List<CodegenProperty> oneOf, List<CodegenProperty> anyOf) {
this.allOf = allOf;
this.oneOf = oneOf;
this.anyOf = anyOf;
}
public List<CodegenProperty> getAllOf() {
return allOf;
}
public List<CodegenProperty> getOneOf() {
return oneOf;
}
public List<CodegenProperty> getAnyOf() {
return anyOf;
}
public String toString() {
final StringBuilder sb = new StringBuilder("CodegenComposedSchemas{");
sb.append("oneOf=").append(oneOf);
sb.append(", anyOf=").append(anyOf);
sb.append(", allOf=").append(allOf);
sb.append('}');
return sb.toString();
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CodegenComposedSchemas that = (CodegenComposedSchemas) o;
return Objects.equals(oneOf, that.oneOf) &&
Objects.equals(anyOf, that.anyOf) &&
Objects.equals(allOf, that.allOf);
}
@Override
public int hashCode() {
return Objects.hash(oneOf, anyOf, allOf);
}
}

View File

@ -49,11 +49,6 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public Set<String> oneOf = new TreeSet<String>(); public Set<String> oneOf = new TreeSet<String>();
public Set<String> allOf = new TreeSet<String>(); public Set<String> allOf = new TreeSet<String>();
// anyOf, oneOf, allOf with full properties/tags (e.g. isString, etc)
public List<CodegenProperty> anyOfProps = new ArrayList<>();
public List<CodegenProperty> allOfProps = new ArrayList<>();
public List<CodegenProperty> oneOfProps = new ArrayList<>();
// The schema name as written in the OpenAPI document. // The schema name as written in the OpenAPI document.
public String name; public String name;
// The language-specific name of the class that implements this schema. // The language-specific name of the class that implements this schema.
@ -110,6 +105,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
public ExternalDocumentation externalDocumentation; public ExternalDocumentation externalDocumentation;
public Map<String, Object> vendorExtensions = new HashMap<String, Object>(); public Map<String, Object> vendorExtensions = new HashMap<String, Object>();
private CodegenComposedSchemas composedSchemas;
/** /**
* The type of the value for the additionalProperties keyword in the OAS document. * The type of the value for the additionalProperties keyword in the OAS document.
@ -810,6 +806,16 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
this.isAnyType = isAnyType; this.isAnyType = isAnyType;
} }
@Override
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
this.composedSchemas = composedSchemas;
}
@Override
public CodegenComposedSchemas getComposedSchemas() {
return composedSchemas;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
@ -849,6 +855,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
getUniqueItems() == that.getUniqueItems() && getUniqueItems() == that.getUniqueItems() &&
getExclusiveMinimum() == that.getExclusiveMinimum() && getExclusiveMinimum() == that.getExclusiveMinimum() &&
getExclusiveMaximum() == that.getExclusiveMaximum() && getExclusiveMaximum() == that.getExclusiveMaximum() &&
Objects.equals(composedSchemas, that.composedSchemas) &&
Objects.equals(parent, that.parent) && Objects.equals(parent, that.parent) &&
Objects.equals(parentSchema, that.parentSchema) && Objects.equals(parentSchema, that.parentSchema) &&
Objects.equals(interfaces, that.interfaces) && Objects.equals(interfaces, that.interfaces) &&
@ -856,9 +863,6 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
Objects.equals(parentModel, that.parentModel) && Objects.equals(parentModel, that.parentModel) &&
Objects.equals(interfaceModels, that.interfaceModels) && Objects.equals(interfaceModels, that.interfaceModels) &&
Objects.equals(children, that.children) && Objects.equals(children, that.children) &&
Objects.equals(anyOf, that.anyOfProps) &&
Objects.equals(oneOf, that.oneOfProps) &&
Objects.equals(allOf, that.allOfProps) &&
Objects.equals(anyOf, that.anyOf) && Objects.equals(anyOf, that.anyOf) &&
Objects.equals(oneOf, that.oneOf) && Objects.equals(oneOf, that.oneOf) &&
Objects.equals(allOf, that.allOf) && Objects.equals(allOf, that.allOf) &&
@ -921,8 +925,8 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
getAdditionalPropertiesType(), getMaxProperties(), getMinProperties(), getUniqueItems(), getMaxItems(), getAdditionalPropertiesType(), getMaxProperties(), getMinProperties(), getUniqueItems(), getMaxItems(),
getMinItems(), getMaxLength(), getMinLength(), getExclusiveMinimum(), getExclusiveMaximum(), getMinimum(), getMinItems(), getMaxLength(), getMinLength(), getExclusiveMinimum(), getExclusiveMaximum(), getMinimum(),
getMaximum(), getPattern(), getMultipleOf(), getItems(), getAdditionalProperties(), getIsModel(), getMaximum(), getPattern(), getMultipleOf(), getItems(), getAdditionalProperties(), getIsModel(),
getAdditionalPropertiesIsAnyType(), hasDiscriminatorWithNonEmptyMapping, anyOfProps, oneOfProps, allOfProps, getAdditionalPropertiesIsAnyType(), hasDiscriminatorWithNonEmptyMapping,
isAnyType); isAnyType, getComposedSchemas());
} }
@Override @Override
@ -938,9 +942,6 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
sb.append(", anyOf=").append(anyOf); sb.append(", anyOf=").append(anyOf);
sb.append(", oneOf=").append(oneOf); sb.append(", oneOf=").append(oneOf);
sb.append(", allOf=").append(allOf); sb.append(", allOf=").append(allOf);
sb.append(", anyOf=").append(anyOfProps);
sb.append(", oneOf=").append(oneOfProps);
sb.append(", allOf=").append(allOfProps);
sb.append(", name='").append(name).append('\''); sb.append(", name='").append(name).append('\'');
sb.append(", classname='").append(classname).append('\''); sb.append(", classname='").append(classname).append('\'');
sb.append(", title='").append(title).append('\''); sb.append(", title='").append(title).append('\'');
@ -1017,6 +1018,7 @@ public class CodegenModel implements IJsonSchemaValidationProperties {
sb.append(", getAdditionalPropertiesIsAnyType=").append(getAdditionalPropertiesIsAnyType()); sb.append(", getAdditionalPropertiesIsAnyType=").append(getAdditionalPropertiesIsAnyType());
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping); sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", getIsAnyType=").append(getIsAnyType()); sb.append(", getIsAnyType=").append(getIsAnyType());
sb.append(", composedSchemas=").append(composedSchemas);
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }

View File

@ -107,6 +107,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public boolean isNull; public boolean isNull;
private boolean hasRequired; private boolean hasRequired;
private boolean hasDiscriminatorWithNonEmptyMapping; private boolean hasDiscriminatorWithNonEmptyMapping;
private CodegenComposedSchemas composedSchemas;
public CodegenParameter copy() { public CodegenParameter copy() {
CodegenParameter output = new CodegenParameter(); CodegenParameter output = new CodegenParameter();
@ -159,6 +160,9 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
output.setHasRequired(this.hasRequired); output.setHasRequired(this.hasRequired);
output.setHasDiscriminatorWithNonEmptyMapping(this.hasDiscriminatorWithNonEmptyMapping); output.setHasDiscriminatorWithNonEmptyMapping(this.hasDiscriminatorWithNonEmptyMapping);
if (this.composedSchemas != null) {
output.setComposedSchemas(this.getComposedSchemas());
}
if (this._enum != null) { if (this._enum != null) {
output._enum = new ArrayList<String>(this._enum); output._enum = new ArrayList<String>(this._enum);
} }
@ -216,7 +220,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _enum, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger, hasDiscriminatorWithNonEmptyMapping); return Objects.hash(isFormParam, isQueryParam, isPathParam, isHeaderParam, isCookieParam, isBodyParam, isContainer, isCollectionFormatMulti, isPrimitiveType, isModel, isExplode, baseName, paramName, dataType, datatypeWithEnum, dataFormat, collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName, style, isDeepObject, isAllowEmptyValue, example, jsonSchema, isString, isNumeric, isInteger, isLong, isNumber, isFloat, isDouble, isDecimal, isByteArray, isBinary, isBoolean, isDate, isDateTime, isUuid, isUri, isEmail, isFreeFormObject, isAnyType, isArray, isMap, isFile, isEnum, _enum, allowableValues, items, mostInnerItems, additionalProperties, vars, requiredVars, vendorExtensions, hasValidation, getMaxProperties(), getMinProperties(), isNullable, isDeprecated, required, getMaximum(), getExclusiveMaximum(), getMinimum(), getExclusiveMinimum(), getMaxLength(), getMinLength(), getPattern(), getMaxItems(), getMinItems(), getUniqueItems(), contentType, multipleOf, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired, isShort, isUnboundedInteger, hasDiscriminatorWithNonEmptyMapping, composedSchemas);
} }
@Override @Override
@ -271,6 +275,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
getExclusiveMaximum() == that.getExclusiveMaximum() && getExclusiveMaximum() == that.getExclusiveMaximum() &&
getExclusiveMinimum() == that.getExclusiveMinimum() && getExclusiveMinimum() == that.getExclusiveMinimum() &&
getUniqueItems() == that.getUniqueItems() && getUniqueItems() == that.getUniqueItems() &&
Objects.equals(composedSchemas, that.getComposedSchemas()) &&
Objects.equals(baseName, that.baseName) && Objects.equals(baseName, that.baseName) &&
Objects.equals(paramName, that.paramName) && Objects.equals(paramName, that.paramName) &&
Objects.equals(dataType, that.dataType) && Objects.equals(dataType, that.dataType) &&
@ -393,6 +398,7 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
sb.append(", getHasVars=").append(hasVars); sb.append(", getHasVars=").append(hasVars);
sb.append(", getHasRequired=").append(hasRequired); sb.append(", getHasRequired=").append(hasRequired);
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping); sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", composedSchemas=").append(composedSchemas);
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }
@ -706,5 +712,15 @@ public class CodegenParameter implements IJsonSchemaValidationProperties {
public void setIsAnyType(boolean isAnyType) { public void setIsAnyType(boolean isAnyType) {
this.isAnyType = isAnyType; this.isAnyType = isAnyType;
} }
@Override
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
this.composedSchemas = composedSchemas;
}
@Override
public CodegenComposedSchemas getComposedSchemas() {
return composedSchemas;
}
} }

View File

@ -191,6 +191,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
private boolean hasVars; private boolean hasVars;
private boolean hasRequired; private boolean hasRequired;
private boolean hasDiscriminatorWithNonEmptyMapping; private boolean hasDiscriminatorWithNonEmptyMapping;
private CodegenComposedSchemas composedSchemas = null;
public String getBaseName() { public String getBaseName() {
return baseName; return baseName;
@ -614,6 +615,16 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
this.xmlNamespace = xmlNamespace; this.xmlNamespace = xmlNamespace;
} }
@Override
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
this.composedSchemas = composedSchemas;
}
@Override
public CodegenComposedSchemas getComposedSchemas() {
return composedSchemas;
}
@Override @Override
public CodegenProperty clone() { public CodegenProperty clone() {
try { try {
@ -642,6 +653,9 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
if (this.vendorExtensions != null) { if (this.vendorExtensions != null) {
cp.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions); cp.vendorExtensions = new HashMap<String, Object>(this.vendorExtensions);
} }
if (this.composedSchemas != null) {
cp.composedSchemas = this.composedSchemas;
}
return cp; return cp;
} catch (CloneNotSupportedException e) { } catch (CloneNotSupportedException e) {
@ -882,6 +896,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
sb.append(", getHasVars=").append(getHasVars()); sb.append(", getHasVars=").append(getHasVars());
sb.append(", getHasRequired=").append(getHasRequired()); sb.append(", getHasRequired=").append(getHasRequired());
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping); sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", composedSchemas=").append(composedSchemas);
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }
@ -937,6 +952,7 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() && getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() && getHasVars() == that.getHasVars() &&
getHasRequired() ==that.getHasRequired() && getHasRequired() ==that.getHasRequired() &&
Objects.equals(composedSchemas, that.composedSchemas) &&
Objects.equals(openApiType, that.openApiType) && Objects.equals(openApiType, that.openApiType) &&
Objects.equals(baseName, that.baseName) && Objects.equals(baseName, that.baseName) &&
Objects.equals(complexType, that.complexType) && Objects.equals(complexType, that.complexType) &&
@ -999,6 +1015,6 @@ public class CodegenProperty implements Cloneable, IJsonSchemaValidationProperti
vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase, vendorExtensions, hasValidation, isInherited, discriminatorValue, nameInCamelCase,
nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName, nameInSnakeCase, enumName, maxItems, minItems, isXmlAttribute, xmlPrefix, xmlName,
xmlNamespace, isXmlWrapped, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired, xmlNamespace, isXmlWrapped, isNull, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping); hasDiscriminatorWithNonEmptyMapping, composedSchemas);
} }
} }

View File

@ -85,6 +85,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
private boolean hasVars; private boolean hasVars;
private boolean hasRequired; private boolean hasRequired;
private boolean hasDiscriminatorWithNonEmptyMapping; private boolean hasDiscriminatorWithNonEmptyMapping;
private CodegenComposedSchemas composedSchemas;
@Override @Override
public int hashCode() { public int hashCode() {
@ -96,7 +97,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(), getMaxProperties(), getMinProperties(), uniqueItems, getMaxItems(), getMinItems(), getMaxLength(),
getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(), getMinLength(), exclusiveMinimum, exclusiveMaximum, getMinimum(), getMaximum(), getPattern(),
is1xx, is2xx, is3xx, is4xx, is5xx, additionalPropertiesIsAnyType, hasVars, hasRequired, is1xx, is2xx, is3xx, is4xx, is5xx, additionalPropertiesIsAnyType, hasVars, hasRequired,
hasDiscriminatorWithNonEmptyMapping); hasDiscriminatorWithNonEmptyMapping, composedSchemas);
} }
@Override @Override
@ -144,6 +145,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() && getAdditionalPropertiesIsAnyType() == that.getAdditionalPropertiesIsAnyType() &&
getHasVars() == that.getHasVars() && getHasVars() == that.getHasVars() &&
getHasRequired() == that.getHasRequired() && getHasRequired() == that.getHasRequired() &&
Objects.equals(composedSchemas, that.getComposedSchemas()) &&
Objects.equals(vars, that.vars) && Objects.equals(vars, that.vars) &&
Objects.equals(requiredVars, that.requiredVars) && Objects.equals(requiredVars, that.requiredVars) &&
Objects.equals(headers, that.headers) && Objects.equals(headers, that.headers) &&
@ -482,6 +484,7 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
sb.append(", getHasVars=").append(hasVars); sb.append(", getHasVars=").append(hasVars);
sb.append(", getHasRequired=").append(hasRequired); sb.append(", getHasRequired=").append(hasRequired);
sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping); sb.append(", getHasDiscriminatorWithNonEmptyMapping=").append(hasDiscriminatorWithNonEmptyMapping);
sb.append(", composedSchemas=").append(composedSchemas);
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }
@ -570,4 +573,14 @@ public class CodegenResponse implements IJsonSchemaValidationProperties {
public void setIsAnyType(boolean isAnyType) { public void setIsAnyType(boolean isAnyType) {
this.isAnyType = isAnyType; this.isAnyType = isAnyType;
} }
@Override
public void setComposedSchemas(CodegenComposedSchemas composedSchemas) {
this.composedSchemas = composedSchemas;
}
@Override
public CodegenComposedSchemas getComposedSchemas() {
return composedSchemas;
}
} }

View File

@ -2440,9 +2440,6 @@ public class DefaultCodegen implements CodegenConfig {
// interfaces (schemas defined in allOf, anyOf, oneOf) // interfaces (schemas defined in allOf, anyOf, oneOf)
List<Schema> interfaces = ModelUtils.getInterfaces(composed); List<Schema> interfaces = ModelUtils.getInterfaces(composed);
List<CodegenProperty> anyOfProps = new ArrayList<>();
List<CodegenProperty> allOfProps = new ArrayList<>();
List<CodegenProperty> oneOfProps = new ArrayList<>();
if (!interfaces.isEmpty()) { if (!interfaces.isEmpty()) {
// m.interfaces is for backward compatibility // m.interfaces is for backward compatibility
if (m.interfaces == null) if (m.interfaces == null)
@ -2467,7 +2464,6 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.warn("{} (anyOf schema) already has `{}` defined and therefore it's skipped.", m.name, languageType); LOGGER.warn("{} (anyOf schema) already has `{}` defined and therefore it's skipped.", m.name, languageType);
} else { } else {
m.anyOf.add(languageType); m.anyOf.add(languageType);
anyOfProps.add(interfaceProperty);
} }
} else if (composed.getOneOf() != null) { } else if (composed.getOneOf() != null) {
@ -2475,7 +2471,6 @@ public class DefaultCodegen implements CodegenConfig {
LOGGER.warn("{} (oneOf schema) already has `{}` defined and therefore it's skipped.", m.name, languageType); LOGGER.warn("{} (oneOf schema) already has `{}` defined and therefore it's skipped.", m.name, languageType);
} else { } else {
m.oneOf.add(languageType); m.oneOf.add(languageType);
oneOfProps.add(interfaceProperty);
} }
} else if (composed.getAllOf() != null) { } else if (composed.getAllOf() != null) {
// no need to add primitive type to allOf, which should comprise of schemas (models) only // no need to add primitive type to allOf, which should comprise of schemas (models) only
@ -2511,23 +2506,16 @@ public class DefaultCodegen implements CodegenConfig {
if (composed.getAnyOf() != null) { if (composed.getAnyOf() != null) {
m.anyOf.add(modelName); m.anyOf.add(modelName);
anyOfProps.add(interfaceProperty);
} else if (composed.getOneOf() != null) { } else if (composed.getOneOf() != null) {
m.oneOf.add(modelName); m.oneOf.add(modelName);
oneOfProps.add(interfaceProperty);
} else if (composed.getAllOf() != null) { } else if (composed.getAllOf() != null) {
m.allOf.add(modelName); m.allOf.add(modelName);
allOfProps.add(interfaceProperty);
} else { } else {
LOGGER.error("Composed schema has incorrect anyOf, allOf, oneOf defined: {}", composed); LOGGER.error("Composed schema has incorrect anyOf, allOf, oneOf defined: {}", composed);
} }
} }
} }
m.oneOfProps = oneOfProps;
m.allOfProps = allOfProps;
m.anyOfProps = anyOfProps;
if (parent != null && composed.getAllOf() != null) { // set parent for allOf only if (parent != null && composed.getAllOf() != null) { // set parent for allOf only
m.parentSchema = parentName; m.parentSchema = parentName;
m.parent = toModelName(parentName); m.parent = toModelName(parentName);
@ -2696,6 +2684,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
m.setTypeProperties(schema); m.setTypeProperties(schema);
m.setComposedSchemas(getComposedSchemas(schema));
if (ModelUtils.isArraySchema(schema)) { if (ModelUtils.isArraySchema(schema)) {
CodegenProperty arrayProperty = fromProperty(name, schema); CodegenProperty arrayProperty = fromProperty(name, schema);
m.setItems(arrayProperty.items); m.setItems(arrayProperty.items);
@ -3514,6 +3503,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
property.setTypeProperties(p); property.setTypeProperties(p);
property.setComposedSchemas(getComposedSchemas(p));
if (ModelUtils.isIntegerSchema(p)) { // integer type if (ModelUtils.isIntegerSchema(p)) { // integer type
property.isNumeric = Boolean.TRUE; property.isNumeric = Boolean.TRUE;
if (ModelUtils.isLongSchema(p)) { // int64/long format if (ModelUtils.isLongSchema(p)) { // int64/long format
@ -4265,6 +4255,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
r.setTypeProperties(responseSchema); r.setTypeProperties(responseSchema);
r.setComposedSchemas(getComposedSchemas(responseSchema));
if (ModelUtils.isArraySchema(responseSchema)) { if (ModelUtils.isArraySchema(responseSchema)) {
r.simpleType = false; r.simpleType = false;
r.containerType = cp.containerType; r.containerType = cp.containerType;
@ -4547,6 +4538,7 @@ public class DefaultCodegen implements CodegenConfig {
} }
ModelUtils.syncValidationProperties(parameterSchema, codegenParameter); ModelUtils.syncValidationProperties(parameterSchema, codegenParameter);
codegenParameter.setTypeProperties(parameterSchema); codegenParameter.setTypeProperties(parameterSchema);
codegenParameter.setComposedSchemas(getComposedSchemas(parameterSchema));
if (Boolean.TRUE.equals(parameterSchema.getNullable())) { // use nullable defined in the spec if (Boolean.TRUE.equals(parameterSchema.getNullable())) { // use nullable defined in the spec
codegenParameter.isNullable = true; codegenParameter.isNullable = true;
@ -6156,6 +6148,7 @@ public class DefaultCodegen implements CodegenConfig {
Schema ps = unaliasSchema(propertySchema, importMapping); Schema ps = unaliasSchema(propertySchema, importMapping);
ModelUtils.syncValidationProperties(ps, codegenParameter); ModelUtils.syncValidationProperties(ps, codegenParameter);
codegenParameter.setTypeProperties(ps); codegenParameter.setTypeProperties(ps);
codegenParameter.setComposedSchemas(getComposedSchemas(ps));
if (ps.getPattern() != null) { if (ps.getPattern() != null) {
codegenParameter.pattern = toRegularExpression(ps.getPattern()); codegenParameter.pattern = toRegularExpression(ps.getPattern());
} }
@ -6590,6 +6583,7 @@ public class DefaultCodegen implements CodegenConfig {
ModelUtils.syncValidationProperties(unaliasedSchema, codegenParameter); ModelUtils.syncValidationProperties(unaliasedSchema, codegenParameter);
codegenParameter.setTypeProperties(unaliasedSchema); codegenParameter.setTypeProperties(unaliasedSchema);
codegenParameter.setComposedSchemas(getComposedSchemas(unaliasedSchema));
// TODO in the future switch al the below schema usages to unaliasedSchema // TODO in the future switch al the below schema usages to unaliasedSchema
// because it keeps models as refs and will not get their referenced schemas // because it keeps models as refs and will not get their referenced schemas
if (ModelUtils.isArraySchema(schema)) { if (ModelUtils.isArraySchema(schema)) {
@ -7154,4 +7148,30 @@ public class DefaultCodegen implements CodegenConfig {
protected String getCollectionFormat(CodegenParameter codegenParameter) { protected String getCollectionFormat(CodegenParameter codegenParameter) {
return null; return null;
} }
private CodegenComposedSchemas getComposedSchemas(Schema schema) {
if (!(schema instanceof ComposedSchema)) {
return null;
}
ComposedSchema cs = (ComposedSchema) schema;
return new CodegenComposedSchemas(
getComposedProperties(cs.getAllOf(), "allOf"),
getComposedProperties(cs.getOneOf(), "oneOf"),
getComposedProperties(cs.getAnyOf(), "anyOf")
);
}
private List<CodegenProperty> getComposedProperties(List<Schema> xOfCollection, String collectionName) {
if (xOfCollection == null) {
return null;
}
List<CodegenProperty> xOf = new ArrayList<>();
int i = 0;
for (Schema xOfSchema: xOfCollection) {
CodegenProperty cp = fromProperty(collectionName + "_" + String.valueOf(i), xOfSchema);
xOf.add(cp);
i += 1;
}
return xOf;
}
} }

View File

@ -145,6 +145,11 @@ public interface IJsonSchemaValidationProperties {
void setIsAnyType(boolean isAnyType); void setIsAnyType(boolean isAnyType);
CodegenComposedSchemas getComposedSchemas();
void setComposedSchemas(CodegenComposedSchemas composedSchemas);
/** /**
* Syncs all the schema's type properties into the IJsonSchemaValidationProperties instance * Syncs all the schema's type properties into the IJsonSchemaValidationProperties instance
* for now this only supports types without format information * for now this only supports types without format information

View File

@ -539,10 +539,10 @@ public interface PathHandlerInterface {
* <p><b>Response headers</b>: [CodegenProperty{openApiType='integer', baseName='X-Rate-Limit', complexType='null', getter='getxRateLimit', setter='setxRateLimit', description='calls per hour allowed by the user', dataType='Integer', datatypeWithEnum='Integer', dataFormat='int32', name='xRateLimit', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Rate-Limit;', baseType='Integer', containerType='null', title='null', unescapedDescription='calls per hour allowed by the user', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{ * <p><b>Response headers</b>: [CodegenProperty{openApiType='integer', baseName='X-Rate-Limit', complexType='null', getter='getxRateLimit', setter='setxRateLimit', description='calls per hour allowed by the user', dataType='Integer', datatypeWithEnum='Integer', dataFormat='int32', name='xRateLimit', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Rate-Limit;', baseType='Integer', containerType='null', title='null', unescapedDescription='calls per hour allowed by the user', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{
"type" : "integer", "type" : "integer",
"format" : "int32" "format" : "int32"
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=true, isModel=false, isContainer=false, isString=false, isNumeric=true, isInteger=true, isShort=true, isLong=false, isUnboundedInteger=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=false, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XRateLimit', nameInSnakeCase='X_RATE_LIMIT', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false, getHasRequired=false, getHasDiscriminatorWithNonEmptyMapping=false}, CodegenProperty{openApiType='string', baseName='X-Expires-After', complexType='Date', getter='getxExpiresAfter', setter='setxExpiresAfter', description='date in UTC when token expires', dataType='Date', datatypeWithEnum='Date', dataFormat='date-time', name='xExpiresAfter', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Expires-After;', baseType='Date', containerType='null', title='null', unescapedDescription='date in UTC when token expires', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{ }', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=true, isModel=false, isContainer=false, isString=false, isNumeric=true, isInteger=true, isShort=true, isLong=false, isUnboundedInteger=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=false, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XRateLimit', nameInSnakeCase='X_RATE_LIMIT', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false, getHasRequired=false, getHasDiscriminatorWithNonEmptyMapping=false, composedSchemas=null}, CodegenProperty{openApiType='string', baseName='X-Expires-After', complexType='Date', getter='getxExpiresAfter', setter='setxExpiresAfter', description='date in UTC when token expires', dataType='Date', datatypeWithEnum='Date', dataFormat='date-time', name='xExpiresAfter', min='null', max='null', defaultValue='null', defaultValueWithParam=' = data.X-Expires-After;', baseType='Date', containerType='null', title='null', unescapedDescription='date in UTC when token expires', maxLength=null, minLength=null, pattern='null', example='null', jsonSchema='{
"type" : "string", "type" : "string",
"format" : "date-time" "format" : "date-time"
}', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=false, isModel=false, isContainer=false, isString=false, isNumeric=false, isInteger=false, isShort=false, isLong=false, isUnboundedInteger=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=true, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XExpiresAfter', nameInSnakeCase='X_EXPIRES_AFTER', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false, getHasRequired=false, getHasDiscriminatorWithNonEmptyMapping=false}]</p> }', minimum='null', maximum='null', exclusiveMinimum=false, exclusiveMaximum=false, required=false, deprecated=false, hasMoreNonReadOnly=false, isPrimitiveType=false, isModel=false, isContainer=false, isString=false, isNumeric=false, isInteger=false, isShort=false, isLong=false, isUnboundedInteger=false, isNumber=false, isFloat=false, isDouble=false, isDecimal=false, isByteArray=false, isBinary=false, isFile=false, isBoolean=false, isDate=false, isDateTime=true, isUuid=false, isUri=false, isEmail=false, isFreeFormObject=false, isArray=false, isMap=false, isEnum=false, isReadOnly=false, isWriteOnly=false, isNullable=false, isSelfReference=false, isCircularReference=false, isDiscriminator=false, _enum=null, allowableValues=null, items=null, additionalProperties=null, vars=[], requiredVars=[], mostInnerItems=null, vendorExtensions={}, hasValidation=false, isInherited=false, discriminatorValue='null', nameInCamelCase='XExpiresAfter', nameInSnakeCase='X_EXPIRES_AFTER', enumName='null', maxItems=null, minItems=null, maxProperties=null, minProperties=null, uniqueItems=false, multipleOf=null, isXmlAttribute=false, xmlPrefix='null', xmlName='null', xmlNamespace='null', isXmlWrapped=false, isNull=false, getAdditionalPropertiesIsAnyType=false, getHasVars=false, getHasRequired=false, getHasDiscriminatorWithNonEmptyMapping=false, composedSchemas=null}]</p>
* *
* <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p> * <p><b>Produces</b>: [{mediaType=application/xml}, {mediaType=application/json}]</p>
* <p><b>Returns</b>: {@link String}</p> * <p><b>Returns</b>: {@link String}</p>