|
30 | 30 | import java.util.stream.Collectors; |
31 | 31 |
|
32 | 32 | import static org.openapitools.codegen.TestUtils.*; |
| 33 | +import static org.openapitools.codegen.languages.AbstractJavaCodegen.DISABLE_DISCRIMINATOR_JSON_IGNORE_PROPERTIES; |
33 | 34 | import static org.openapitools.codegen.languages.JavaJAXRSSpecServerCodegen.*; |
34 | 35 | import static org.openapitools.codegen.languages.features.GzipFeatures.USE_GZIP_FEATURE; |
35 | 36 | import static org.testng.Assert.assertTrue; |
@@ -1403,6 +1404,71 @@ public void testDiscriminatorJsonIgnorePropertiesPropagatesToChildren_whenLegacy |
1403 | 1404 | } |
1404 | 1405 | } |
1405 | 1406 |
|
| 1407 | + /** |
| 1408 | + * With {@code disableDiscriminatorJsonIgnoreProperties=false} (the default) the jaxrs-spec |
| 1409 | + * template emits {@code @JsonIgnoreProperties} on the discriminated model and uses |
| 1410 | + * {@code JsonTypeInfo.As.PROPERTY}. Regression guard mirroring the Java/Spring template behaviour. |
| 1411 | + */ |
| 1412 | + @Test |
| 1413 | + public void disableDiscriminatorJsonIgnorePropertiesIsFalseThenJsonIgnorePropertiesShouldBeAdded() throws Exception { |
| 1414 | + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1415 | + output.deleteOnExit(); |
| 1416 | + |
| 1417 | + Map<String, Object> properties = new HashMap<>(); |
| 1418 | + properties.put("legacyDiscriminatorBehavior", "false"); |
| 1419 | + properties.put(DISABLE_DISCRIMINATOR_JSON_IGNORE_PROPERTIES, "false"); |
| 1420 | + |
| 1421 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 1422 | + .setGeneratorName("jaxrs-spec") |
| 1423 | + .setAdditionalProperties(properties) |
| 1424 | + .setInputSpec("src/test/resources/3_0/jaxrs-spec/discriminator-mapping-children.yaml") |
| 1425 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 1426 | + |
| 1427 | + DefaultGenerator generator = new DefaultGenerator(); |
| 1428 | + Map<String, File> files = generator.opts(configurator.toClientOptInput()).generate().stream() |
| 1429 | + .collect(Collectors.toMap(File::getName, Function.identity())); |
| 1430 | + |
| 1431 | + JavaFileAssert.assertThat(files.get("PetResponse.java")) |
| 1432 | + .fileContains( |
| 1433 | + "@JsonIgnoreProperties(", |
| 1434 | + "value = \"petType\"", |
| 1435 | + "allowSetters = true", |
| 1436 | + "include = JsonTypeInfo.As.PROPERTY") |
| 1437 | + .fileDoesNotContain("include = JsonTypeInfo.As.EXISTING_PROPERTY"); |
| 1438 | + } |
| 1439 | + |
| 1440 | + /** |
| 1441 | + * With {@code disableDiscriminatorJsonIgnoreProperties=true} the jaxrs-spec template must OMIT |
| 1442 | + * {@code @JsonIgnoreProperties} (so a user-supplied one does not collide — Jackson forbids |
| 1443 | + * duplicate annotations) and switch {@code @JsonTypeInfo} to {@code JsonTypeInfo.As.EXISTING_PROPERTY} |
| 1444 | + * so the discriminator is not serialized twice. Aligns JavaJaxRS/spec/typeInfoAnnotation.mustache |
| 1445 | + * with Java/typeInfoAnnotation.mustache (PRs #22528 / #22924). |
| 1446 | + */ |
| 1447 | + @Test |
| 1448 | + public void disableDiscriminatorJsonIgnorePropertiesIsTrueThenJsonIgnorePropertiesShouldBeNotAdded() throws Exception { |
| 1449 | + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); |
| 1450 | + output.deleteOnExit(); |
| 1451 | + |
| 1452 | + Map<String, Object> properties = new HashMap<>(); |
| 1453 | + properties.put("legacyDiscriminatorBehavior", "false"); |
| 1454 | + properties.put(DISABLE_DISCRIMINATOR_JSON_IGNORE_PROPERTIES, "true"); |
| 1455 | + |
| 1456 | + final CodegenConfigurator configurator = new CodegenConfigurator() |
| 1457 | + .setGeneratorName("jaxrs-spec") |
| 1458 | + .setAdditionalProperties(properties) |
| 1459 | + .setInputSpec("src/test/resources/3_0/jaxrs-spec/discriminator-mapping-children.yaml") |
| 1460 | + .setOutputDir(output.getAbsolutePath().replace("\\", "/")); |
| 1461 | + |
| 1462 | + DefaultGenerator generator = new DefaultGenerator(); |
| 1463 | + Map<String, File> files = generator.opts(configurator.toClientOptInput()).generate().stream() |
| 1464 | + .collect(Collectors.toMap(File::getName, Function.identity())); |
| 1465 | + |
| 1466 | + JavaFileAssert.assertThat(files.get("PetResponse.java")) |
| 1467 | + .fileDoesNotContain("@JsonIgnoreProperties(") |
| 1468 | + .fileContains("include = JsonTypeInfo.As.EXISTING_PROPERTY") |
| 1469 | + .fileDoesNotContain("include = JsonTypeInfo.As.PROPERTY"); |
| 1470 | + } |
| 1471 | + |
1406 | 1472 | /** |
1407 | 1473 | * With {@code useOneOfInterfaces=true} a oneOf schema is generated as a Java interface, and the |
1408 | 1474 | * concrete subtypes implement it. With {@code useSealed=true} the interface is {@code sealed} and |
|
0 commit comments