mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-01 07:53:43 +00:00
[Protobuf] Add isEnumSchema check in generateNestedSchema (#22384)
Co-authored-by: xil <xil@uber.com>
This commit is contained in:
parent
6210db308e
commit
9655c22ff6
@ -385,7 +385,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
||||
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)){
|
||||
@ -400,7 +400,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
|
||||
} 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)){
|
||||
|
||||
@ -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;
|
||||
@ -296,4 +299,50 @@ public class ProtobufSchemaCodegenTest {
|
||||
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…
x
Reference in New Issue
Block a user