|
17 | 17 | package org.openapitools.codegen.protobuf; |
18 | 18 |
|
19 | 19 | import io.swagger.v3.oas.models.OpenAPI; |
| 20 | +import io.swagger.v3.oas.models.media.ArraySchema; |
20 | 21 | import io.swagger.v3.oas.models.media.IntegerSchema; |
| 22 | +import io.swagger.v3.oas.models.media.MapSchema; |
| 23 | +import io.swagger.v3.oas.models.media.ObjectSchema; |
21 | 24 | import io.swagger.v3.oas.models.media.Schema; |
22 | 25 | import io.swagger.v3.oas.models.media.StringSchema; |
23 | 26 | import org.openapitools.codegen.ClientOptInput; |
@@ -296,4 +299,50 @@ public void unspecifiedEnumValuesIgnoredIfAlreadyPresent() { |
296 | 299 | Assert.assertEquals(enumVars1.get(1).get("value"), "FOO"); |
297 | 300 | Assert.assertEquals(enumVars1.get(1).get("isString"), false); |
298 | 301 | } |
| 302 | + |
| 303 | + @SuppressWarnings("unchecked") |
| 304 | + @Test(description = "Validate that enums in arrays are treated as complex types") |
| 305 | + public void enumInArrayIsTreatedAsComplexType() { |
| 306 | + final Schema enumSchema = new StringSchema()._enum(Arrays.asList("APPLE", "BANANA", "ORANGE")); |
| 307 | + final ArraySchema arraySchema = new ArraySchema(); |
| 308 | + arraySchema.setItems(enumSchema); |
| 309 | + |
| 310 | + final Schema model = new Schema() |
| 311 | + .description("a sample model with enum array") |
| 312 | + .addProperties("fruitList", arraySchema); |
| 313 | + |
| 314 | + final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen(); |
| 315 | + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 316 | + codegen.setOpenAPI(openAPI); |
| 317 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 318 | + codegen.additionalProperties().put(USE_SIMPLIFIED_ENUM_NAMES, true); |
| 319 | + codegen.processOpts(); |
| 320 | + codegen.postProcessModels(createCodegenModelWrapper(cm)); |
| 321 | + |
| 322 | + final CodegenProperty property = cm.vars.get(0); |
| 323 | + Assert.assertEquals(property.baseName, "fruitList"); |
| 324 | + } |
| 325 | + |
| 326 | + @SuppressWarnings("unchecked") |
| 327 | + @Test(description = "Validate that enums in maps are treated as complex types") |
| 328 | + public void enumInMapIsTreatedAsComplexType() { |
| 329 | + final Schema enumSchema = new StringSchema()._enum(Arrays.asList("RED", "GREEN", "BLUE")); |
| 330 | + final MapSchema mapSchema = new MapSchema(); |
| 331 | + mapSchema.setAdditionalProperties(enumSchema); |
| 332 | + |
| 333 | + final Schema model = new Schema() |
| 334 | + .description("a sample model with enum map") |
| 335 | + .addProperties("colorMap", mapSchema); |
| 336 | + |
| 337 | + final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen(); |
| 338 | + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 339 | + codegen.setOpenAPI(openAPI); |
| 340 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 341 | + codegen.additionalProperties().put(USE_SIMPLIFIED_ENUM_NAMES, true); |
| 342 | + codegen.processOpts(); |
| 343 | + codegen.postProcessModels(createCodegenModelWrapper(cm)); |
| 344 | + |
| 345 | + final CodegenProperty property = cm.vars.get(0); |
| 346 | + Assert.assertEquals(property.baseName, "colorMap"); |
| 347 | + } |
299 | 348 | } |
0 commit comments