Skip to content

Commit 7d14a26

Browse files
refactor: replace writer(new DefaultPrettyPrinter()) with writerWithDefaultPrettyPrinter() (#5252)
* refactor: replace mapper().writer(new DefaultPrettyPrinter()) with writerWithDefaultPrettyPrinter() * add test to show custom pretty printer is honored now --------- Co-authored-by: Ewa Ostrowska <ewa.ostrowska@smartbear.com>
1 parent b9b2d8c commit 7d14a26

14 files changed

Lines changed: 52 additions & 33 deletions

File tree

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json.java

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

3-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
43
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.fasterxml.jackson.databind.ObjectWriter;
65
import org.slf4j.Logger;
@@ -19,7 +18,7 @@ public static ObjectMapper mapper() {
1918
}
2019

2120
public static ObjectWriter pretty() {
22-
return mapper().writer(new DefaultPrettyPrinter());
21+
return mapper().writerWithDefaultPrettyPrinter();
2322
}
2423

2524
public static String pretty(Object o) {

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Json31.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.swagger.v3.core.util;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
54
import com.fasterxml.jackson.databind.JsonNode;
65
import com.fasterxml.jackson.databind.ObjectMapper;
76
import com.fasterxml.jackson.databind.ObjectWriter;
@@ -32,7 +31,7 @@ public static ObjectMapper converterMapper() {
3231
}
3332

3433
public static ObjectWriter pretty() {
35-
return mapper().writer(new DefaultPrettyPrinter());
34+
return mapper().writerWithDefaultPrettyPrinter();
3635
}
3736

3837
public static String pretty(Object o) {

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml.java

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

3-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
43
import com.fasterxml.jackson.databind.ObjectMapper;
54
import com.fasterxml.jackson.databind.ObjectWriter;
65
import org.slf4j.Logger;
@@ -19,7 +18,7 @@ public static ObjectMapper mapper() {
1918
}
2019

2120
public static ObjectWriter pretty() {
22-
return mapper().writer(new DefaultPrettyPrinter());
21+
return mapper().writerWithDefaultPrettyPrinter();
2322
}
2423

2524
public static String pretty(Object o) {

modules/swagger-core/src/main/java/io/swagger/v3/core/util/Yaml31.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.swagger.v3.core.util;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
54
import com.fasterxml.jackson.databind.ObjectMapper;
65
import com.fasterxml.jackson.databind.ObjectWriter;
76
import io.swagger.v3.oas.models.media.Schema;
@@ -24,7 +23,7 @@ public static ObjectMapper mapper() {
2423
}
2524

2625
public static ObjectWriter pretty() {
27-
return mapper().writer(new DefaultPrettyPrinter());
26+
return mapper().writerWithDefaultPrettyPrinter();
2827
}
2928

3029
public static String pretty(Object o) {

modules/swagger-core/src/test/java/io/swagger/v3/core/resolving/SwaggerTestBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.swagger.v3.core.resolving;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
54
import com.fasterxml.jackson.databind.DeserializationFeature;
65
import com.fasterxml.jackson.databind.ObjectMapper;
76
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -29,7 +28,7 @@ protected ModelResolver modelResolver() {
2928

3029
protected void prettyPrint(Object o) {
3130
try {
32-
LOGGER.debug(mapper().writer(new DefaultPrettyPrinter()).writeValueAsString(o));
31+
LOGGER.debug(mapper().writerWithDefaultPrettyPrinter().writeValueAsString(o));
3332
} catch (Exception e) {
3433
LOGGER.error("Failed to pretty print object", e);
3534
}

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/JsonSerializationTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.swagger.v3.core.serialization;
22

33
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.core.util.DefaultIndenter;
5+
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
46
import com.fasterxml.jackson.dataformat.yaml.JacksonYAMLParseException;
57
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
68
import io.swagger.v3.core.matchers.SerializationMatchers;
@@ -23,6 +25,7 @@
2325

2426
import static org.testng.Assert.assertEquals;
2527
import static org.testng.Assert.assertFalse;
28+
import static org.testng.Assert.assertTrue;
2629

2730
public class JsonSerializationTest {
2831

@@ -154,4 +157,33 @@ public void testSerializeYAMLWithCustomFactoryAndCodePointLimitReached() throws
154157

155158
// then - Throw JacksonYAMLParseException
156159
}
160+
161+
@Test
162+
public void testCustomPrettyPrinterIsHonored() {
163+
//given
164+
DefaultPrettyPrinter originalPrinter = (DefaultPrettyPrinter) Json.mapper().getSerializationConfig().getDefaultPrettyPrinter();
165+
166+
try {
167+
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
168+
printer.indentObjectsWith(new DefaultIndenter(" ", "\n"));
169+
Json.mapper().setDefaultPrettyPrinter(printer);
170+
171+
//when
172+
OpenAPI openAPI = new OpenAPI()
173+
.info(new Info().title("Pet Store"));
174+
175+
String json = Json.pretty(openAPI);
176+
177+
//then
178+
assertTrue(json.contains("{\n \"openapi\""),
179+
"Custom four-space indentation should be honored");
180+
assertTrue(json.contains(" \"info\" : {"),
181+
"Nested objects should use four-space indentation");
182+
assertTrue(json.contains(" \"title\" : \"Pet Store\""),
183+
"Doubly-nested properties should use eight-space indentation");
184+
} finally {
185+
// Restore original pretty printer to avoid affecting other tests
186+
Json.mapper().setDefaultPrettyPrinter(originalPrinter);
187+
}
188+
}
157189
}

modules/swagger-java17-support/src/test/java/io/swagger/v3/java17/resolving/SwaggerTestBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.swagger.v3.java17.resolving;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
54
import com.fasterxml.jackson.databind.DeserializationFeature;
65
import com.fasterxml.jackson.databind.ObjectMapper;
76
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -29,7 +28,7 @@ protected ModelResolver modelResolver() {
2928

3029
protected void prettyPrint(Object o) {
3130
try {
32-
LOGGER.debug(mapper().writer(new DefaultPrettyPrinter()).writeValueAsString(o));
31+
LOGGER.debug(mapper().writerWithDefaultPrettyPrinter().writeValueAsString(o));
3332
} catch (Exception e) {
3433
LOGGER.error("Failed to pretty print object", e);
3534
}

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

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

3-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
43
import io.swagger.v3.core.filter.OpenAPISpecFilter;
54
import io.swagger.v3.core.filter.SpecFilter;
65
import io.swagger.v3.jaxrs2.util.ServletUtils;
@@ -86,12 +85,12 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
8685
if (type.equalsIgnoreCase("yaml")) {
8786
resp.setContentType(APPLICATION_YAML);
8887
try (PrintWriter pw = resp.getWriter()) {
89-
pw.write(pretty ? ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputYamlMapper().writeValueAsString(oas));
88+
pw.write(pretty ? ctx.getOutputYamlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(oas) : ctx.getOutputYamlMapper().writeValueAsString(oas));
9089
}
9190
} else {
9291
resp.setContentType(APPLICATION_JSON);
9392
try (PrintWriter pw = resp.getWriter()) {
94-
pw.write(pretty ? ctx.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) : ctx.getOutputJsonMapper().writeValueAsString(oas));
93+
pw.write(pretty ? ctx.getOutputJsonMapper().writerWithDefaultPrettyPrinter().writeValueAsString(oas) : ctx.getOutputJsonMapper().writeValueAsString(oas));
9594
}
9695
}
9796

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

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

3-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
43
import io.swagger.v3.core.filter.OpenAPISpecFilter;
54
import io.swagger.v3.core.filter.SpecFilter;
65
import io.swagger.v3.core.util.Configuration;
@@ -401,14 +400,14 @@ public Map<String, String> resolve() throws Exception{
401400
String openapiYaml = null;
402401
if ("JSON".equals(outputFormat) || "JSONANDYAML".equals(outputFormat)) {
403402
if (prettyPrint != null && prettyPrint) {
404-
openapiJson = context.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(openAPI);
403+
openapiJson = context.getOutputJsonMapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);
405404
} else {
406405
openapiJson = context.getOutputJsonMapper().writeValueAsString(openAPI);
407406
}
408407
}
409408
if ("YAML".equals(outputFormat) || "JSONANDYAML".equals(outputFormat)) {
410409
if (prettyPrint != null && prettyPrint) {
411-
openapiYaml = context.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(openAPI);
410+
openapiYaml = context.getOutputYamlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);
412411
} else {
413412
openapiYaml = context.getOutputYamlMapper().writeValueAsString(openAPI);
414413
}

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

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

3-
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
43
import io.swagger.v3.core.filter.OpenAPISpecFilter;
54
import io.swagger.v3.core.filter.SpecFilter;
65
import io.swagger.v3.jaxrs2.integration.JaxrsOpenApiContextBuilder;
@@ -71,14 +70,14 @@ protected Response getOpenApi(HttpHeaders headers,
7170
if (StringUtils.isNotBlank(type) && type.trim().equalsIgnoreCase("yaml")) {
7271
return Response.status(Response.Status.OK)
7372
.entity(pretty ?
74-
ctx.getOutputYamlMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) :
73+
ctx.getOutputYamlMapper().writerWithDefaultPrettyPrinter().writeValueAsString(oas) :
7574
ctx.getOutputYamlMapper().writeValueAsString(oas))
7675
.type("application/yaml")
7776
.build();
7877
} else {
7978
return Response.status(Response.Status.OK)
8079
.entity(pretty ?
81-
ctx.getOutputJsonMapper().writer(new DefaultPrettyPrinter()).writeValueAsString(oas) :
80+
ctx.getOutputJsonMapper().writerWithDefaultPrettyPrinter().writeValueAsString(oas) :
8281
ctx.getOutputJsonMapper().writeValueAsString(oas))
8382
.type(MediaType.APPLICATION_JSON_TYPE)
8483
.build();

0 commit comments

Comments
 (0)