|
45 | 45 | import static org.openapitools.codegen.TestUtils.createCodegenModelWrapper; |
46 | 46 | import static org.openapitools.codegen.languages.ProtobufSchemaCodegen.USE_SIMPLIFIED_ENUM_NAMES; |
47 | 47 | import static org.testng.Assert.assertEquals; |
| 48 | +import static org.openapitools.codegen.languages.ProtobufSchemaCodegen.START_ENUMS_WITH_UNSPECIFIED; |
48 | 49 |
|
49 | 50 | public class ProtobufSchemaCodegenTest { |
50 | 51 |
|
@@ -213,4 +214,86 @@ private void testEnumValues(boolean simpleEnumValue) { |
213 | 214 | Assert.assertEquals(enumVars2.get(1).get("value"), simpleEnumValue ? "_2" : "\"TEST_INT_ENUM__2\""); |
214 | 215 | Assert.assertEquals(enumVars2.get(1).get("isString"), false); |
215 | 216 | } |
| 217 | + |
| 218 | + @SuppressWarnings("unchecked") |
| 219 | + @Test(description = "Validate that unspecified enum values are added when the option is selected") |
| 220 | + public void unspecifiedEnumValuesAreAdded() { |
| 221 | + String enumKey = "aValidEnumWithoutUnspecifiedValues"; |
| 222 | + |
| 223 | + final Schema<?> model = new Schema<>() |
| 224 | + .description("a sample model") |
| 225 | + .addProperty(enumKey, new StringSchema()._enum(Arrays.asList("foo", "bar"))); |
| 226 | + |
| 227 | + final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen(); |
| 228 | + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 229 | + codegen.setOpenAPI(openAPI); |
| 230 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 231 | + codegen.additionalProperties().put(USE_SIMPLIFIED_ENUM_NAMES, true); |
| 232 | + codegen.additionalProperties().put(START_ENUMS_WITH_UNSPECIFIED, true); |
| 233 | + |
| 234 | + codegen.processOpts(); |
| 235 | + codegen.postProcessModels(createCodegenModelWrapper(cm)); |
| 236 | + |
| 237 | + final CodegenProperty property1 = cm.vars.get(0); |
| 238 | + Assert.assertEquals(property1.baseName, enumKey); |
| 239 | + Assert.assertEquals(property1.dataType, "string"); |
| 240 | + Assert.assertEquals(property1.baseType, "string"); |
| 241 | + Assert.assertEquals(property1.datatypeWithEnum, "A_valid_enum_without_unspecified_values"); |
| 242 | + Assert.assertEquals(property1.name, "a_valid_enum_without_unspecified_values"); |
| 243 | + Assert.assertTrue(property1.isEnum); |
| 244 | + Assert.assertEquals(property1.allowableValues.size(), 2); |
| 245 | + List<Map<String, Object>> enumVars1 = (List<Map<String, Object>>) property1.allowableValues.get("enumVars"); |
| 246 | + Assert.assertEquals(enumVars1.size(), 3); |
| 247 | + |
| 248 | + Assert.assertEquals(enumVars1.get(0).get("name"), "UNSPECIFIED"); |
| 249 | + Assert.assertEquals(enumVars1.get(0).get("value"), "UNSPECIFIED"); |
| 250 | + Assert.assertEquals(Boolean.valueOf((String) enumVars1.get(0).get("isString")), false); |
| 251 | + |
| 252 | + Assert.assertEquals(enumVars1.get(1).get("name"), "FOO"); |
| 253 | + Assert.assertEquals(enumVars1.get(1).get("value"), "FOO"); |
| 254 | + Assert.assertEquals(enumVars1.get(1).get("isString"), false); |
| 255 | + |
| 256 | + Assert.assertEquals(enumVars1.get(2).get("name"), "BAR"); |
| 257 | + Assert.assertEquals(enumVars1.get(2).get("value"), "BAR"); |
| 258 | + Assert.assertEquals(enumVars1.get(2).get("isString"), false); |
| 259 | + } |
| 260 | + |
| 261 | + @SuppressWarnings("unchecked") |
| 262 | + @Test(description = "Validate that unspecified enum values are NOT added when the option is selected if they are already present") |
| 263 | + public void unspecifiedEnumValuesIgnoredIfAlreadyPresent() { |
| 264 | + String enumKey = "aValidEnumWithUnspecifiedValues"; |
| 265 | + |
| 266 | + final Schema<?> model = new Schema<>() |
| 267 | + .description("a sample model") |
| 268 | + .addProperty(enumKey, new StringSchema()._enum(Arrays.asList( "UNSPECIFIED", "foo"))); |
| 269 | + |
| 270 | + final ProtobufSchemaCodegen codegen = new ProtobufSchemaCodegen(); |
| 271 | + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", model); |
| 272 | + codegen.setOpenAPI(openAPI); |
| 273 | + final CodegenModel cm = codegen.fromModel("sample", model); |
| 274 | + codegen.additionalProperties().put(USE_SIMPLIFIED_ENUM_NAMES, true); |
| 275 | + codegen.additionalProperties().put(START_ENUMS_WITH_UNSPECIFIED, true); |
| 276 | + |
| 277 | + codegen.processOpts(); |
| 278 | + codegen.postProcessModels(createCodegenModelWrapper(cm)); |
| 279 | + |
| 280 | + final CodegenProperty property1 = cm.vars.get(0); |
| 281 | + Assert.assertEquals(property1.baseName, enumKey); |
| 282 | + Assert.assertEquals(property1.dataType, "string"); |
| 283 | + Assert.assertEquals(property1.baseType, "string"); |
| 284 | + Assert.assertEquals(property1.datatypeWithEnum, "A_valid_enum_with_unspecified_values"); |
| 285 | + Assert.assertEquals(property1.name, "a_valid_enum_with_unspecified_values"); |
| 286 | + Assert.assertTrue(property1.isEnum); |
| 287 | + Assert.assertEquals(property1.allowableValues.size(), 2); |
| 288 | + List<Map<String, Object>> enumVars1 = (List<Map<String, Object>>) property1.allowableValues.get("enumVars"); |
| 289 | + Assert.assertEquals(enumVars1.size(), 2); |
| 290 | + |
| 291 | + Assert.assertEquals(enumVars1.get(0).get("name"), "UNSPECIFIED"); |
| 292 | + Assert.assertEquals(enumVars1.get(0).get("value"), "UNSPECIFIED"); |
| 293 | + Assert.assertEquals(enumVars1.get(0).get("isString"), false); |
| 294 | + |
| 295 | + Assert.assertEquals(enumVars1.get(1).get("name"), "FOO"); |
| 296 | + Assert.assertEquals(enumVars1.get(1).get("value"), "FOO"); |
| 297 | + Assert.assertEquals(enumVars1.get(1).get("isString"), false); |
| 298 | + } |
216 | 299 | } |
0 commit comments