Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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,16 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error serializing object to JSON", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o).replace("\r", ""));
LOGGER.debug(pretty().writeValueAsString(o).replace("\r", ""));
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("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,16 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("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", ""));
LOGGER.debug(pretty().writeValueAsString(o).replace("\r", ""));
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error pretty-printing JSON (3.1)", e);
}
}

Expand Down
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,16 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error serializing object to YAML", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o));
LOGGER.debug(pretty().writeValueAsString(o));
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("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,16 @@ public static String pretty(Object o) {
try {
return pretty().writeValueAsString(o);
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error serializing object to YAML (3.1)", e);
return null;
}
}

public static void prettyPrint(Object o) {
try {
System.out.println(pretty().writeValueAsString(o));
LOGGER.debug(pretty().writeValueAsString(o));
} catch (Exception e) {
e.printStackTrace();
LOGGER.error("Error pretty-printing YAML (3.1)", e);
}
}

Expand Down
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
@@ -1,10 +1,17 @@
package io.swagger.v3.oas.transformer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
Expand Down Expand Up @@ -134,7 +141,9 @@ public void transform(final Transformer transformer, final Artifact sourceArtifa
* @return A configured transformer
*/
public Transformer getTransformer() {
final Transformer transformer = new Transformer(System.out, System.err);
final Transformer transformer = new Transformer(
createLoggingPrintStream(getLog()::info),
createLoggingPrintStream(getLog()::error));
transformer.setOptionDefaults(JakartaTransformer.class, getOptionDefaults());
return transformer;
}
Expand Down Expand Up @@ -177,6 +186,50 @@ private Map<Transformer.AppOption, String> getOptionDefaults() {
return optionDefaults;
}

private PrintStream createLoggingPrintStream(Consumer<String> logConsumer) {
try {
return new PrintStream(new LoggingOutputStream(logConsumer), true, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("UTF-8 encoding is not supported", e);
}
}

private static final class LoggingOutputStream extends OutputStream {
private final Consumer<String> logConsumer;
private final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

private LoggingOutputStream(Consumer<String> logConsumer) {
this.logConsumer = logConsumer;
}

@Override
public void write(int b) {
if (b == '\n') {
flushBuffer();
} else if (b != '\r') {
buffer.write(b);
}
}

@Override
public void flush() {
flushBuffer();
}

@Override
public void close() throws IOException {
flushBuffer();
}

private void flushBuffer() {
if (buffer.size() == 0) {
return;
}
logConsumer.accept(new String(buffer.toByteArray(), StandardCharsets.UTF_8));
buffer.reset();
}
}

private boolean isEmpty(final String input) {
return input == null || input.trim()
.length() == 0;
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 @@ -41,7 +41,7 @@ public void init(ServletConfig config) throws ServletException {
.ctxId(ctxId)
.buildContext(true);
} catch (OpenApiConfigurationException e) {
e.printStackTrace();
LOGGER.error("Failed to initialize OpenAPI servlet context", e);
}
}

Expand Down
Loading
Loading