Skip to content

Commit aeec0e2

Browse files
committed
Issue 7440 Added support for returning response in jaxrs-spec interfaces.
1 parent 9e1bbe0 commit aeec0e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6020
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l jaxrs-spec -o samples/server/petstore/jaxrs-spec-interface-response
30+
-DhideGenerationTimestamp=true
31+
-DserializableModel=true
32+
-DinterfaceOnly=true
33+
-DreturnResponse=true"
34+
35+
java $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegen.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ public class JavaJAXRSSpecServerCodegen extends AbstractJavaJAXRSServerCodegen
2323
{
2424

2525
public static final String INTERFACE_ONLY = "interfaceOnly";
26+
public static final String RETURN_RESPONSE = "returnResponse";
2627
public static final String GENERATE_POM = "generatePom";
2728

2829
private boolean interfaceOnly = false;
30+
private boolean returnResponse = false;
2931
private boolean generatePom = true;
3032

3133
public JavaJAXRSSpecServerCodegen()
@@ -75,6 +77,7 @@ public JavaJAXRSSpecServerCodegen()
7577
cliOptions.add(library);
7678
cliOptions.add(CliOption.newBoolean(GENERATE_POM, "Whether to generate pom.xml if the file does not already exist.").defaultValue(String.valueOf(generatePom)));
7779
cliOptions.add(CliOption.newBoolean(INTERFACE_ONLY, "Whether to generate only API interface stubs without the server files.").defaultValue(String.valueOf(interfaceOnly)));
80+
cliOptions.add(CliOption.newBoolean(RETURN_RESPONSE, "Whether generate API interface should return javax.ws.rs.core.Response instead of a deserialized entity. Only useful if interfaceOnly is true.").defaultValue(String.valueOf(returnResponse)));
7881
}
7982

