Skip to content

Commit 64abd53

Browse files
authored
Merge pull request #989 from swagger-api/codegen-issue-11224
Codegen issue 11224
2 parents 5c6b33c + b284095 commit 64abd53

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

src/main/java/io/swagger/codegen/v3/generators/DefaultCodegenConfig.java

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,36 +1578,62 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
15781578
codegenProperty.name = toVarName(name);
15791579
codegenProperty.baseName = name;
15801580
codegenProperty.nameInCamelCase = camelize(codegenProperty.name, false);
1581-
codegenProperty.description = escapeText(propertySchema.getDescription());
1582-
codegenProperty.unescapedDescription = propertySchema.getDescription();
1583-
codegenProperty.title = propertySchema.getTitle();
15841581
codegenProperty.getter = toGetter(name);
15851582
codegenProperty.setter = toSetter(name);
1586-
String example = toExampleValue(propertySchema);
1583+
setSchemaProperties(name, codegenProperty, propertySchema);
1584+
1585+
final String type = getSchemaType(propertySchema);
1586+
1587+
processPropertySchemaTypes(name, codegenProperty, propertySchema);
1588+
1589+
codegenProperty.datatype = getTypeDeclaration(propertySchema);
1590+
codegenProperty.dataFormat = propertySchema.getFormat();
1591+
1592+
// this can cause issues for clients which don't support enums
1593+
boolean isEnum = getBooleanValue(codegenProperty, IS_ENUM_EXT_NAME);
1594+
if (isEnum) {
1595+
codegenProperty.datatypeWithEnum = toEnumName(codegenProperty);
1596+
codegenProperty.enumName = toEnumName(codegenProperty);
1597+
} else {
1598+
codegenProperty.datatypeWithEnum = codegenProperty.datatype;
1599+
}
1600+
1601+
codegenProperty.baseType = getSchemaType(propertySchema);
1602+
1603+
processPropertySchemaContainerTypes(codegenProperty, propertySchema, type);
1604+
return codegenProperty;
1605+
}
1606+
1607+
protected void setSchemaProperties(String name, CodegenProperty codegenProperty, Schema schema) {
1608+
codegenProperty.description = escapeText(schema.getDescription());
1609+
codegenProperty.unescapedDescription = schema.getDescription();
1610+
codegenProperty.title = schema.getTitle();
1611+
String example = toExampleValue(schema);
15871612
if(!"null".equals(example)) {
15881613
codegenProperty.example = example;
15891614
}
1590-
codegenProperty.defaultValue = toDefaultValue(propertySchema);
1591-
codegenProperty.defaultValueWithParam = toDefaultValueWithParam(name, propertySchema);
1592-
codegenProperty.jsonSchema = Json.pretty(propertySchema);
1593-
codegenProperty.nullable = Boolean.TRUE.equals(propertySchema.getNullable());
1594-
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_NULLABLE_EXT_NAME, Boolean.TRUE.equals(propertySchema.getNullable()));
1595-
if (propertySchema.getReadOnly() != null) {
1596-
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_READ_ONLY_EXT_NAME, propertySchema.getReadOnly());
1597-
}
1598-
if (propertySchema.getXml() != null) {
1599-
if (propertySchema.getXml().getAttribute() != null) {
1600-
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_XML_ATTRIBUTE_EXT_NAME, propertySchema.getXml().getAttribute());
1615+
codegenProperty.defaultValue = toDefaultValue(schema);
1616+
codegenProperty.defaultValueWithParam = toDefaultValueWithParam(name, schema);
1617+
codegenProperty.jsonSchema = Json.pretty(schema);
1618+
codegenProperty.nullable = Boolean.TRUE.equals(schema.getNullable());
1619+
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_NULLABLE_EXT_NAME, Boolean.TRUE.equals(schema.getNullable()));
1620+
if (schema.getReadOnly() != null) {
1621+
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_READ_ONLY_EXT_NAME, schema.getReadOnly());
1622+
}
1623+
if (schema.getXml() != null) {
1624+
if (schema.getXml().getAttribute() != null) {
1625+
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_XML_ATTRIBUTE_EXT_NAME, schema.getXml().getAttribute());
16011626
}
1602-
codegenProperty.xmlPrefix = propertySchema.getXml().getPrefix();
1603-
codegenProperty.xmlName = propertySchema.getXml().getName();
1604-
codegenProperty.xmlNamespace = propertySchema.getXml().getNamespace();
1627+
codegenProperty.xmlPrefix = schema.getXml().getPrefix();
1628+
codegenProperty.xmlName = schema.getXml().getName();
1629+
codegenProperty.xmlNamespace = schema.getXml().getNamespace();
16051630
}
1606-
if (propertySchema.getExtensions() != null && !propertySchema.getExtensions().isEmpty()) {
1607-
codegenProperty.getVendorExtensions().putAll(propertySchema.getExtensions());
1631+
if (schema.getExtensions() != null && !schema.getExtensions().isEmpty()) {
1632+
codegenProperty.getVendorExtensions().putAll(schema.getExtensions());
16081633
}
1634+
}
16091635

