Skip to content

Commit fab5000

Browse files
committed
refs #4455 - add config properties for default response status code
1 parent 9048843 commit fab5000

File tree

15 files changed

+145
-16
lines changed

15 files changed

+145
-16
lines changed

modules/swagger-gradle-plugin/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Parameter | Description | Required | Default
9696
`modelConverterClasses`|see [configuration property](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties)|false|
9797
`contextId`|see [Context](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#context)|false|
9898
`outputPath`|**DEPRECATED** output path where file(s) are saved|false|
99-
99+
`defaultResponseCode`|see [configuration property](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties)|false|
100100

101101
**Note** parameter `openApiFile` corresponds to [config](https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Integration-and-Configuration#configuration-properties) openAPI. It points to a location of a file in YAML or JSON format representing the input spec that will be merged with the resolved spec. Typically used to add Info section, or any other meta data.
102102
An example of such file:
@@ -122,3 +122,4 @@ Since version 2.1.6, `sortOutput` parameter is available, allowing to sort objec
122122
Since version 2.1.6, `objectMapperProcessorClass` allows to configure also the ObjectMapper instance used to serialize the resolved OpenAPI
123123
Since version 2.1.9, `alwaysResolveAppPath` parameter is available, allowing to trigger resolving of Application Path from annotation also not in runtime (e.g. using servlet in separate application, or in maven plugin at build time, etc)
124124
Since version 2.1.15, `skipResolveAppPath` parameter is available, allowing to skip resolving of Application Path from annotation
125+
Since version 2.2.17, `defaultResponseCode` parameter is available, allowing to set the code used when resolving responses with no http status code annotation

modules/swagger-gradle-plugin/src/main/java/io/swagger/v3/plugins/gradle/tasks/ResolveTask.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public enum Format {JSON, YAML, JSONANDYAML};
7777

7878
private Boolean convertToOpenAPI31 = false;
7979

80+
private String defaultResponseCode;
81+
8082
@Input
8183
@Optional
8284
public String getOutputFileName() {
@@ -227,6 +229,22 @@ public void setObjectMapperProcessorClass(String objectMapperProcessorClass) {
227229
this.objectMapperProcessorClass = objectMapperProcessorClass;
228230
}
229231

232+
/**
233+
* @since 2.2.17
234+
*/
235+
@Input
236+
@Optional
237+
public String getDefaultResponseCode() {
238+
return defaultResponseCode;
239+
}
240+
241+
/**
242+
* @since 2.2.17
243+
*/
244+
public void setDefaultResponseCode(String defaultResponseCode) {
245+
this.defaultResponseCode = defaultResponseCode;
246+
}
247+
230248
/**
231249
* @since 2.0.6
232250
*/
@@ -461,6 +479,11 @@ public void resolve() throws GradleException {
461479
method.invoke(swaggerLoader, objectMapperProcessorClass);
462480
}
463481

482+
if (StringUtils.isNotBlank(defaultResponseCode)) {
483+
method=swaggerLoaderClass.getDeclaredMethod("setDefaultResponseCode",String.class);
484+
method.invoke(swaggerLoader, defaultResponseCode);
485+
}
486+
464487
method=swaggerLoaderClass.getDeclaredMethod("setPrettyPrint", Boolean.class);
465488
method.invoke(swaggerLoader, prettyPrint);
466489

modules/swagger-integration/src/main/java/io/swagger/v3/oas/integration/GenericOpenApiContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ private OpenAPIConfiguration mergeParentConfiguration(OpenAPIConfiguration confi
630630
if (merged.isConvertToOpenAPI31() == null) {
631631
merged.setConvertToOpenAPI31(parentConfig.isConvertToOpenAPI31());
632632
}
633+
if (merged.getDefaultResponseCode() == null) {
634+
merged.setDefaultResponseCode(parentConfig.getDefaultResponseCode());
635+
}
633636

634637
return merged;
635638
}

modules/swagger-integration/src/main/java/io/swagger/v3/oas/integration/SwaggerConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ public class SwaggerConfiguration implements OpenAPIConfiguration {
4040

4141
private Boolean convertToOpenAPI31;
4242

43+
@Override
44+
public String getDefaultResponseCode() {
45+
return defaultResponseCode;
46+
}
47+
48+
public void setDefaultResponseCode(String defaultResponseCode) {
49+
this.defaultResponseCode = defaultResponseCode;
50+
}
51+
52+
public SwaggerConfiguration defaultResponseCode(String defaultResponseCode) {
53+
this.defaultResponseCode = defaultResponseCode;
54+
return this;
55+
}
56+
57+
private String defaultResponseCode;
58+
4359
public Long getCacheTTL() {
4460
return cacheTTL;
4561
}

modules/swagger-integration/src/main/java/io/swagger/v3/oas/integration/api/OpenAPIConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,9 @@ public interface OpenAPIConfiguration {
6363
* @since 2.2.12
6464
*/
6565
Boolean isConvertToOpenAPI31();
66+
67+
/**
68+
* @since 2.2.17
69+
*/
70+
public String getDefaultResponseCode();
6671
}

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/OperationParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public static Optional<RequestBody> getRequestBody(io.swagger.v3.oas.annotations
6262
}
6363

6464
public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.annotations.responses.ApiResponse[] responses, Produces classProduces, Produces methodProduces, Components components, JsonView jsonViewAnnotation) {
65-
return getApiResponses(responses, classProduces, methodProduces, components, jsonViewAnnotation, false);
65+
return getApiResponses(responses, classProduces, methodProduces, components, jsonViewAnnotation, false, ApiResponses.DEFAULT);
6666
}
6767

68-
public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.annotations.responses.ApiResponse[] responses, Produces classProduces, Produces methodProduces, Components components, JsonView jsonViewAnnotation, boolean openapi31) {
68+
public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.annotations.responses.ApiResponse[] responses, Produces classProduces, Produces methodProduces, Components components, JsonView jsonViewAnnotation, boolean openapi31, String defaultResponseKey) {
6969
if (responses == null) {
7070
return Optional.empty();
7171
}
@@ -103,7 +103,7 @@ public static Optional<ApiResponses> getApiResponses(final io.swagger.v3.oas.ann
103103
if (StringUtils.isNotBlank(response.responseCode())) {
104104
apiResponsesObject.addApiResponse(response.responseCode(), apiResponseObject);
105105
} else {
106-
apiResponsesObject._default(apiResponseObject);
106+
apiResponsesObject.addApiResponse(defaultResponseKey, apiResponseObject);
107107
}
108108
}
109109
}

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/Reader.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public class Reader implements OpenApiReader {
8888
private Paths paths;
8989
private Set<Tag> openApiTags;
9090

91+
private String defaultResponseKey = ApiResponses.DEFAULT;
92+
9193
private static final String GET_METHOD = "get";
9294
private static final String POST_METHOD = "post";
9395
private static final String PUT_METHOD = "put";
@@ -216,6 +218,7 @@ public void setConfiguration(OpenAPIConfiguration openApiConfiguration) {
216218
this.components = this.openAPI.getComponents();
217219
}
218220
}
221+
this.defaultResponseKey = StringUtils.isBlank(config.getDefaultResponseCode()) ? ApiResponses.DEFAULT : config.getDefaultResponseCode();
219222
}
220223
}
221224

