@Test public void testMultiLineYaml() throws Exception { final String yaml = "multiLineString: |\n" + " \"hello multiple lines\n" + " in my content without\n" + " any problem\"\n"; JsonNode node = Yaml.mapper().readValue(yaml, JsonNode.class); String serializedYaml = Yaml.pretty(node); assertEquals(serializedYaml, yaml); }
@Test(description = "Shows how to restrict a particular schema") public void testExcludeSchema() { String yaml = "type: object\n" + "description: We don't store social security numbers here!\n" + "not:\n" + " $ref: \"#/components/schemas/AuthorizedUser\""; String yamlUser = "type: object\n" + "properties:\n" + " id:\n" + " type: string\n" + " description: 'A valid user social security'\n" + " pattern: '^\\d{3}-?\\d{2}-?\\d{4}$'"; Map<String, io.swagger.v3.oas.models.media.Schema> map = readAll(ArbitraryDataReceiver.class); Yaml.prettyPrint(map); SerializationMatchers.assertEqualsToYaml(map.get("ArbitraryDataReceiver"), yaml); SerializationMatchers.assertEqualsToYaml(map.get("AuthorizedUser"), yamlUser); }
default OpenAPIConfiguration deserializeConfig(String path, String configAsString) { try { if (path.toLowerCase().endsWith("json")) { return Json.mapper().readValue(configAsString, SwaggerConfiguration.class); } else { // assume yaml return Yaml.mapper().readValue(configAsString, SwaggerConfiguration.class); } } catch (Exception e) { LOGGER.error("exception reading config: " + e.getMessage(), e); return null; } }
public static String pretty(Object o) { try { return pretty().writeValueAsString(o); } catch (Exception e) { e.printStackTrace(); return null; } }
public static ObjectWriter pretty() { return mapper().writer(new DefaultPrettyPrinter()); }
public static void prettyPrint(Object o) { try { System.out.println(pretty().writeValueAsString(o)); } catch (Exception e) { e.printStackTrace(); } } }
public static <T> T deserializeYamlFileFromClasspath(String path, Class<T> type) { return deserializeFileFromClasspath(path, type, Yaml.mapper()); }
private static void apply(Object objectToSerialize, String str, ObjectMapper mapper) { final ObjectNode lhs = mapper.convertValue(objectToSerialize, ObjectNode.class); ObjectNode rhs = null; try { rhs = mapper.readValue(str, ObjectNode.class); } catch (IOException e) { LOGGER.error("Failed to read value", e); } if (!lhs.equals(new ObjectNodeComparator(), rhs)) { assertEquals(Yaml.pretty(lhs), Yaml.pretty(rhs)); //fail(String.format("Serialized object:\n%s\ndoes not equal to expected serialized string:\n%s", lhs, rhs)); } }
Yaml.prettyPrint(values); for (Map.Entry<String, Schema> entry : values.entrySet()) { String name = entry.getKey();
public static void assertEqualsToYaml(Object objectToSerialize, String yamlStr) { apply(objectToSerialize, yamlStr, Yaml.mapper()); }
public static String pretty(Object o) { try { return pretty().writeValueAsString(o); } catch (Exception e) { e.printStackTrace(); return null; } }
@Test(description = "should serialize correctly typed numeric enums") public void testIssue1765() throws Exception { String yaml = "openapi: '3.0.1'\n" + "paths:\n" + " /test:\n" + " get:\n" + " parameters:\n" + " - name: \"days\"\n" + " in: \"path\"\n" + " required: true\n" + " schema:\n" + " type: \"integer\"\n" + " format: \"int32\"\n" + " enum:\n" + " - 1\n" + " - 2\n" + " - 3\n" + " - 4\n" + " - 5\n" + " responses:\n" + " default:\n" + " description: great"; OpenAPI swagger = Yaml.mapper().readValue(yaml, OpenAPI.class); SerializationMatchers.assertEqualsToYaml(swagger, yaml); }
public static void prettyPrint(Object o) { try { System.out.println(pretty().writeValueAsString(o)); } catch (Exception e) { e.printStackTrace(); } } }
@Test(description = "should serialize correctly typed numeric enums") public void testIssue1765() throws Exception { String yaml = "openapi: '3.0.1'\n" + "paths:\n" + " /test:\n" + " get:\n" + " parameters:\n" + " - name: \"days\"\n" + " in: \"path\"\n" + " required: true\n" + " schema:\n" + " type: \"integer\"\n" + " format: \"int32\"\n" + " enum:\n" + " - 1\n" + " - 2\n" + " - 3\n" + " - 4\n" + " - 5\n" + " responses:\n" + " default:\n" + " description: great"; OpenAPI swagger = Yaml.mapper().readValue(yaml, OpenAPI.class); SerializationMatchers.assertEqualsToYaml(swagger, yaml); } }
@Override public void preprocessOpenAPI(OpenAPI openAPI) { super.preprocessOpenAPI(openAPI); try { String valueAsString = Yaml.pretty(openAPI); String outputFile = outputFolder + File.separator + this.outputFile; FileUtils.writeStringToFile(new File(outputFile), valueAsString); LOGGER.debug("wrote file to " + outputFile); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } }