Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Json {

private static final class ObjectMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createJson();
}

private static final Logger LOGGER = LoggerFactory.getLogger(Json.class);

public static ObjectMapper mapper() {
return ObjectMapperHolder.MAPPER;
}
Expand All @@ -22,16 +26,17 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to JSON", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o).replace("\r", ""));
String prettyString = pretty().writeValueAsString(o).replace("\r", "");
PrettyPrintHelper.emit(LOGGER, prettyString);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing JSON", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static final class ConverterMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createJsonConverter();
}

static Logger LOGGER = LoggerFactory.getLogger(Json31.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Json31.class);

public static ObjectMapper mapper() {
return ObjectMapperHolder.MAPPER;
Expand All @@ -39,16 +39,17 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to JSON (3.1)", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o).replace("\r", ""));
String prettyString = pretty().writeValueAsString(o).replace("\r", "");
PrettyPrintHelper.emit(LOGGER, prettyString);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing JSON (3.1)", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.swagger.v3.core.util;

import org.slf4j.Logger;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Consumer;

class PrettyPrintHelper {

private static final ThreadLocal<Consumer<String>> OVERRIDE = new ThreadLocal<>();

private PrettyPrintHelper() {
// utility class
}

static void setOverride(Consumer<String> consumer) {
OVERRIDE.set(consumer);
}

static void clearOverride() {
OVERRIDE.remove();
}

static void emit(Logger logger, String message) {
Consumer<String> consumer = OVERRIDE.get();
if (consumer != null) {
consumer.accept(message);
} else {
logger.debug(message);
}
}

static void emitError(Logger logger, String message, Throwable throwable) {
Consumer<String> consumer = OVERRIDE.get();
if (consumer != null) {
StringBuilder builder = new StringBuilder(message);
if (throwable != null) {
builder.append(System.lineSeparator());
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
builder.append(writer);
}
consumer.accept(builder.toString());
}
logger.error(message, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Yaml {

private static final class ObjectMapperHolder {
private static final ObjectMapper MAPPER = ObjectMapperFactory.createYaml();
}

private static final Logger LOGGER = LoggerFactory.getLogger(Yaml.class);

public static ObjectMapper mapper() {
return ObjectMapperHolder.MAPPER;
}
Expand All @@ -22,16 +26,17 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to YAML", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o));
String prettyString = pretty().writeValueAsString(o);
PrettyPrintHelper.emit(LOGGER, prettyString);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing YAML", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private static final class ObjectMapperHolder {
}


static Logger LOGGER = LoggerFactory.getLogger(Yaml31.class);
private static final Logger LOGGER = LoggerFactory.getLogger(Yaml31.class);

public static ObjectMapper mapper() {
return ObjectMapperHolder.MAPPER;
Expand All @@ -31,16 +31,17 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error serializing object to YAML (3.1)", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o));
String prettyString = pretty().writeValueAsString(o);
PrettyPrintHelper.emit(LOGGER, prettyString);
} catch (Exception e) {
e.printStackTrace();
PrettyPrintHelper.emitError(LOGGER, "Error pretty-printing YAML (3.1)", e);
}
}

Expand All @@ -60,4 +61,5 @@ public static Map<String, Object> jsonSchemaAsMap(Schema schema) {
LOGGER.error("Exception converting jsonSchema to Map", e);
return null;
}
}}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;

import java.io.IOException;
Expand All @@ -44,6 +46,7 @@
import static org.testng.Assert.fail;

public class SpecFilterTest {
private static final Logger LOGGER = LoggerFactory.getLogger(SpecFilterTest.class);

private static final String RESOURCE_RECURSIVE_MODELS = "specFiles/recursivemodels.json";
private static final String RESOURCE_PATH = "specFiles/petstore-3.0-v2.json";
Expand Down Expand Up @@ -189,7 +192,7 @@ public void run() {
try {
filteredMap.put("filtered " + id, new SpecFilter().filter(openAPI, new NoOpOperationsFilter(), null, null, null));
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Failed to filter OpenAPI concurrently", e);
}
}
}.start();
Expand Down Expand Up @@ -220,7 +223,7 @@ public void run() {
}
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Interrupted while waiting for filtering threads to complete", e);
}
for (OpenAPI filtered : filteredMap.values()) {
assertEquals(Json.pretty(openAPI), Json.pretty(filtered));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.swagger.v3.core.jackson.ModelResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SwaggerTestBase {
static ObjectMapper mapper;
private static final Logger LOGGER = LoggerFactory.getLogger(SwaggerTestBase.class);

public static ObjectMapper mapper() {
if (mapper == null) {
Expand All @@ -26,9 +29,9 @@ protected ModelResolver modelResolver() {

protected void prettyPrint(Object o) {
try {
System.out.println(mapper().writer(new DefaultPrettyPrinter()).writeValueAsString(o));
LOGGER.debug(mapper().writer(new DefaultPrettyPrinter()).writeValueAsString(o));
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Failed to pretty print object", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.LoaderOptions;

Expand All @@ -37,6 +39,7 @@
import static org.testng.Assert.assertTrue;

public class OpenAPI3_1SerializationTest {
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAPI3_1SerializationTest.class);

@Test
public void testSerializePetstore() throws Exception {
Expand Down Expand Up @@ -1390,7 +1393,7 @@ public void testBooleanSchemaSerialization() {
.openapi("3.1.0")
.components(new Components().addSchemas("test", new Schema().booleanSchemaValue(true)));

System.out.println("--------- root ----------");
LOGGER.debug("--------- root ----------");
Json31.prettyPrint(openAPI);
assertEquals(Json31.pretty(openAPI), withJacksonSystemLineSeparator("{\n" +
" \"openapi\" : \"3.1.0\",\n" +
Expand All @@ -1400,19 +1403,19 @@ public void testBooleanSchemaSerialization() {
" }\n" +
" }\n" +
"}"));
System.out.println("--------- schema ----------");
LOGGER.debug("--------- schema ----------");
Json31.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
assertEquals(Json31.pretty(openAPI.getComponents().getSchemas().get("test")), "true");
System.out.println("--------- root YAML----------");
LOGGER.debug("--------- root YAML----------");
Yaml31.prettyPrint(openAPI);
assertEquals(Yaml31.pretty(openAPI), "openapi: 3.1.0\n" +
"components:\n" +
" schemas:\n" +
" test: true\n");
System.out.println("--------- schema YAML ----------");
LOGGER.debug("--------- schema YAML ----------");
Yaml31.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
assertEquals(Yaml31.pretty(openAPI.getComponents().getSchemas().get("test")), "true\n");
System.out.println("--------- root 3.0 ----------");
LOGGER.debug("--------- root 3.0 ----------");
Json.prettyPrint(openAPI);
assertEquals(Json.pretty(openAPI), withJacksonSystemLineSeparator("{\n" +
" \"openapi\" : \"3.1.0\",\n" +
Expand All @@ -1422,16 +1425,16 @@ public void testBooleanSchemaSerialization() {
" }\n" +
" }\n" +
"}"));
System.out.println("--------- schema 3.0 ----------");
LOGGER.debug("--------- schema 3.0 ----------");
Json.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
assertEquals(Json.pretty(openAPI.getComponents().getSchemas().get("test")), "{ }");
System.out.println("--------- root YAML 3.0 ----------");
LOGGER.debug("--------- root YAML 3.0 ----------");
Yaml.prettyPrint(openAPI);
assertEquals(Yaml.pretty(openAPI), "openapi: 3.1.0\n" +
"components:\n" +
" schemas:\n" +
" test: {}\n");
System.out.println("--------- schema YAML 3.0 ----------");
LOGGER.debug("--------- schema YAML 3.0 ----------");
Yaml.prettyPrint(openAPI.getComponents().getSchemas().get("test"));
assertEquals(Yaml.pretty(openAPI.getComponents().getSchemas().get("test")), "{}\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,18 @@ void setOutputStream(PrintStream outputStream) {
public String run(Function function) {
final PrintStream out = getOutputStream();
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

final PrintStream capture = new PrintStream(outputStream);
try {
setOutputStream(new PrintStream(outputStream));
setOutputStream(capture);
PrettyPrintHelper.setOverride(message -> {
capture.println(message);
capture.flush();
});
function.run();
} finally {
PrettyPrintHelper.clearOverride();
setOutputStream(out);
capture.close();
}

try {
Expand Down
Loading
Loading