Skip to content

Commit 1c8c322

Browse files
committed
Samples for deduction
1 parent 69e4336 commit 1c8c322

82 files changed

Lines changed: 4460 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generatorName: spring
2+
outputDir: samples/openapi3/server/petstore/spring-boot-oneof-interface
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/oneof_polymorphism_and_inheritance.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
5+
additionalProperties:
6+
groupId: org.openapitools.openapi3
7+
documentationProvider: springdoc
8+
artifactId: springboot-oneof
9+
snapshotVersion: "true"
10+
hideGenerationTimestamp: "true"
11+
useOneOfInterfaces: "true"
12+
useDeductionForOneOfInterfaces: "true"

docs/generators/java-camel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
7272
|licenseName|The name of the license| |Unlicense|
7373
|licenseUrl|The URL of the license| |http://unlicense.org|
7474
|modelPackage|package for generated models| |org.openapitools.model|
75+
|oneOfInterfacesDeduction|whether to use deduction for generated oneOf interfaces| |false|
7576
|openApiNullable|Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.| |true|
7677
|optionalAcceptNullable|Use `ofNullable` instead of just `of` to accept null values when using Optional.| |true|
7778
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|

docs/generators/spring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
6565
|licenseName|The name of the license| |Unlicense|
6666
|licenseUrl|The URL of the license| |http://unlicense.org|
6767
|modelPackage|package for generated models| |org.openapitools.model|
68+
|oneOfInterfacesDeduction|whether to use deduction for generated oneOf interfaces| |false|
6869
|openApiNullable|Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.| |true|
6970
|optionalAcceptNullable|Use `ofNullable` instead of just `of` to accept null values when using Optional.| |true|
7071
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class SpringCodegen extends AbstractJavaCodegen
9696
public static final String USE_REQUEST_MAPPING_ON_INTERFACE = "useRequestMappingOnInterface";
9797
public static final String USE_SEALED = "useSealed";
9898
public static final String OPTIONAL_ACCEPT_NULLABLE = "optionalAcceptNullable";
99-
99+
public static final String USE_DEDUCTION_FOR_ONE_OF_INTERFACES = "oneOfInterfacesDeduction";
100100
@Getter
101101
public enum RequestMappingMode {
102102
api_interface("Generate the @RequestMapping annotation on the generated Api Interface."),
@@ -152,6 +152,8 @@ public enum RequestMappingMode {
152152
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
153153
@Getter @Setter
154154
protected boolean optionalAcceptNullable = true;
155+
@Getter @Setter
156+
protected boolean useDeductionForOneOfInterfaces = false;
155157

156158
public SpringCodegen() {
157159
super();
@@ -260,6 +262,7 @@ public SpringCodegen() {
260262
"Use `ofNullable` instead of just `of` to accept null values when using Optional.",
261263
optionalAcceptNullable));
262264

265+
cliOptions.add(CliOption.newBoolean(USE_DEDUCTION_FOR_ONE_OF_INTERFACES, "whether to use deduction for generated oneOf interfaces", useDeductionForOneOfInterfaces));
263266
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
264267
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
265268
"Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");

modules/openapi-generator/src/main/resources/JavaSpring/oneof_interface.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
{{/withXml}}
55
{{#discriminator}}
66
{{>typeInfoAnnotation}}
7+
{{/discriminator}}{{^discriminator}}{{#useDeductionForOneOfInterfaces}}
8+
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
9+
@JsonSubTypes({
10+
{{#interfaceModels}}
11+
@JsonSubTypes.Type(value = {{classname}}.class){{^-last}}, {{/-last}}
12+
{{/interfaceModels}}
13+
})
14+
{{/useDeductionForOneOfInterfaces}}
715
{{/discriminator}}
816
{{>generatedAnnotation}}
917
public {{>sealed}}interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {{>permits}}{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,10 +1702,12 @@ public void testOneOfAndAllOf() throws IOException {
17021702
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");
17031703

17041704
codegen.setUseOneOfInterfaces(true);
1705+
codegen.setUseDeductionForOneOfInterfaces(true);
17051706
codegen.setLegacyDiscriminatorBehavior(false);
17061707

17071708
generator.opts(input).generate();
17081709

1710+
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Animal.java"), "@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)", "@JsonSubTypes.Type(value = Dog.class),", "@JsonSubTypes.Type(value = Cat.class)");
17091711
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Foo.java"), "public class Foo extends Entity implements FooRefOrValue");
17101712
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/FooRef.java"), "public class FooRef extends EntityRef implements FooRefOrValue");
17111713
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/FooRefOrValue.java"), "public interface FooRefOrValue");

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,20 @@ components:
209209
properties:
210210
length:
211211
type: integer
212+
Animal:
213+
oneOf:
214+
- $ref: '#/components/schemas/Dog'
215+
- $ref: '#/components/schemas/Cat'
216+
Cat:
217+
type: object
218+
properties:
219+
declawed:
220+
type: boolean
221+
Dog:
222+
type: object
223+
properties:
224+
bark:
225+
type: boolean
212226

213227
requestBodies:
214228
Foo:

samples/client/others/rust/hyper/oneOf/.openapi-generator/FILES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
Cargo.toml
44
README.md
55
docs/Addressable.md
6+
docs/Animal.md
67
docs/Apple.md
78
docs/Banana.md
89
docs/Bar.md
910
docs/BarApi.md
1011
docs/BarCreate.md
1112
docs/BarRef.md
1213
docs/BarRefOrValue.md
14+
docs/Cat.md
15+
docs/Dog.md
1316
docs/Entity.md
1417
docs/EntityRef.md
1518
docs/Extensible.md
@@ -31,12 +34,15 @@ src/apis/mod.rs
3134
src/apis/request.rs
3235
src/lib.rs
3336
src/models/addressable.rs
37+
src/models/animal.rs
3438
src/models/apple.rs
3539
src/models/banana.rs
3640
src/models/bar.rs
3741
src/models/bar_create.rs
3842
src/models/bar_ref.rs
3943
src/models/bar_ref_or_value.rs
44+
src/models/cat.rs
45+
src/models/dog.rs
4046
src/models/entity.rs
4147
src/models/entity_ref.rs
4248
src/models/extensible.rs

samples/client/others/rust/hyper/oneOf/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ Class | Method | HTTP request | Description
3535
## Documentation For Models
3636

3737
- [Addressable](docs/Addressable.md)
38+
- [Animal](docs/Animal.md)
3839
- [Apple](docs/Apple.md)
3940
- [Banana](docs/Banana.md)
4041
- [Bar](docs/Bar.md)
4142
- [BarCreate](docs/BarCreate.md)
4243
- [BarRef](docs/BarRef.md)
4344
- [BarRefOrValue](docs/BarRefOrValue.md)
45+
- [Cat](docs/Cat.md)
46+
- [Dog](docs/Dog.md)
4447
- [Entity](docs/Entity.md)
4548
- [EntityRef](docs/EntityRef.md)
4649
- [Extensible](docs/Extensible.md)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Animal
2+
3+
## Enum Variants
4+
5+
| Name | Description |
6+
|---- | -----|
7+
| Cat | |
8+
| Dog | |
9+
10+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
11+
12+

0 commit comments

Comments
 (0)