Skip to content

Commit 221c6c0

Browse files
authored
Merge pull request #2106 from tobilarscheid/enhancement/use-custom-json-serialization
use custom object mapper to serialize json in order to avoid null values
2 parents f6051e3 + 3ad7595 commit 221c6c0

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/listing/AcceptHeaderApiListingResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.jaxrs.listing;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import io.swagger.annotations.ApiOperation;
45

56
import javax.servlet.ServletConfig;
@@ -25,7 +26,7 @@ public Response getListingJson(
2526
@Context Application app,
2627
@Context ServletConfig sc,
2728
@Context HttpHeaders headers,
28-
@Context UriInfo uriInfo) {
29+
@Context UriInfo uriInfo) throws JsonProcessingException {
2930
return getListingJsonResponse(app, context, sc, headers, uriInfo);
3031
}
3132

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/listing/ApiListingResource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.jaxrs.listing;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import io.swagger.annotations.ApiOperation;
45
import org.apache.commons.lang3.StringUtils;
56

@@ -25,7 +26,7 @@ public Response getListing(
2526
@Context ServletConfig sc,
2627
@Context HttpHeaders headers,
2728
@Context UriInfo uriInfo,
28-
@PathParam("type") String type) {
29+
@PathParam("type") String type) throws JsonProcessingException {
2930
if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
3031
return getListingYamlResponse(app, context, sc, headers, uriInfo);
3132
} else {

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/listing/BaseApiListingResource.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger.jaxrs.listing;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import io.swagger.config.FilterFactory;
45
import io.swagger.config.Scanner;
56
import io.swagger.config.SwaggerConfig;
@@ -10,6 +11,7 @@
1011
import io.swagger.jaxrs.config.ReaderConfigUtils;
1112
import io.swagger.jaxrs.config.SwaggerContextService;
1213
import io.swagger.models.Swagger;
14+
import io.swagger.util.Json;
1315
import io.swagger.util.Yaml;
1416
import org.slf4j.Logger;
1517
import org.slf4j.LoggerFactory;
@@ -161,11 +163,11 @@ protected Response getListingJsonResponse(
161163
ServletContext servletContext,
162164
ServletConfig servletConfig,
163165
HttpHeaders headers,
164-
UriInfo uriInfo) {
166+
UriInfo uriInfo) throws JsonProcessingException {
165167
Swagger swagger = process(app, servletContext, servletConfig, headers, uriInfo);
166168

167169
if (swagger != null) {
168-
return Response.ok().entity(swagger).build();
170+
return Response.ok().entity(Json.mapper().writeValueAsString(swagger)).type(MediaType.APPLICATION_JSON_TYPE).build();
169171
} else {
170172
return Response.status(404).build();
171173
}

modules/swagger-jaxrs/src/test/java/io/swagger/ApiListingResourceTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.swagger;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import io.swagger.jaxrs.Reader;
45
import io.swagger.jaxrs.listing.ApiListingResource;
56
import io.swagger.models.Swagger;
@@ -24,7 +25,7 @@ public void shouldCheckModelsSet() {
2425
}
2526

2627
@Test
27-
public void shouldHandleNullServletConfig_issue1689() {
28+
public void shouldHandleNullServletConfig_issue1689() throws JsonProcessingException {
2829
ApiListingResource a = new ApiListingResource();
2930
try {
3031
a.getListing(null, null, null, null, "json");
@@ -38,7 +39,7 @@ public void shouldHandleNullServletConfig_issue1689() {
3839

3940
}
4041
@Test
41-
public void shouldHandleErrorServletConfig_issue1691() {
42+
public void shouldHandleErrorServletConfig_issue1691() throws JsonProcessingException {
4243

4344
ServletConfig sc = new ServletConfig() {
4445
@Override

0 commit comments

Comments
 (0)