Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bin/configs/spring-boot-oneof-interface.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
generatorName: spring
outputDir: samples/openapi3/server/petstore/spring-boot-oneof-interface
inputSpec: modules/openapi-generator/src/test/resources/3_0/oneof_polymorphism_and_inheritance.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
groupId: org.openapitools.openapi3
documentationProvider: springdoc
artifactId: springboot-oneof
snapshotVersion: "true"
hideGenerationTimestamp: "true"
useOneOfInterfaces: "true"
useDeductionForOneOfInterfaces: "true"
1 change: 1 addition & 0 deletions docs/generators/java-camel.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|licenseName|The name of the license| |Unlicense|
|licenseUrl|The URL of the license| |http://unlicense.org|
|modelPackage|package for generated models| |org.openapitools.model|
|oneOfInterfacesDeduction|whether to use deduction for generated oneOf interfaces| |false|
|openApiNullable|Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.| |true|
|optionalAcceptNullable|Use `ofNullable` instead of just `of` to accept null values when using Optional.| |true|
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
Expand Down
1 change: 1 addition & 0 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|licenseName|The name of the license| |Unlicense|
|licenseUrl|The URL of the license| |http://unlicense.org|
|modelPackage|package for generated models| |org.openapitools.model|
|oneOfInterfacesDeduction|whether to use deduction for generated oneOf interfaces| |false|
|openApiNullable|Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.| |true|
|optionalAcceptNullable|Use `ofNullable` instead of just `of` to accept null values when using Optional.| |true|
|parentArtifactId|parent artifactId in generated pom N.B. parentGroupId, parentArtifactId and parentVersion must all be specified for any of them to take effect| |null|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String USE_REQUEST_MAPPING_ON_INTERFACE = "useRequestMappingOnInterface";
public static final String USE_SEALED = "useSealed";
public static final String OPTIONAL_ACCEPT_NULLABLE = "optionalAcceptNullable";

