forked from loafle/openapi-generator-original
[Java] Return content type on default spring reactive responses (#10099)
* Return content type on default spring reactive responses * Return content type on default spring reactive responses * Refactor apiUtil and use request mediaType Co-authored-by: Joshua Bridge <jbridg12@jaguarlandrover.com>
This commit is contained in:
parent
d68d65ce00
commit
6331eb6293
@ -2,7 +2,10 @@ package {{apiPackage}};
|
|||||||
|
|
||||||
{{#reactive}}
|
{{#reactive}}
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import org.springframework.core.io.buffer.DefaultDataBuffer;
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
{{/reactive}}
|
{{/reactive}}
|
||||||
@ -27,8 +30,13 @@ public class ApiUtil {
|
|||||||
}
|
}
|
||||||
{{/reactive}}
|
{{/reactive}}
|
||||||
{{#reactive}}
|
{{#reactive}}
|
||||||
public static Mono<Void> getExampleResponse(ServerWebExchange exchange, String example) {
|
public static Mono<Void> getExampleResponse(ServerWebExchange exchange, MediaType mediaType, String example) {
|
||||||
return exchange.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap(example.getBytes(StandardCharsets.UTF_8))));
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
response.getHeaders().setContentType(mediaType);
|
||||||
|
|
||||||
|
byte[] exampleBytes = example.getBytes(StandardCharsets.UTF_8);
|
||||||
|
DefaultDataBuffer data = new DefaultDataBufferFactory().wrap(exampleBytes);
|
||||||
|
return response.writeWith(Mono.just(data));
|
||||||
}
|
}
|
||||||
{{/reactive}}
|
{{/reactive}}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ Mono<Void> result = Mono.empty();
|
|||||||
{{/-first}}
|
{{/-first}}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
|
||||||
String exampleString = {{>exampleString}};
|
String exampleString = {{>exampleString}};
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
{{#-last}}
|
{{#-last}}
|
||||||
|
@ -42,7 +42,7 @@ public interface AnotherFakeApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"client\" : \"client\" }";
|
String exampleString = "{ \"client\" : \"client\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
package org.openapitools.api;
|
package org.openapitools.api;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import org.springframework.core.io.buffer.DefaultDataBuffer;
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
public class ApiUtil {
|
public class ApiUtil {
|
||||||
public static Mono<Void> getExampleResponse(ServerWebExchange exchange, String example) {
|
public static Mono<Void> getExampleResponse(ServerWebExchange exchange, MediaType mediaType, String example) {
|
||||||
return exchange.getResponse().writeWith(Mono.just(new DefaultDataBufferFactory().wrap(example.getBytes(StandardCharsets.UTF_8))));
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
response.getHeaders().setContentType(mediaType);
|
||||||
|
|
||||||
|
byte[] exampleBytes = example.getBytes(StandardCharsets.UTF_8);
|
||||||
|
DefaultDataBuffer data = new DefaultDataBufferFactory().wrap(exampleBytes);
|
||||||
|
return response.writeWith(Mono.just(data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ public interface FakeApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) {
|
||||||
String exampleString = "{ \"my_string\" : \"my_string\", \"my_number\" : 0.8008281904610115, \"my_boolean\" : true }";
|
String exampleString = "{ \"my_string\" : \"my_string\", \"my_number\" : 0.8008281904610115, \"my_boolean\" : true }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,7 +171,7 @@ public interface FakeApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"client\" : \"client\" }";
|
String exampleString = "{ \"client\" : \"client\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -353,7 +353,7 @@ public interface FakeApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }";
|
String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ public interface FakeClassnameTestApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"client\" : \"client\" }";
|
String exampleString = "{ \"client\" : \"client\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,12 +79,12 @@ public interface PetApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }";
|
String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
||||||
String exampleString = "<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status> </Pet>";
|
String exampleString = "<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status> </Pet>";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,12 +109,12 @@ public interface PetApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }";
|
String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
||||||
String exampleString = "<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status> </Pet>";
|
String exampleString = "<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status> </Pet>";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -139,12 +139,12 @@ public interface PetApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }";
|
String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
||||||
String exampleString = "<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status> </Pet>";
|
String exampleString = "<Pet> <id>123456789</id> <name>doggie</name> <photoUrls> <photoUrls>aeiou</photoUrls> </photoUrls> <tags> </tags> <status>aeiou</status> </Pet>";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -207,7 +207,7 @@ public interface PetApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }";
|
String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,12 +76,12 @@ public interface StoreApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\" }";
|
String exampleString = "{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
||||||
String exampleString = "<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete> </Order>";
|
String exampleString = "<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete> </Order>";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,12 +104,12 @@ public interface StoreApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\" }";
|
String exampleString = "{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
||||||
String exampleString = "<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete> </Order>";
|
String exampleString = "<Order> <id>123456789</id> <petId>123456789</petId> <quantity>123</quantity> <shipDate>2000-01-23T04:56:07.000Z</shipDate> <status>aeiou</status> <complete>true</complete> </Order>";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,12 +107,12 @@ public interface UserApiDelegate {
|
|||||||
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
|
||||||
String exampleString = "{ \"firstName\" : \"firstName\", \"lastName\" : \"lastName\", \"password\" : \"password\", \"userStatus\" : 6, \"phone\" : \"phone\", \"id\" : 0, \"email\" : \"email\", \"username\" : \"username\" }";
|
String exampleString = "{ \"firstName\" : \"firstName\", \"lastName\" : \"lastName\", \"password\" : \"password\", \"userStatus\" : 6, \"phone\" : \"phone\", \"id\" : 0, \"email\" : \"email\", \"username\" : \"username\" }";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
|
||||||
String exampleString = "<User> <id>123456789</id> <username>aeiou</username> <firstName>aeiou</firstName> <lastName>aeiou</lastName> <email>aeiou</email> <password>aeiou</password> <phone>aeiou</phone> <userStatus>123</userStatus> </User>";
|
String exampleString = "<User> <id>123456789</id> <username>aeiou</username> <firstName>aeiou</firstName> <lastName>aeiou</lastName> <email>aeiou</email> <password>aeiou</password> <phone>aeiou</phone> <userStatus>123</userStatus> </User>";
|
||||||
result = ApiUtil.getExampleResponse(exchange, exampleString);
|
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user