Skip to content

Commit 997b859

Browse files
committed
Rename USE_UNWRAPPED_FOR_COMPOSITE_ONEOF
1 parent e04af45 commit 997b859

5 files changed

Lines changed: 31 additions & 34 deletions

File tree

docs/customization.md

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -681,11 +681,11 @@ index 6f27abd..146c61c 100644
681681
type: string
682682
```
683683

684-
- `USE_UNWRAPPED_FOR_INLINE_ONEOF` set to true to unwrap inline oneOf combined with allOf/properties and without discriminator (aka mixed oneOf). Set the vendorExtension X_ONE_OF_UNWRAPPED to be used by generators. For example the java generator annotates the new oneOf property with the jackson `@JsonUnwrapped` annotation
684+
- `USE_UNWRAPPED_FOR_COMPOSITE_ONEOF` set to true to unwrap oneOf combined with allOf/properties and without discriminator (aka "composite" oneOf). Set the vendorExtension X_ONE_OF_UNWRAPPED to be used by generators. For example the java generator annotates the new oneOf property with the jackson `@JsonUnwrapped` annotation
685685

686686
Example:
687687
```
688-
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/oneOf_unwrap_mixed.yaml -o /tmp/java/ --openapi-normalizer X_ONE_OF_UNWRAPPED=true
688+
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/oneOf_unwrap_mixed.yaml -o /tmp/java/ --openapi-normalizer USE_UNWRAPPED_FOR_COMPOSITE_ONEOF=true
689689
```
690690

691691
Here is what the change in the spec looks like:
@@ -698,28 +698,16 @@ index 6f27abd..146c61c 100644
698698
schemas:
699699
Account:
700700
- oneOf:
701-
- - properties:
702-
- bankNumber:
703-
- type: string
704-
- bic:
705-
- type: string
706-
- - properties:
707-
- iban:
708-
- type: string
701+
- - $ref: '#/components/schemas/LegacyBankNumber'
702+
- - ref: '#/components/schemas/WireTransferInfo'
709703
properties:
710704
bank:
711705
$ref: '#/components/schemas/Bank'
712706
+ oneOf:
713707
+ X_ONE_OF_UNWRAPPED: true
714708
+ oneOf:
715-
+ - properties:
716-
+ bankNumber:
717-
+ type: string
718-
+ bic:
719-
+ type: string
720-
+ - properties:
721-
+ iban:
722-
+ type: string
709+
+ - ref: '#/components/schemas/LegacyBankNumber'
710+
+ - ref: '#/components/schemas/WireTransferInfo'
723711
```
724712