public static final String USE_DEDUCTION_FOR_ONE_OF_INTERFACES = "oneOfInterfacesDeduction";
@Getter
public enum RequestMappingMode {
api_interface("Generate the @RequestMapping annotation on the generated Api Interface."),
Expand Down Expand Up @@ -152,6 +152,8 @@ public enum RequestMappingMode {
protected RequestMappingMode requestMappingMode = RequestMappingMode.controller;
@Getter @Setter
protected boolean optionalAcceptNullable = true;
@Getter @Setter
protected boolean useDeductionForOneOfInterfaces = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -260,6 +262,7 @@ public SpringCodegen() {
"Use `ofNullable` instead of just `of` to accept null values when using Optional.",
optionalAcceptNullable));

cliOptions.add(CliOption.newBoolean(USE_DEDUCTION_FOR_ONE_OF_INTERFACES, "whether to use deduction for generated oneOf interfaces", useDeductionForOneOfInterfaces));
supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application.");
supportedLibraries.put(SPRING_CLOUD_LIBRARY,
"Spring-Cloud-Feign client with Spring-Boot auto-configured settings.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
{{/withXml}}
{{#discriminator}}
{{>typeInfoAnnotation}}
{{/discriminator}}{{^discriminator}}{{#useDeductionForOneOfInterfaces}}
@JsonTypeInfo(use = JsonTypeInfo.Id.DEDUCTION)
@JsonSubTypes({
{{#interfaceModels}}
@JsonSubTypes.Type(value = {{classname}}.class){{^-last}}, {{/-last}}
{{/interfaceModels}}
})
{{/useDeductionForOneOfInterfaces}}
{{/discriminator}}
{{>generatedAnnotation}}
public {{>sealed}}interface {{classname}}{{#vendorExtensions.x-implements}}{{#-first}} extends {{{.}}}{{/-first}}{{^-first}}, {{{.}}}{{/-first}}{{/vendorExtensions.x-implements}} {{>permits}}{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1702,10 +1702,12 @@ public void testOneOfAndAllOf() throws IOException {
generator.setGeneratorPropertyDefault(CodegenConstants.LEGACY_DISCRIMINATOR_BEHAVIOR, "false");

codegen.setUseOneOfInterfaces(true);
codegen.setUseDeductionForOneOfInterfaces(true);
codegen.setLegacyDiscriminatorBehavior(false);

generator.opts(input).generate();

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)");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/Foo.java"), "public class Foo extends Entity implements FooRefOrValue");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/FooRef.java"), "public class FooRef extends EntityRef implements FooRefOrValue");
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/FooRefOrValue.java"), "public interface FooRefOrValue");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ components:
properties:
length:
type: integer
Animal:
oneOf:
- $ref: '#/components/schemas/Dog'
- $ref: '#/components/schemas/Cat'
Cat:
type: object
properties:
declawed:
type: boolean
Dog:
type: object
properties:
bark:
type: boolean

requestBodies:
Foo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
Cargo.toml
README.md
docs/Addressable.md
docs/Animal.md
docs/Apple.md
docs/Banana.md
docs/Bar.md
docs/BarApi.md
docs/BarCreate.md
docs/BarRef.md
docs/BarRefOrValue.md
docs/Cat.md
docs/Dog.md
docs/Entity.md
docs/EntityRef.md
docs/Extensible.md
Expand All @@ -31,12 +34,15 @@ src/apis/mod.rs
src/apis/request.rs
src/lib.rs
src/models/addressable.rs
src/models/animal.rs
src/models/apple.rs
src/models/banana.rs
src/models/bar.rs
src/models/bar_create.rs
src/models/bar_ref.rs
src/models/bar_ref_or_value.rs
src/models/cat.rs
src/models/dog.rs
src/models/entity.rs
src/models/entity_ref.rs
src/models/extensible.rs
Expand Down
3 changes: 3 additions & 0 deletions samples/client/others/rust/hyper/oneOf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ Class | Method | HTTP request | Description
## Documentation For Models

- [Addressable](docs/Addressable.md)
- [Animal](docs/Animal.md)
- [Apple](docs/Apple.md)
- [Banana](docs/Banana.md)
- [Bar](docs/Bar.md)
- [BarCreate](docs/BarCreate.md)
- [BarRef](docs/BarRef.md)
- [BarRefOrValue](docs/BarRefOrValue.md)
- [Cat](docs/Cat.md)
- [Dog](docs/Dog.md)
- [Entity](docs/Entity.md)
- [EntityRef](docs/EntityRef.md)
- [Extensible](docs/Extensible.md)
Expand Down
12 changes: 12 additions & 0 deletions samples/client/others/rust/hyper/oneOf/docs/Animal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Animal

## Enum Variants

| Name | Description |
|---- | -----|
| Cat | |
| Dog | |

[[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 changes: 11 additions & 0 deletions samples/client/others/rust/hyper/oneOf/docs/Cat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Cat

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**declawed** | Option<**bool**> | | [optional]

[[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 changes: 11 additions & 0 deletions samples/client/others/rust/hyper/oneOf/docs/Dog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dog

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**bark** | Option<**bool**> | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


26 changes: 26 additions & 0 deletions samples/client/others/rust/hyper/oneOf/src/models/animal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* ByRefOrValue
*
* This tests for a oneOf interface representation
*
* The version of the OpenAPI document: 0.0.1
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Animal {
Dog(Box<models::Dog>),
Cat(Box<models::Cat>),
}

impl Default for Animal {
fn default() -> Self {
Self::Dog(Default::default())
}
}

27 changes: 27 additions & 0 deletions samples/client/others/rust/hyper/oneOf/src/models/cat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* ByRefOrValue
*
* This tests for a oneOf interface representation
*
* The version of the OpenAPI document: 0.0.1
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct Cat {
#[serde(rename = "declawed", skip_serializing_if = "Option::is_none")]
pub declawed: Option<bool>,
}

impl Cat {
pub fn new() -> Cat {
Cat {
declawed: None,
}
}
}

27 changes: 27 additions & 0 deletions samples/client/others/rust/hyper/oneOf/src/models/dog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* ByRefOrValue
*
* This tests for a oneOf interface representation
*
* The version of the OpenAPI document: 0.0.1
*
* Generated by: https://openapi-generator.tech
*/

use crate::models;
use serde::{Deserialize, Serialize};

#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
pub struct Dog {
#[serde(rename = "bark", skip_serializing_if = "Option::is_none")]
pub bark: Option<bool>,
}

impl Dog {
pub fn new() -> Dog {
Dog {
bark: None,
}
}
}

6 changes: 6 additions & 0 deletions samples/client/others/rust/hyper/oneOf/src/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod addressable;
pub use self::addressable::Addressable;
pub mod animal;
pub use self::animal::Animal;
pub mod apple;
pub use self::apple::Apple;
pub mod banana;
Expand All @@ -12,6 +14,10 @@ pub mod bar_ref;
pub use self::bar_ref::BarRef;
pub mod bar_ref_or_value;
pub use self::bar_ref_or_value::BarRefOrValue;
pub mod cat;
pub use self::cat::Cat;
pub mod dog;
pub use self::dog::Dog;
pub mod entity;
pub use self::entity::Entity;
pub mod entity_ref;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
Cargo.toml
README.md
docs/Addressable.md
docs/Animal.md
docs/Apple.md
docs/Banana.md
docs/Bar.md
docs/BarApi.md
docs/BarCreate.md
docs/BarRef.md
docs/BarRefOrValue.md
docs/Cat.md
docs/Dog.md
docs/Entity.md
docs/EntityRef.md
docs/Extensible.md
Expand All @@ -29,12 +32,15 @@ src/apis/foo_api.rs
src/apis/mod.rs
src/lib.rs
src/models/addressable.rs
src/models/animal.rs
src/models/apple.rs
src/models/banana.rs
src/models/bar.rs
src/models/bar_create.rs
src/models/bar_ref.rs
src/models/bar_ref_or_value.rs
src/models/cat.rs
src/models/dog.rs
src/models/entity.rs
src/models/entity_ref.rs
src/models/extensible.rs
Expand Down
3 changes: 3 additions & 0 deletions samples/client/others/rust/reqwest/oneOf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ Class | Method | HTTP request | Description
## Documentation For Models

- [Addressable](docs/Addressable.md)
- [Animal](docs/Animal.md)
- [Apple](docs/Apple.md)
- [Banana](docs/Banana.md)
- [Bar](docs/Bar.md)
- [BarCreate](docs/BarCreate.md)
- [BarRef](docs/BarRef.md)
- [BarRefOrValue](docs/BarRefOrValue.md)
- [Cat](docs/Cat.md)
- [Dog](docs/Dog.md)
- [Entity](docs/Entity.md)
- [EntityRef](docs/EntityRef.md)
- [Extensible](docs/Extensible.md)
Expand Down
12 changes: 12 additions & 0 deletions samples/client/others/rust/reqwest/oneOf/docs/Animal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Animal

## Enum Variants

| Name | Description |
|---- | -----|
| Cat | |
| Dog | |

[[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 changes: 11 additions & 0 deletions samples/client/others/rust/reqwest/oneOf/docs/Cat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Cat

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**declawed** | Option<**bool**> | | [optional]

[[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 changes: 11 additions & 0 deletions samples/client/others/rust/reqwest/oneOf/docs/Dog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dog

## Properties

Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**bark** | Option<**bool**> | | [optional]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


Loading
Loading