public static String pretty(Object o) { try { return pretty().writeValueAsString(o); } catch (Exception e) { e.printStackTrace(); return null; } }
public static void prettyPrint(Object o) { try { System.out.println(pretty().writeValueAsString(o).replace("\r", "")); } catch (Exception e) { e.printStackTrace(); } } }
public static OpenAPIConfiguration deepCopy(OpenAPIConfiguration config) { if (config == null) { return null; } try { return Json.mapper().readValue(Json.pretty(config), SwaggerConfiguration.class); } catch (Exception e) { LOGGER.error("Exception cloning config: " + e.getMessage(), e); return config; } }
@Override public void run() { try { Thread[] thds = new Thread[tg.activeCount()]; tg.enumerate(thds); for (Thread t : thds) { if (t != null) { t.join(10000); } } } catch (Exception e) { e.printStackTrace(); } for (OpenAPI filtered : filteredMap.values()) { assertEquals(Json.pretty(openAPI), Json.pretty(filtered)); } } }
@Override public void run() { Json.pretty(new ThrowHelper()); } });
resp.setContentType(APPLICATION_JSON); PrintWriter pw = resp.getWriter(); pw.write(pretty ? Json.pretty(oas) : Json.mapper().writeValueAsString(oas)); pw.close();
if (mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) { if (prettyPrint) { out.write(Json.pretty().writeValueAsBytes(data)); } else { out.write(Json.mapper().writeValueAsBytes(data)); headers.add("Content-Type", "application/json"); if (prettyPrint) { out.write(Json.pretty().writeValueAsBytes(data)); } else { out.write(Json.mapper().writeValueAsBytes(data));
Schema clonedModel = Json.mapper().readValue(Json.pretty(definition), Schema.class); if (clonedModel.getProperties() != null) { clonedModel.getProperties().clear();
private void handleUnwrapped(List<Schema> props, Schema innerModel, String prefix, String suffix) { if (StringUtils.isBlank(suffix) && StringUtils.isBlank(prefix)) { if (innerModel.getProperties() != null) { props.addAll(innerModel.getProperties().values()); } } else { if (prefix == null) { prefix = ""; } if (suffix == null) { suffix = ""; } if (innerModel.getProperties() != null) { for (Schema prop : (Collection<Schema>) innerModel.getProperties().values()) { try { Schema clonedProp = Json.mapper().readValue(Json.pretty(prop), Schema.class); clonedProp.setName(prefix + prop.getName() + suffix); props.add(clonedProp); } catch (IOException e) { LOGGER.error("Exception cloning property", e); return; } } } } }
@Test(description = "it should clone everything") public void cloneEverything() throws IOException { final OpenAPI openAPI = getOpenAPI(RESOURCE_PATH); final OpenAPI filtered = new SpecFilter().filter(openAPI, new NoOpOperationsFilter(), null, null, null); assertEquals(Json.pretty(filtered), Json.pretty(openAPI)); }
@Test(description = "it should scan a model with numbers") public void scanModelWithNumbers() throws IOException { final Map<String, Schema> models = readAll(ModelWithNumbers.class); assertEquals(models.size(), 1); final Schema model = models.get("ModelWithNumbers"); // Check if we get required properties after building models from classes. checkModel(model); // Check if we get required properties after deserialization from JSON checkModel(Json.mapper().readValue(Json.pretty(model), Schema.class)); }
@Test public void testByteProperty() { Schema model = new Schema() .addProperties("byteProperty", new ByteArraySchema()); assertEquals(Json.pretty(model), "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteProperty\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"byte\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + "}"); }
@Test public void testFormatOfDecimal() { final Map<String, Schema> models = ModelConverters.getInstance().readAll(ModelWithDecimalFields.class); assertEquals(models.size(), 1); String json = Json.pretty(models); assertEquals(normalizeLineEnds(json), "{\n" + " \"ModelWithDecimalFields\" : {\n" + " \"type\" : \"object\",\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"minimum\" : 3.3,\n" + " \"exclusiveMinimum\" : false,\n" + " \"type\" : \"number\",\n" + " \"format\" : \"double\"\n" + " }\n" + " }\n" + " }\n" + "}"); }
@Test public void testFormatOfInteger() { final Map<String, Schema> models = ModelConverters.getInstance().readAll(ModelWithIntegerFields.class); assertEquals(models.size(), 1); String json = Json.pretty(models); assertEquals(normalizeLineEnds(json), "{\n" + " \"ModelWithIntegerFields\" : {\n" + " \"type\" : \"object\",\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"minimum\" : 3,\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " }\n" + " }\n" + " }\n" + "}"); }
@Test public void testFormatOfBigDecimal() { final Map<String, Schema> models = ModelConverters.getInstance().readAll(ModelWithoutScientificFields.class); assertEquals(models.size(), 1); String json = Json.pretty(models); assertEquals(normalizeLineEnds(json), "{\n" + " \"ModelWithoutScientificFields\" : {\n" + " \"type\" : \"object\",\n" + " \"properties\" : {\n" + " \"id\" : {\n" + " \"maximum\" : 9999999999999999.99,\n" + " \"exclusiveMaximum\" : false,\n" + " \"minimum\" : -9999999999999999.99,\n" + " \"exclusiveMinimum\" : false,\n" + " \"type\" : \"number\"\n" + " }\n" + " }\n" + " }\n" + "}"); }
} else { return Response.status(Response.Status.OK) .entity(pretty ? Json.pretty(oas) : Json.mapper().writeValueAsString(oas)) .type(MediaType.APPLICATION_JSON_TYPE) .build();
@Test public void testByteArray() { Schema model = new Schema() .addProperties("byteArray", new ArraySchema().items(new BinarySchema())); assertEquals(Json.pretty(model), "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteArray\" : {" + NEWLINE + " \"type\" : \"array\"," + NEWLINE + " \"items\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"binary\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + "}"); }
@Test public void testReadOnlyByteArray() { Schema model = new Schema() .addProperties("byteArray", new ArraySchema().items(new BinarySchema()).readOnly(true)); assertEquals(Json.pretty(model), "{" + NEWLINE + " \"properties\" : {" + NEWLINE + " \"byteArray\" : {" + NEWLINE + " \"type\" : \"array\"," + NEWLINE + " \"readOnly\" : true," + NEWLINE + " \"items\" : {" + NEWLINE + " \"type\" : \"string\"," + NEWLINE + " \"format\" : \"binary\"" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + " }" + NEWLINE + "}"); }
@Test(description = "it should serialize a boolean additionalProperties") public void testBooleanAdditionalPropertiesSerialization() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); Schema schema = new ObjectSchema().additionalProperties(true); assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : true\n" + "}"); schema = new ObjectSchema().additionalProperties(responseSchema); assertEquals(normalizeLineEnds(Json.pretty(schema)), "{\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"object\",\n" + " \"additionalProperties\" : {\n" + " \"type\" : \"integer\",\n" + " \"format\" : \"int32\"\n" + " },\n" + " \"x-foo\" : \"vendor x\"\n" + " }\n" + "}"); }