725713
- `FILTER`

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public class OpenAPINormalizer {
159159
// when set to true, sort model properties by name to ensure deterministic output
160160
final String SORT_MODEL_PROPERTIES = "SORT_MODEL_PROPERTIES";
161161

162-
final String USE_UNWRAPPED_FOR_INLINE_ONEOF = "USE_UNWRAPPED_FOR_INLINE_ONEOF";
162+
final String USE_UNWRAPPED_FOR_COMPOSITE_ONEOF = "USE_UNWRAPPED_FOR_COMPOSITE_ONEOF";
163163
// ============= end of rules =============
164164

165165
/**
@@ -220,7 +220,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
220220
ruleNames.add(REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT);
221221
ruleNames.add(SORT_MODEL_PROPERTIES);
222222
ruleNames.add(REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING);
223-
ruleNames.add(USE_UNWRAPPED_FOR_INLINE_ONEOF);
223+
ruleNames.add(USE_UNWRAPPED_FOR_COMPOSITE_ONEOF);
224224

225225
// rules that are default to true
226226
rules.put(SIMPLIFY_ONEOF_ANYOF, true);
@@ -1092,7 +1092,7 @@ protected Schema normalizeOneOf(Schema schema, Set<Schema> visitedSchemas) {
10921092
}
10931093

10941094
protected Schema processUnwrappedOneOf(Schema schema) {
1095-
if (!getRule(USE_UNWRAPPED_FOR_INLINE_ONEOF)) {
1095+
if (!getRule(USE_UNWRAPPED_FOR_COMPOSITE_ONEOF)) {
10961096
return schema;
10971097
}
10981098
if (!(ModelUtils.hasOneOf(schema) && (ModelUtils.hasProperties(schema) || ModelUtils.hasAllOf(schema)))) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4457,7 +4457,7 @@ void unwrapped_oneOf_with_inheritance_sb3() throws IOException {
44574457
final Map<String, File> files = generateFromContract("src/test/resources/3_0/oneOf_unwrap_mixed.yaml", RESTCLIENT,
44584458
Map.of(AbstractJavaCodegen.USE_ONE_OF_INTERFACES, true,
44594459
USE_SPRING_BOOT4, false),
4460-
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_INLINE_ONEOF", "true")));
4460+
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_COMPOSITE_ONEOF", "true")));
44614461

44624462
JavaFileAssert.assertThat(files.get("Account.java"))
44634463
.assertProperty("oneOf")

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7733,7 +7733,7 @@ void unwrapped_oneOf_with_inheritance_sb4() throws IOException {
77337733
USE_DEDUCTION_FOR_ONE_OF_INTERFACES, true,
77347734
USE_SPRING_BOOT4, true,
77357735
USE_JACKSON_3, true),
7736-
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_INLINE_ONEOF", "true")));
7736+
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_COMPOSITE_ONEOF", "true")));
77377737

77387738
JavaFileAssert.assertThat(files.get("Account.java"))
77397739
.assertProperty("oneOf")
@@ -7743,7 +7743,8 @@ void unwrapped_oneOf_with_inheritance_sb4() throws IOException {
77437743
JavaFileAssert.assertThat(files.get("AccountOneOf.java"))
77447744
.isInterface()
77457745
.assertTypeAnnotations().doesNotContainWithName("JsonSubTypes").toType()
7746-
.fileContains("static interface AccountOneOfMixin", "@JsonCreator")
7746+
.fileContains("static interface AccountOneOfMixin", "@JsonCreator",
7747+
"@JsonSubTypes.Type(value = LegacyBankNumber.class)", "@JsonSubTypes.Type(value = WireTransferInfo.class)")
77477748
.hasImports("tools.jackson.databind.JsonNode");
77487749
JavaFileAssert.assertThat(files.get("JacksonMixinConfig.java"))
77497750
.fileContains(".addMixIn(AccountOneOf.class, AccountOneOf.AccountOneOfMixin.class)",
@@ -7762,7 +7763,7 @@ void unwrapped_oneOf_with_inheritance_sb3() throws IOException {
77627763
USE_DEDUCTION_FOR_ONE_OF_INTERFACES, true,
77637764
USE_SPRING_BOOT3, true)
77647765
,
7765-
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_INLINE_ONEOF", "true")));
7766+
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_COMPOSITE_ONEOF", "true")));
77667767

77677768
JavaFileAssert.assertThat(files.get("Account.java"))
77687769
.assertProperty("oneOf")
@@ -7790,14 +7791,18 @@ void unwrapped_oneOf_with_composition() throws IOException {
77907791
USE_DEDUCTION_FOR_ONE_OF_INTERFACES, false,
77917792
USE_SPRING_BOOT4, true,
77927793
USE_JACKSON_3, true),
7793-
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_INLINE_ONEOF", "true")));
7794+
configurator -> configurator.setOpenapiNormalizer(Map.of("USE_UNWRAPPED_FOR_COMPOSITE_ONEOF", "true")));
77947795
JavaFileAssert.assertThat(files.get("Account.java"))
77957796
.assertProperty("oneOf")
77967797
.doesImportAnnotation("JsonUnwrapped")
77977798
.assertPropertyAnnotations().containsWithName("JsonUnwrapped");
77987799
JavaFileAssert.assertThat(files.get("AccountOneOf.java"))
77997800
.isNormalClass()
7801+
.assertProperty("bankNumber").toType()
7802+
.assertProperty("iban").toType()
7803+
.assertProperty("bic").toType()
78007804
.assertTypeAnnotations().doesNotContainWithName("JsonSubTypes").toType()
78017805
.fileDoesNotContain("Mixin", "@JsonCreator");
7806+
78027807
}
78037808
}

modules/openapi-generator/src/test/resources/3_0/oneOf_unwrap_mixed.yaml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ components:
2525
bank:
2626
$ref: '#/components/schemas/Bank'
2727
oneOf:
28-
- properties:
29-
bankNumber:
30-
type: string
31-
bic:
32-
type: string
33-
- properties:
34-
iban:
35-
type: string
28+
- $ref: '#/components/schemas/LegacyBankNumber'
29+
- $ref: '#/components/schemas/WireTransferInfo'
30+
LegacyBankNumber:
31+
properties:
32+
bankNumber:
33+
type: string
34+
WireTransferInfo:
35+
properties:
36+
iban:
37+
type: string
38+
bic:
39+
type: string
3640
Bank:
3741
allOf:
3842
- properties:

0 commit comments

Comments
 (0)