Skip to content

Commit f617db8

Browse files
authored
Added "return-response" property to return Response instead of a model (#219) (#223)
Signed-off-by: Helber Belmiro <[email protected]> Signed-off-by: Helber Belmiro <[email protected]>
1 parent 65b74ea commit f617db8

File tree

13 files changed

+713
-2
lines changed

13 files changed

+713
-2
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ public class PetResource {
120120

121121
See the [integration-tests](integration-tests) module for more information of how to use this extension. Please be advised that the extension is on experimental, early development stage.
122122

123+
## Returning `Response` objects
124+
125+
By default, this extension generates the methods according to their returning models based on the [OpenAPI specification Schema Object](https://spec.openapis.org/oas/v3.1.0#schema-object). If you want to return `javax.ws.rs.core.Response` instead, you can set the `return-response` property to `true`.
126+
127+
### Example
128+
129+
Given you want to return `javax.ws.rs.core.Response` for the `my-openapi.yaml` file, you must add the following to your `application.properties` file:
130+
131+
```properties
132+
quarkus.openapi-generator.codegen.spec.my_openapi_yaml.return-response=true
133+
```
134+
123135
## Logging
124136

125137
Since the most part of this extension work is in the `generate-code` execution phase of the Quarkus Maven's plugin, the log configuration must be set in the Maven context. When building your project, add `-Dorg.slf4j.simpleLogger.log.org.openapitools=off` to the `mvn` command to reduce the internal generator noise. For example:

deployment/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
<artifactId>validation-api</artifactId>
7171
</exclusion>
7272
<exclusion>
73-
<groupId>jakarta.xml.bind</groupId>
74-
<artifactId>jakarta.xml.bind-api</artifactId>
73+
<groupId>javax.xml.bind</groupId>
74+
<artifactId>javax.xml.bind-api</artifactId>
7575
</exclusion>
7676
<exclusion>
7777
<groupId>joda-time</groupId>

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/CodegenConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class CodegenConfig {
3333

3434
private static final String CUSTOM_REGISTER_PROVIDERS_FORMAT = "%s.custom-register-providers";
3535

36+
private static final String RETURN_RESPONSE_PROP_FORMAT = "%s.return-response";
37+
3638
/**
3739
* OpenAPI Spec details for codegen configuration.
3840
*/
@@ -103,4 +105,8 @@ public static String getSanitizedFileName(final Path openApiFilePath) {
103105
public static String getCustomRegisterProvidersFormat(final Path openApiFilePath) {
104106
return String.format(CUSTOM_REGISTER_PROVIDERS_FORMAT, getBuildTimeSpecPropertyPrefix(openApiFilePath));
105107
}
108+
109+
public static String getReturnResponsePropertyName(final Path openApiFilePath) {
110+
return String.format(RETURN_RESPONSE_PROP_FORMAT, getBuildTimeSpecPropertyPrefix(openApiFilePath));
111+
}
106112
}

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/SpecItemConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ public class SpecItemConfig {
5252
*/
5353
@ConfigItem(name = "custom-register-providers")
5454
public Optional<String> customRegisterProviders;
55+
56+
/**
57+
* Defines if the methods should return {@link javax.ws.rs.core.Response} or a model. Default is <code>false</code>.
58+
*/
59+
@ConfigItem(name = "return-response")
60+
public Optional<Boolean> returnResponse;
5561
}

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/codegen/OpenApiGeneratorCodeGenBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getBasePackagePropertyName;
1010
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getCustomRegisterProvidersFormat;
1111
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getImportMappingsPropertyName;
12+
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getReturnResponsePropertyName;
1213
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSanitizedFileName;
1314
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getSkipFormModelPropertyName;
1415
import static io.quarkiverse.openapi.generator.deployment.CodegenConfig.getTypeMappingsPropertyName;
@@ -123,6 +124,9 @@ protected void generate(final Config config, final Path openApiFilePath, final P
123124
config.getOptionalValue(getCustomRegisterProvidersFormat(openApiFilePath), String.class)
124125
.ifPresent(generator::withCustomRegisterProviders);
125126

127+
generator.withReturnResponse(config.getOptionalValue(getReturnResponsePropertyName(openApiFilePath), Boolean.class)
128+
.orElse(false));
129+
126130
SmallRyeConfig smallRyeConfig = config.unwrap(SmallRyeConfig.class);
127131
smallRyeConfig.getOptionalValues(getTypeMappingsPropertyName(openApiFilePath), String.class, String.class)
128132
.ifPresent(generator::withTypeMappings);

deployment/src/main/java/io/quarkiverse/openapi/generator/deployment/wrapper/OpenApiClientGeneratorWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public OpenApiClientGeneratorWrapper withTypeMappings(final Map<String, String>
127127
return this;
128128
}
129129

130+
public OpenApiClientGeneratorWrapper withReturnResponse(Boolean returnResponse) {
131+
configurator.addAdditionalProperty("return-response", returnResponse);
132+
return this;
133+
}
134+
130135
public OpenApiClientGeneratorWrapper withImportMappings(final Map<String, String> typeMappings) {
131136
typeMappings.forEach(configurator::addImportMapping);
132137
return this;

deployment/src/main/resources/templates/api.qute

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ public interface {classname} {
7474
@org.eclipse.microprofile.faulttolerance.CircuitBreaker
7575
{/if}{/for}
7676
{/if}{/for}
77+
{#if return-response}
78+
public javax.ws.rs.core.Response {op.nickname}(
79+
{#else}
7780
public {#if op.returnType}{op.returnType}{#else}void{/if} {op.nickname}(
81+
{/if}
7882
{#if op.hasFormParams}
7983
@org.jboss.resteasy.annotations.providers.multipart.MultipartForm {op.operationIdCamelCase}MultipartForm multipartForm{#if op.hasPathParams},{/if}{!
8084
!}{#for p in op.pathParams}{#include pathParams.qute param=p/}{#if p_hasNext}, {/if}{/for}{#if op.hasQueryParams},{/if}{!

integration-tests/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<module>include</module>
2525
<module>exclude</module>
2626
<module>change-directory</module>
27+
<module>return-response</module>
2728
</modules>
2829
<dependencyManagement>
2930
<dependencies>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<parent>
5+
<artifactId>quarkus-openapi-generator-integration-tests</artifactId>
6+
<groupId>io.quarkiverse.openapi.generator</groupId>
7+
<version>2.0.0-SNAPSHOT</version>
8+
</parent>
9+
<modelVersion>4.0.0</modelVersion>
10+
11+
<artifactId>quarkus-openapi-generator-it-return-response</artifactId>
12+
<name>Quarkus - Openapi Generator - Integration Tests - return-response</name>
13+
<description>Example project for usage of the return-response property</description>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>io.quarkiverse.openapi.generator</groupId>
18+
<artifactId>quarkus-openapi-generator</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.assertj</groupId>
22+
<artifactId>assertj-core</artifactId>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.quarkus</groupId>
27+
<artifactId>quarkus-junit5</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>io.quarkus</groupId>
35+
<artifactId>quarkus-maven-plugin</artifactId>
36+
<extensions>true</extensions>
37+
<executions>
38+
<execution>
39+
<goals>
40+
<goal>build</goal>
41+
<goal>generate-code</goal>
42+
<goal>generate-code-tests</goal>
43+
</goals>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
<profiles>
50+
<profile>
51+
<id>native-image</id>
52+
<activation>
53+
<property>
54+
<name>native</name>
55+
</property>
56+
</activation>
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<configuration>
62+
<skipTests>${native.surefire.skip}</skipTests>
63+
</configuration>
64+
</plugin>
65+
<plugin>
66+
<artifactId>maven-failsafe-plugin</artifactId>
67+
<executions>
68+
<execution>
69+
<goals>
70+
<goal>integration-test</goal>
71+
<goal>verify</goal>
72+
</goals>
73+
<configuration>
74+
<systemPropertyVariables>
75+
<native.image.path>
76+
${project.build.directory}/${project.build.finalName}-runner
77+
</native.image.path>
78+
<java.util.logging.manager>org.jboss.logmanager.LogManager
79+
</java.util.logging.manager>
80+
<maven.home>${maven.home}</maven.home>
81+
</systemPropertyVariables>
82+
</configuration>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
87+
</build>
88+
<properties>
89+
<quarkus.package.type>native</quarkus.package.type>
90+
</properties>
91+
</profile>
92+
</profiles>
93+
94+
</project>

0 commit comments

Comments
 (0)