[Protobuf] Add isEnumSchema check in generateNestedSchema (#22384)

Co-authored-by: xil <xil@uber.com>
This commit is contained in:
Xi Lu 2025-11-19 01:46:43 -08:00 committed by GitHub
parent 6210db308e
commit 9655c22ff6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 5 deletions

View File

@ -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)){
@ -1116,7 +1116,7 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
} 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) {
@ -1124,8 +1124,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf
addtionalPropertiesName = toVarName(toModelName(ref));
}
}
properties.put(addtionalPropertiesName, schema);
properties.put(addtionalPropertiesName, schema);
}
}
}

View File

@ -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");
}
}