8083
@Override
@@ -86,6 +89,12 @@ public void processOpts()
8689
if (additionalProperties.containsKey(INTERFACE_ONLY)) {
8790
interfaceOnly = Boolean.valueOf(additionalProperties.get(INTERFACE_ONLY).toString());
8891
}
92+
if (additionalProperties.containsKey(RETURN_RESPONSE)) {
93+
returnResponse = Boolean.valueOf(additionalProperties.get(RETURN_RESPONSE).toString());
94+
if (!returnResponse) {
95+
additionalProperties.remove(RETURN_RESPONSE);
96+
}
97+
}
8998
if (interfaceOnly) {
9099
// Change default artifactId if genereating interfaces only, before command line options are applied in base class.
91100
artifactId = "swagger-jaxrs-client";

modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/apiInterface.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
}{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} })
1212
@ApiResponses(value = { {{#responses}}
1313
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}){{#hasMore}},{{/hasMore}}{{/responses}} })
14-
{{>returnTypeInterface}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}});
14+
{{#returnResponse}}Response{{/returnResponse}}{{^returnResponse}}{{>returnTypeInterface}}{{/returnResponse}} {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}});

modules/swagger-codegen/src/main/resources/JavaJaxRS/spec/apiMethod.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
{{/hasMore}}{{/authMethods}}
1111
}{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} })
1212
@ApiResponses(value = { {{#responses}}
13-
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#containerType}}, responseContainer = "{{{containerType}}}"{{/containerType}}){{#hasMore}},{{/hasMore}}{{/responses}} })
13+
@ApiResponse(code = {{{code}}}, message = "{{{message}}}", response = {{{baseType}}}.class{{#containerType}}, responseContainer = "{{{containerType}}}"{{/containerType}}){{#hasMore}},{{/hasMore}}{{/responses}}
14+
})
1415
public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
1516
return Response.ok().entity("magic!").build();
1617
}

modules/swagger-codegen/src/test/java/io/swagger/codegen/languages/JavaJAXRSSpecServerCodegenTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ public void verify_that_generatePom_exists_as_a_parameter_with_default_true() {
6363
Assert.fail("Missing " + JavaJAXRSSpecServerCodegen.GENERATE_POM);
6464
}
6565

66+
@Test
67+
public void verify_that_returnResponse_is_removed_from_additional_properties_if_false() {
68+
generator.additionalProperties().put(JavaJAXRSSpecServerCodegen.RETURN_RESPONSE, Boolean.FALSE.toString());
69+
generator.processOpts();
70+
Assert.assertFalse(generator.additionalProperties().containsKey(JavaJAXRSSpecServerCodegen.RETURN_RESPONSE));
71+
}
72+
73+
@Test
74+
public void verify_that_returnResponse_is_preserved_in_additional_properties_if_true() {
75+
generator.additionalProperties().put(JavaJAXRSSpecServerCodegen.RETURN_RESPONSE, Boolean.TRUE.toString());
76+
generator.processOpts();
77+
Assert.assertTrue(generator.additionalProperties().containsKey(JavaJAXRSSpecServerCodegen.RETURN_RESPONSE));
78+
}
79+
6680
@Test
6781
public void verify_that_interfaceOnly_exists_as_a_parameter_with_default_false() {
6882
for (CliOption option : generator.cliOptions()) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.4.0-SNAPSHOT
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>io.swagger</groupId>
4+
<artifactId>swagger-jaxrs-client</artifactId>
5+
<packaging>jar</packaging>
6+
<name>swagger-jaxrs-client</name>
7+
<version>1.0.0</version>
8+
<build>
9+
<sourceDirectory>src/main/java</sourceDirectory>
10+
<plugins>
11+
<plugin>
12+
<groupId>org.apache.maven.plugins</groupId>
13+
<artifactId>maven-jar-plugin</artifactId>
14+
<version>2.2</version>
15+
</plugin>
16+
</plugins>
17+
</build>
18+
<dependencies>
19+
<dependency>
20+
<groupId>javax.ws.rs</groupId>
21+
<artifactId>javax.ws.rs-api</artifactId>
22+
<version>2.0</version>
23+
<scope>provided</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.swagger</groupId>
27+
<artifactId>swagger-annotations</artifactId>
28+
<scope>provided</scope>
29+
<version>1.5.3</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<version>${junit-version}</version>
35+
<scope>test</scope>
36+
</dependency>
37+
<!-- Bean Validation API support -->
38+
<dependency>
39+
<groupId>javax.validation</groupId>
40+
<artifactId>validation-api</artifactId>
41+
<version>1.1.0.Final</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
</dependencies>
45+
<properties>
46+
<junit-version>4.8.1</junit-version>
47+
</properties>
48+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.swagger.api;
2+
3+
import io.swagger.model.Client;
4+
5+
import javax.ws.rs.*;
6+
import javax.ws.rs.core.Response;
7+
8+
import io.swagger.annotations.*;
9+
10+
import java.util.Map;
11+
import java.util.List;
12+
import javax.validation.constraints.*;
13+
import javax.validation.Valid;
14+
15+
@Path("/another-fake")
16+
@Api(description = "the another-fake API")
17+
public interface AnotherFakeApi {
18+
19+
@PATCH
20+
@Path("/dummy")
21+
@Consumes({ "application/json" })
22+
@Produces({ "application/json" })
23+
@ApiOperation(value = "To test special tags", notes = "To test special tags", tags={ "$another-fake?" })
24+
@ApiResponses(value = {
25+
@ApiResponse(code = 200, message = "successful operation", response = Client.class) })
26+
Response testSpecialTags(@Valid Client body);
27+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.swagger.api;
2+
3+
import java.math.BigDecimal;
4+
import io.swagger.model.Client;
5+
import java.util.Date;
6+
import org.joda.time.LocalDate;
7+
import io.swagger.model.OuterComposite;
8+
9+
import javax.ws.rs.*;
10+
import javax.ws.rs.core.Response;
11+
12+
import io.swagger.annotations.*;
13+
14+
import java.util.Map;
15+
import java.util.List;
16+
import javax.validation.constraints.*;
17+
import javax.validation.Valid;
18+
19+
@Path("/fake")
20+
@Api(description = "the fake API")
21+
public interface FakeApi {
22+
23+
@POST
24+
@Path("/outer/boolean")
25+
@ApiOperation(value = "", notes = "Test serialization of outer boolean types", tags={ "fake", })
26+
@ApiResponses(value = {
27+
@ApiResponse(code = 200, message = "Output boolean", response = Boolean.class) })
28+
Response fakeOuterBooleanSerialize(@Valid Boolean body);
29+
30+
@POST
31+
@Path("/outer/composite")
32+
@ApiOperation(value = "", notes = "Test serialization of object with outer number type", tags={ "fake", })
33+
@ApiResponses(value = {
34+
@ApiResponse(code = 200, message = "Output composite", response = OuterComposite.class) })
35+
Response fakeOuterCompositeSerialize(@Valid OuterComposite body);
36+
37+
@POST
38+
@Path("/outer/number")
39+
@ApiOperation(value = "", notes = "Test serialization of outer number types", tags={ "fake", })
40+
@ApiResponses(value = {
41+
@ApiResponse(code = 200, message = "Output number", response = BigDecimal.class) })
42+
Response fakeOuterNumberSerialize(@Valid BigDecimal body);
43+
44+
@POST
45+
@Path("/outer/string")
46+
@ApiOperation(value = "", notes = "Test serialization of outer string types", tags={ "fake", })
47+
@ApiResponses(value = {
48+
@ApiResponse(code = 200, message = "Output string", response = String.class) })
49+
Response fakeOuterStringSerialize(@Valid String body);
50+
51+
@PATCH
52+
@Consumes({ "application/json" })
53+
@Produces({ "application/json" })
54+
@ApiOperation(value = "To test \"client\" model", notes = "To test \"client\" model", tags={ "fake", })
55+
@ApiResponses(value = {
56+
@ApiResponse(code = 200, message = "successful operation", response = Client.class) })
57+
Response testClientModel(@Valid Client body);
58+
59+
@POST
60+
@Consumes({ "application/xml; charset=utf-8", "application/json; charset=utf-8" })
61+
@Produces({ "application/xml; charset=utf-8", "application/json; charset=utf-8" })
62+
@ApiOperation(value = "Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 ", notes = "Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 ", authorizations = {
63+
@Authorization(value = "http_basic_test")
64+
}, tags={ "fake", })
65+
@ApiResponses(value = {
66+
@ApiResponse(code = 400, message = "Invalid username supplied", response = Void.class),
67+
@ApiResponse(code = 404, message = "User not found", response = Void.class) })
68+
Response testEndpointParameters(@FormParam(value = "number") BigDecimal number,@FormParam(value = "double") Double _double,@FormParam(value = "pattern_without_delimiter") String patternWithoutDelimiter,@FormParam(value = "byte") byte[] _byte,@FormParam(value = "integer") Integer integer,@FormParam(value = "int32") Integer int32,@FormParam(value = "int64") Long int64,@FormParam(value = "float") Float _float,@FormParam(value = "string") String string,@FormParam(value = "binary") byte[] binary,@FormParam(value = "date") LocalDate date,@FormParam(value = "dateTime") Date dateTime,@FormParam(value = "password") String password,@FormParam(value = "callback") String paramCallback);
69+
70+
@GET
71+
@Consumes({ "*/*" })
72+
@Produces({ "*/*" })
73+
@ApiOperation(value = "To test enum parameters", notes = "To test enum parameters", tags={ "fake", })
74+
@ApiResponses(value = {
75+
@ApiResponse(code = 400, message = "Invalid request", response = Void.class),
76+
@ApiResponse(code = 404, message = "Not found", response = Void.class) })
77+
Response testEnumParameters(@FormParam(value = "enum_form_string_array") List<String> enumFormStringArray,@FormParam(value = "enum_form_string") String enumFormString,@HeaderParam("enum_header_string_array") @ApiParam("Header parameter enum test (string array)") List<String> enumHeaderStringArray,@HeaderParam("enum_header_string") @DefaultValue("-efg") @ApiParam("Header parameter enum test (string)") String enumHeaderString,@QueryParam("enum_query_string_array") @ApiParam("Query parameter enum test (string array)") List<String> enumQueryStringArray,@QueryParam("enum_query_string") @DefaultValue("-efg") @ApiParam("Query parameter enum test (string)") String enumQueryString,@QueryParam("enum_query_integer") @ApiParam("Query parameter enum test (double)") Integer enumQueryInteger,@FormParam(value = "enum_query_double") Double enumQueryDouble);
78+
79+
@POST
80+
@Path("/inline-additionalProperties")
81+
@Consumes({ "application/json" })
82+
@ApiOperation(value = "test inline additionalProperties", notes = "", tags={ "fake", })
83+
@ApiResponses(value = {
84+
@ApiResponse(code = 200, message = "successful operation", response = Void.class) })
85+
Response testInlineAdditionalProperties(@Valid Object param);
86+
87+
@GET
88+
@Path("/jsonFormData")
89+
@Consumes({ "application/json" })
90+
@ApiOperation(value = "test json serialization of form data", notes = "", tags={ "fake" })
91+
@ApiResponses(value = {
92+
@ApiResponse(code = 200, message = "successful operation", response = Void.class) })
93+
Response testJsonFormData(@FormParam(value = "param") String param,@FormParam(value = "param2") String param2);
94+
}

0 commit comments

Comments
 (0)