Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Java/Spring] Support Discriminator Based OneOf Interface #11650

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
6a8769e
Fix OpenAPITools#5381
alexsuperdev Mar 21, 2020
049359c
Fix OpenAPITools#5381
alexsuperdev Mar 22, 2020
49f13eb
Fix OpenAPITools#5381
alexsuperdev Mar 22, 2020
1a5a484
Fix OpenAPITools#5381
alexsuperdev Mar 22, 2020
66a09d2
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
alexsuperdev Mar 22, 2020
48a1a4a
Fix OpenAPITools#5381
alexsuperdev Mar 24, 2020
a811330
Fix OpenAPITools#5381
alexsuperdev Mar 24, 2020
299cdb3
Fix OpenAPITools#5381
alexsuperdev Mar 24, 2020
461cbc8
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
alexsuperdev Mar 24, 2020
dd0118d
Merge branch 'master' of https://github.com/OpenAPITools/openapi-gene…
alexsuperdev Mar 25, 2020
26a95f6
Fix OpenAPITools#5381
alexsuperdev Mar 25, 2020
9af7db6
Fix OpenAPITools#5381
alexsuperdev Mar 30, 2020
fbfd57d
Fix OpenAPITools#5381
alexsuperdev Mar 21, 2020
bfab304
Fix OpenAPITools#5381
alexsuperdev Mar 22, 2020
29003d8
Fix OpenAPITools#5381
alexsuperdev Apr 1, 2020
d864831
Fix OpenAPITools#5381
alexsuperdev Apr 1, 2020
6d54799
Fix OpenAPITools#5381
alexsuperdev Apr 2, 2020
c6a5c6b
Merge remote-tracking branch 'super/master' into spring_fix_5381_jb
jburgess Dec 1, 2020
d96cd2e
Merge remote-tracking branch 'super/master' into spring_fix_5381_jb
jburgess Dec 3, 2020
8f09f52
Initial merge of 5.0
jburgess Dec 3, 2020
0d31df4
Aligned with master formatting
jburgess Dec 3, 2020
397e711
Corrected spacing for class names to align with samples.
jburgess Dec 4, 2020
a922217
Merge remote-tracking branch 'source/master' into spring_fix_5381_jb
jburgess Dec 5, 2020
21a98e5
Merge remote-tracking branch 'source/master' into spring_fix_5381_jb
jburgess Aug 11, 2021
1b5964e
Merge remote-tracking branch 'source/master' into spring_fix_5381_jb
jburgess Aug 11, 2021
05105fa
Merged master
jburgess Aug 11, 2021
c89a28b
Updated samples
jburgess Aug 11, 2021
4808e87
Merge branch 'spring_fix_5381_jb' of https://github.com/jburgess/open…
wing328 Sep 14, 2021
03a6210
Merge branch 'master' into jburgess-spring_fix_5381_jb
cachescrubber Jan 27, 2022
7db40af
Merge branch 'master' into jburgess-spring_fix_5381_jb
cachescrubber Jan 29, 2022
f634245
Consolidate methods from JavaClient and SpringCodegen (mov up to Abst…
cachescrubber Jan 29, 2022
921e9b8
set useLegacyDiscriminator to false, format templates
cachescrubber Jan 29, 2022
9502601
Suport JsonTypeName, fq class name for spring.io.Resource
cachescrubber Jan 30, 2022
054cfda
Generate Samples
cachescrubber Jan 30, 2022
cb998d4
Test full qualified usage of the spring Resource interface.
cachescrubber Jan 30, 2022
79e6439
Add java-camel to samples.circleci.spring profile
cachescrubber Jan 30, 2022
cbfa506
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Jan 30, 2022
7a93562
Add more complex example combining inheritance and oneof-interface
cachescrubber Jan 30, 2022
2773e1e
Remove x-implements Serializable from JavaClientCodegen (moved to Abs…
cachescrubber Jan 30, 2022
d850256
Fix spacing before opening brace after extends/implements
cachescrubber Jan 30, 2022
595fb2c
Generate Samples
cachescrubber Jan 30, 2022
6a542a0
Add more complex example combining inheritance and oneof-interface
cachescrubber Jan 30, 2022
da50e04
Generate Samples
cachescrubber Jan 30, 2022
ca1c72a
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Jan 31, 2022
e0d7905
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Jan 31, 2022
6dfe29e
Fix JsonTypeName annotation handling in Java and JavaSpring
cachescrubber Feb 2, 2022
3849661
Content mediatype is hardcoded in api.mustache #11511
cachescrubber Feb 5, 2022
79c80f8
Generate Samples
cachescrubber Feb 5, 2022
a1a4ff0
OAS3 incorrect data type when providing a default value #11367
cachescrubber Feb 5, 2022
bd6ba44
Generate Samples
cachescrubber Feb 5, 2022
4ff2187
Fix JsonTypeName annotation handling in Java and JavaSpring
cachescrubber Feb 2, 2022
45d1fd2
Generate Samples
cachescrubber Feb 5, 2022
2650814
Merge branch 'bugfix/spring_generator' into feature/java_spring_oneof…
cachescrubber Feb 5, 2022
25ac888
getIsClassnameSanitized: use null safe equals
cachescrubber Feb 5, 2022
fe29270
Merge branch 'bugfix/spring_generator' into feature/java_spring_oneof…
cachescrubber Feb 5, 2022
6e3150b
Fix JsonTypeName annotation handling in Java and JavaSpring (merge)
cachescrubber Feb 5, 2022
bcbd156
Generate Samples
cachescrubber Feb 5, 2022
5e77e8c
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 5, 2022
6202d69
Generate Samples
cachescrubber Feb 5, 2022
e20ba2c
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 9, 2022
d676188
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 10, 2022
122d414
Merge remote-tracking branch 'origin/feature/java_spring_oneof_discri…
cachescrubber Feb 10, 2022
c1ce27a
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 12, 2022
be407dc
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 15, 2022
26ae1a9
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 18, 2022
c0f153f
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 19, 2022
aaf6e99
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 23, 2022
c1a7192
Add oneof sample
cachescrubber Feb 23, 2022
925252e
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 23, 2022
58bd55b
Generate Samples
cachescrubber Feb 23, 2022
af28bbe
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Feb 25, 2022
d6558a8
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Mar 1, 2022
d03f748
Giv example oas spec a meaningful name, demo usage of oneOf in Model
cachescrubber Mar 2, 2022
b345151
Generate Samples
cachescrubber Mar 2, 2022
f8b70a8
Remove unnecessary JsonTypeName include, add example for JsonTypeName…
cachescrubber Mar 2, 2022
c161329
Generate Samples
cachescrubber Mar 2, 2022
833893a
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Mar 6, 2022
6e01140
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Mar 6, 2022
d43692e
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Mar 7, 2022
deeb394
Generate Samples
cachescrubber Mar 7, 2022
e619e73
Merge branch 'master' into feature/java_spring_oneof_discriminator_basic
cachescrubber Mar 15, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/samples-spring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- samples/server/petstore/springboot-delegate
- samples/openapi3/server/petstore/springboot-delegate
- samples/server/petstore/spring-boot-nullable-set
- samples/openapi3/server/petstore/spring-boot-oneof
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
Expand Down
10 changes: 10 additions & 0 deletions bin/configs/spring-boot-onewof.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
generatorName: spring
outputDir: samples/openapi3/server/petstore/spring-boot-oneof
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_5381_cs.yaml
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
additionalProperties:
groupId: org.openapitools.openapi3
documentationProvider: springdoc
artifactId: springboot-oneof
snapshotVersion: "true"
hideGenerationTimestamp: "true"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
README.md
pom.xml
src/main/java/org/openapitools/OpenApiGeneratorApplication.java
src/main/java/org/openapitools/RFC3339DateFormat.java
src/main/java/org/openapitools/api/ApiUtil.java
src/main/java/org/openapitools/api/FooApi.java
src/main/java/org/openapitools/api/FooApiController.java
src/main/java/org/openapitools/configuration/HomeController.java
src/main/java/org/openapitools/model/Addressable.java
src/main/java/org/openapitools/model/Bar.java
src/main/java/org/openapitools/model/BarRef.java
src/main/java/org/openapitools/model/Entity.java
src/main/java/org/openapitools/model/EntityRef.java
src/main/java/org/openapitools/model/Extensible.java
src/main/java/org/openapitools/model/Foo.java
src/main/java/org/openapitools/model/FooRef.java
src/main/java/org/openapitools/model/FooRefOrValue.java
src/main/resources/application.properties
src/main/resources/openapi.yaml
src/test/java/org/openapitools/OpenApiGeneratorApplicationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6.0.0-SNAPSHOT
21 changes: 21 additions & 0 deletions samples/openapi3/server/petstore/spring-boot-oneof/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OpenAPI generated server

Spring Boot Server

## Overview
This server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
By using the [OpenAPI-Spec](https://openapis.org), you can easily generate a server stub.
This is an example of building a OpenAPI-enabled server in Java using the SpringBoot framework.


The underlying library integrating OpenAPI to Spring Boot is [springdoc](https://springdoc.org).
Springdoc will generate an OpenAPI v3 specification based on the generated Controller and Model classes.
The specification is available to download using the following url:
http://localhost:8080/v3/api-docs/

Start your server as a simple java application

You can view the api documentation in swagger-ui by pointing to
http://localhost:8080/swagger-ui.html

Change default port value in application.properties
78 changes: 78 additions & 0 deletions samples/openapi3/server/petstore/spring-boot-oneof/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools.openapi3</groupId>
<artifactId>springboot-oneof</artifactId>
<packaging>jar</packaging>
<name>springboot-oneof</name>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<springdoc.version>1.6.4</springdoc.version>
<swagger-ui.version>4.4.1-1</swagger-ui.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
</parent>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<!--SpringDoc dependencies -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.2</version>
</dependency>
<!-- Bean Validation API support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.openapitools;

import com.fasterxml.jackson.databind.Module;
import org.openapitools.jackson.nullable.JsonNullableModule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"org.openapitools", "org.openapitools.api" , "org.openapitools.configuration"})
public class OpenApiGeneratorApplication {

public static void main(String[] args) {
SpringApplication.run(OpenApiGeneratorApplication.class, args);
}

@Bean
public Module jsonNullableModule() {
return new JsonNullableModule();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.openapitools;

import com.fasterxml.jackson.databind.util.StdDateFormat;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class RFC3339DateFormat extends DateFormat {
private static final long serialVersionUID = 1L;
private static final TimeZone TIMEZONE_Z = TimeZone.getTimeZone("UTC");

private final StdDateFormat fmt = new StdDateFormat()
.withTimeZone(TIMEZONE_Z)
.withColonInTimeZone(true);

public RFC3339DateFormat() {
this.calendar = new GregorianCalendar();
}

@Override
public Date parse(String source, ParsePosition pos) {
return fmt.parse(source, pos);
}

@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
return fmt.format(date, toAppendTo, fieldPosition);
}

@Override
public Object clone() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.openapitools.api;

import org.springframework.web.context.request.NativeWebRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ApiUtil {
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
try {
HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class);
res.setCharacterEncoding("UTF-8");
res.addHeader("Content-Type", contentType);
res.getWriter().print(example);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (6.0.0-SNAPSHOT).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
package org.openapitools.api;

import org.openapitools.model.Foo;
import org.openapitools.model.FooRefOrValue;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Generated;

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
@Validated
@Tag(name = "foo", description = "the foo API")
public interface FooApi {

default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}

/**
* POST /foo : Create a Foo
*
* @param foo The Foo to be created (optional)
* @return Error (status code 201)
*/
@Operation(
operationId = "createFoo",
summary = "Create a Foo",
tags = { "Foo" },
responses = {
@ApiResponse(responseCode = "201", description = "Error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = FooRefOrValue.class))
})
}
)
@RequestMapping(
method = RequestMethod.POST,
value = "/foo",
produces = { "application/json" },
consumes = { "application/json;charset=utf-8" }
)
default ResponseEntity<FooRefOrValue> createFoo(
@Parameter(name = "Foo", description = "The Foo to be created") @Valid @RequestBody(required = false) Foo foo
) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
String exampleString = "null";
ApiUtil.setExampleResponse(request, "application/json", exampleString);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

}


/**
* GET /foo : GET all Foos
*
* @return Success (status code 200)
*/
@Operation(
operationId = "getAllFoos",
summary = "GET all Foos",
tags = { "Foo" },
responses = {
@ApiResponse(responseCode = "200", description = "Success", content = {
@Content(mediaType = "application/json;charset=utf-8", schema = @Schema(implementation = FooRefOrValue.class))
})
}
)
@RequestMapping(
method = RequestMethod.GET,
value = "/foo",
produces = { "application/json;charset=utf-8" }
)
default ResponseEntity<List<FooRefOrValue>> getAllFoos(

) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json;charset=utf-8"))) {
String exampleString = "null";
ApiUtil.setExampleResponse(request, "application/json;charset=utf-8", exampleString);
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

}

}
Loading