1610-
final String type = getSchemaType(propertySchema);
1636+
protected void processPropertySchemaTypes(String name, CodegenProperty codegenProperty, Schema propertySchema) {
16111637
if (propertySchema instanceof IntegerSchema) {
16121638
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_NUMERIC_EXT_NAME, Boolean.TRUE);
16131639
if(SchemaTypeUtil.INTEGER64_FORMAT.equals(propertySchema.getFormat())) {
@@ -1644,7 +1670,6 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
16441670
codegenProperty.allowableValues = allowableValues;
16451671
}
16461672
}
1647-
16481673
if (propertySchema instanceof StringSchema) {
16491674
codegenProperty.maxLength = propertySchema.getMaxLength();
16501675
codegenProperty.minLength = propertySchema.getMinLength();
@@ -1715,20 +1740,9 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
17151740
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_DATE_TIME_EXT_NAME, Boolean.TRUE);
17161741
handlePropertySchema(propertySchema, codegenProperty);
17171742
}
1718-
codegenProperty.datatype = getTypeDeclaration(propertySchema);
1719-
codegenProperty.dataFormat = propertySchema.getFormat();
1720-
1721-
// this can cause issues for clients which don't support enums
1722-
boolean isEnum = getBooleanValue(codegenProperty, IS_ENUM_EXT_NAME);
1723-
if (isEnum) {
1724-
codegenProperty.datatypeWithEnum = toEnumName(codegenProperty);
1725-
codegenProperty.enumName = toEnumName(codegenProperty);
1726-
} else {
1727-
codegenProperty.datatypeWithEnum = codegenProperty.datatype;
1728-
}
1729-
1730-
codegenProperty.baseType = getSchemaType(propertySchema);
1743+
}
17311744

1745+
protected void processPropertySchemaContainerTypes(CodegenProperty codegenProperty, Schema propertySchema, String type) {
17321746
if (propertySchema instanceof ArraySchema) {
17331747
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_CONTAINER_EXT_NAME, Boolean.TRUE);
17341748
codegenProperty.getVendorExtensions().put(CodegenConstants.IS_LIST_CONTAINER_EXT_NAME, Boolean.TRUE);
@@ -1784,7 +1798,6 @@ public CodegenProperty fromProperty(String name, Schema propertySchema) {
17841798
}
17851799
setNonArrayMapProperty(codegenProperty, type);
17861800
}
1787-
return codegenProperty;
17881801
}
17891802

17901803
private void handleMinMaxValues(Schema propertySchema, CodegenProperty codegenProperty) {

src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.swagger.v3.oas.models.OpenAPI;
2222
import io.swagger.v3.oas.models.Operation;
2323
import io.swagger.v3.oas.models.PathItem;
24+
import io.swagger.v3.oas.models.media.Schema;
2425
import org.apache.commons.lang3.StringUtils;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
@@ -468,6 +469,19 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
468469
}
469470
}
470471

472+
@Override
473+
public CodegenProperty fromProperty(String name, Schema propertySchema) {
474+
CodegenProperty codegenProperty = super.fromProperty(name, propertySchema);
475+
if (propertySchema != null && propertySchema.get$ref() != null) {
476+
Schema refSchema = OpenAPIUtil.getSchemaFromRefSchema(propertySchema, this.openAPI);
477+
if (refSchema != null && !isObjectSchema(refSchema) && refSchema.getEnum() == null) {
478+
setSchemaProperties(name, codegenProperty, refSchema);
479+
processPropertySchemaTypes(name, codegenProperty, refSchema);
480+
}
481+
}
482+
return codegenProperty;
483+
}
484+
471485
@Override
472486
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
473487
if((isDefaultLibrary() || isSpringMvcLibrary()) && !useTags) {

0 commit comments

Comments
 (0)