@@ -1083,7 +1086,9 @@ protected Operation parseMethod(
10831086
classProduces,
10841087
methodProduces,
10851088
components,
1086-
jsonViewAnnotation
1089+
jsonViewAnnotation,
1090+
config.isOpenAPI31(),
1091+
defaultResponseKey
10871092
).ifPresent(responses -> {
10881093
if (operation.getResponses() == null) {
10891094
operation.setResponses(responses);
@@ -1104,7 +1109,9 @@ protected Operation parseMethod(
11041109
classProduces,
11051110
methodProduces,
11061111
components,
1107-
jsonViewAnnotation
1112+
jsonViewAnnotation,
1113+
config.isOpenAPI31(),
1114+
defaultResponseKey
11081115
).ifPresent(responses -> {
11091116
if (operation.getResponses() == null) {
11101117
operation.setResponses(responses);
@@ -1164,20 +1171,20 @@ protected Operation parseMethod(
11641171
methodProduces == null ? new String[0] : methodProduces.value(), content, mediaType);
11651172
if (operation.getResponses() == null) {
11661173
operation.responses(
1167-
new ApiResponses()._default(
1174+
new ApiResponses().addApiResponse(defaultResponseKey,
11681175
new ApiResponse().description(DEFAULT_DESCRIPTION)
11691176
.content(content)
11701177
)
11711178
);
11721179
}
1173-
if (operation.getResponses().getDefault() != null &&
1174-
StringUtils.isBlank(operation.getResponses().getDefault().get$ref())) {
1175-
if (operation.getResponses().getDefault().getContent() == null) {
1176-
operation.getResponses().getDefault().content(content);
1180+
if (operation.getResponses().get(defaultResponseKey) != null &&
1181+
StringUtils.isBlank(operation.getResponses().get(defaultResponseKey).get$ref())) {
1182+
if (operation.getResponses().get(defaultResponseKey).getContent() == null) {
1183+
operation.getResponses().get(defaultResponseKey).content(content);
11771184
} else {
1178-
for (String key : operation.getResponses().getDefault().getContent().keySet()) {
1179-
if (operation.getResponses().getDefault().getContent().get(key).getSchema() == null) {
1180-
operation.getResponses().getDefault().getContent().get(key).setSchema(returnTypeSchema);
1185+
for (String key : operation.getResponses().get(defaultResponseKey).getContent().keySet()) {
1186+
if (operation.getResponses().get(defaultResponseKey).getContent().get(key).getSchema() == null) {
1187+
operation.getResponses().get(defaultResponseKey).getContent().get(key).setSchema(returnTypeSchema);
11811188
}
11821189
}
11831190
}
@@ -1414,7 +1421,7 @@ protected void setOperationObjectFromApiOperationAnnotation(
14141421
AnnotationsUtils.getExternalDocumentation(apiOperation.externalDocs(), openapi31).ifPresent(operation::setExternalDocs);
14151422
}
14161423

1417-
OperationParser.getApiResponses(apiOperation.responses(), classProduces, methodProduces, components, jsonViewAnnotation, openapi31).ifPresent(responses -> {
1424+
OperationParser.getApiResponses(apiOperation.responses(), classProduces, methodProduces, components, jsonViewAnnotation, openapi31, defaultResponseKey).ifPresent(responses -> {
14181425
if (operation.getResponses() == null) {
14191426
operation.setResponses(responses);
14201427
} else {

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/integration/ServletConfigContextUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public class ServletConfigContextUtils {
4646
*/
4747
public static final String OPENAPI_CONFIGURATION_OBJECT_MAPPER_PROCESSOR_KEY = "openApi.configuration.objectMapperProcessorClass";
4848

49+
/**
50+
* @since 2.2.17
51+
*/
52+
public static final String OPENAPI_CONFIGURATION_DEFAULT_RESPONSE_CODE_KEY = "openApi.configuration.defaultResponseCode";
53+
4954
/**
5055
* @since 2.0.6
5156
*/

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/integration/ServletOpenApiConfigurationLoader.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_CACHE_TTL_KEY;
1818
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_FILTER_KEY;
1919
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_OBJECT_MAPPER_PROCESSOR_KEY;
20+
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_DEFAULT_RESPONSE_CODE_KEY;
2021
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_OPENAPI_31_KEY;
2122
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_CONVERT_TO_OPENAPI_31_KEY;
2223
import static io.swagger.v3.jaxrs2.integration.ServletConfigContextUtils.OPENAPI_CONFIGURATION_PRETTYPRINT_KEY;
@@ -65,6 +66,7 @@ public OpenAPIConfiguration load(String path) throws IOException {
6566
.cacheTTL(getLongInitParam(servletConfig, OPENAPI_CONFIGURATION_CACHE_TTL_KEY))
6667
.scannerClass(getInitParam(servletConfig, OPENAPI_CONFIGURATION_SCANNER_KEY))
6768
.objectMapperProcessorClass(getInitParam(servletConfig, OPENAPI_CONFIGURATION_OBJECT_MAPPER_PROCESSOR_KEY))
69+
.defaultResponseCode(getInitParam(servletConfig, OPENAPI_CONFIGURATION_DEFAULT_RESPONSE_CODE_KEY))
6870
.openAPI31(getBooleanInitParam(servletConfig, OPENAPI_CONFIGURATION_OPENAPI_31_KEY))
6971
.convertToOpenAPI31(getBooleanInitParam(servletConfig, OPENAPI_CONFIGURATION_CONVERT_TO_OPENAPI_31_KEY))
7072
.modelConverterClasses(resolveModelConverterClasses(servletConfig));
@@ -138,6 +140,9 @@ public boolean exists(String path) {
138140
if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_OBJECT_MAPPER_PROCESSOR_KEY) != null) {
139141
return true;
140142
}
143+
if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_DEFAULT_RESPONSE_CODE_KEY) != null) {
144+
return true;
145+
}
141146
if (getInitParam(servletConfig, OPENAPI_CONFIGURATION_CONVERT_TO_OPENAPI_31_KEY) != null) {
142147
return true;
143148
}

modules/swagger-jaxrs2/src/main/java/io/swagger/v3/jaxrs2/integration/SwaggerLoader.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class SwaggerLoader {
3636
private String openapiAsString;
3737

3838
private String objectMapperProcessorClass;
39+
private String defaultResponseCode;
3940
private String modelConverterClasses;
4041

4142
private Boolean sortOutput = false;
@@ -62,6 +63,20 @@ public void setObjectMapperProcessorClass(String objectMapperProcessorClass) {
6263
this.objectMapperProcessorClass = objectMapperProcessorClass;
6364
}
6465

66+
/**
67+
* @since 2.2.17
68+
*/
69+
public String getDefaultResponseCode() {
70+
return defaultResponseCode;
71+
}
72+
73+
/**
74+
* @since 2.2.17
75+
*/
76+
public void setDefaultResponseCode(String defaultResponseCode) {
77+
this.defaultResponseCode = defaultResponseCode;
78+
}
79+
6580
/**
6681
* @since 2.0.6
6782
*/
@@ -279,6 +294,7 @@ public Map<String, String> resolve() throws Exception{
279294
.resourceClasses(resourceClassesSet)
280295
.resourcePackages(resourcePackagesSet)
281296
.objectMapperProcessorClass(objectMapperProcessorClass)
297+
.defaultResponseCode(defaultResponseCode)
282298
.modelConverterClasses(modelConverterSet)
283299
.sortOutput(sortOutput)
284300
.alwaysResolveAppPath(alwaysResolveAppPath)

0 commit comments

Comments
 (0)