Skip to content
Merged
Show file tree
Hide file tree
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 @@ -385,7 +385,7 @@ private Schema generateNestedSchema(Schema schema, Set<Schema> visitedSchemas) {
if(ModelUtils.isArraySchema(schema)) {
Schema itemsSchema = ModelUtils.getSchemaItems(schema);
itemsSchema = ModelUtils.getReferencedSchema(openAPI, itemsSchema);
if(ModelUtils.isModel(itemsSchema)) {
if(ModelUtils.isModel(itemsSchema) || (itemsSchema != null && ModelUtils.isEnumSchema(itemsSchema))) {
String newSchemaName = ModelUtils.getSimpleRef(ModelUtils.getSchemaItems(schema).get$ref()) + ARRAY_SUFFIX;
return addSchemas(schema, newSchemaName, visitedSchemas);
}else if (ModelUtils.isPrimitiveType(itemsSchema)){
Expand All @@ -400,7 +400,7 @@ private Schema generateNestedSchema(Schema schema, Set<Schema> visitedSchemas) {
} else if(ModelUtils.isMapSchema(schema)) {
Schema mapValueSchema = ModelUtils.getAdditionalProperties(schema);
mapValueSchema = ModelUtils.getReferencedSchema(openAPI, mapValueSchema);
if(ModelUtils.isModel(mapValueSchema) ) {
if(ModelUtils.isModel(mapValueSchema) || (mapValueSchema != null && ModelUtils.isEnumSchema(mapValueSchema))) {
String newSchemaName = ModelUtils.getSimpleRef(ModelUtils.getAdditionalProperties(schema).get$ref()) + MAP_SUFFIX;
return addSchemas(schema, newSchemaName, visitedSchemas);
}else if (ModelUtils.isPrimitiveType(mapValueSchema)){
Expand Down Expand Up @@ -1116,16 +1116,16 @@ protected void addProperties(Map<String, Schema> properties, List<String> requir
} else {
Schema additionalProperties = ModelUtils.getAdditionalProperties(schema);
if(additionalProperties == null) {
return;
return;
} else if (additionalProperties.getTitle() != null) {
addtionalPropertiesName = additionalProperties.getTitle();
} else if (additionalProperties.get$ref() != null) {
String ref = ModelUtils.getSimpleRef(additionalProperties.get$ref());
addtionalPropertiesName = toVarName(toModelName(ref));
}
}
properties.put(addtionalPropertiesName, schema);

properties.put(addtionalPropertiesName, schema);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.openapitools.codegen.protobuf;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.media.MapSchema;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.openapitools.codegen.ClientOptInput;
Expand Down Expand Up @@ -296,4 +299,50 @@ public void unspecifiedEnumValuesIgnoredIfAlreadyPresent() {
Assert.assertEquals(enumVars1.get(1).get("value"), "FOO");
Assert.assertEquals(enumVars1.get(1).get("isString"), false);
}

@SuppressWarnings("unchecked")
@Test(description = "Validate that enums in arrays are treated as complex types")
public void enumInArrayIsTreatedAsComplexType() {
final Schema enumSchema = new StringSchema()._enum(Arrays.asList("APPLE", "BANANA", "ORANGE"));
final ArraySchema arraySchema = new ArraySchema();
arraySchema.setItems(enumSchema);

final Schema model = new Schema()
.description("a sample model with enum array")
.addProperties("fruitList", arraySchema);

final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen();
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
codegen.setOpenAPI(openAPI);
final CodegenModel cm = codegen.fromModel("sample", model);
codegen.additionalProperties().put(USE_SIMPLIFIED_ENUM_NAMES, true);
codegen.processOpts();
codegen.postProcessModels(createCodegenModelWrapper(cm));

final CodegenProperty property = cm.vars.get(0);
Assert.assertEquals(property.baseName, "fruitList");
}

@SuppressWarnings("unchecked")
@Test(description = "Validate that enums in maps are treated as complex types")
public void enumInMapIsTreatedAsComplexType() {
final Schema enumSchema = new StringSchema()._enum(Arrays.asList("RED", "GREEN", "BLUE"));
final MapSchema mapSchema = new MapSchema();
mapSchema.setAdditionalProperties(enumSchema);

final Schema model = new Schema()
.description("a sample model with enum map")
.addProperties("colorMap", mapSchema);

final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen();
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model);
codegen.setOpenAPI(openAPI);
final CodegenModel cm = codegen.fromModel("sample", model);
codegen.additionalProperties().put(USE_SIMPLIFIED_ENUM_NAMES, true);
codegen.processOpts();
codegen.postProcessModels(createCodegenModelWrapper(cm));

final CodegenProperty property = cm.vars.get(0);
Assert.assertEquals(property.baseName, "colorMap");
}
}
Loading