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 ObjectWriter pretty() { return mapper().writer(new DefaultPrettyPrinter()); }
@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 public void testQuotedStrings() throws Exception { final String yaml = "singleLineUnquotedString: Look ma no quotes\n"; JsonNode node = Yaml.mapper().readValue(yaml, JsonNode.class); String serializedYaml = Yaml.pretty(node); assertEquals(serializedYaml, yaml); }
@Test public void testPreserveQuotesOnNumbers() throws Exception { final String yaml = "quotedNumber: \"3.0\"\n"; JsonNode node = Yaml.mapper().readValue(yaml, JsonNode.class); String serializedYaml = Yaml.pretty(node); assertEquals(serializedYaml, yaml); }
@Test public void testPreserveNoQuotesOnNumbers() throws Exception { final String yaml = "unquotedNumber: 4.0\n"; JsonNode node = Yaml.mapper().readValue(yaml, JsonNode.class); String serializedYaml = Yaml.pretty(node); assertEquals(serializedYaml, yaml); }
@Test public void testPreserveNoQuotesOnNonNumbers() throws Exception { final String yaml = "unquotedNumber: 4.0.0\n"; JsonNode node = Yaml.mapper().readValue(yaml, JsonNode.class); String serializedYaml = Yaml.pretty(node); assertEquals(serializedYaml, yaml); } }
public static <T> T deserializeYamlFileFromClasspath(String path, Class<T> type) { return deserializeFileFromClasspath(path, type, Yaml.mapper()); }
resp.setContentType(APPLICATION_YAML); PrintWriter pw = resp.getWriter(); pw.write(pretty ? Yaml.pretty(oas) : Yaml.mapper().writeValueAsString(oas)); pw.close(); } else {
public static void assertEqualsToYaml(Object objectToSerialize, String yamlStr) { apply(objectToSerialize, yamlStr, Yaml.mapper()); }
out.write(Yaml.pretty().writeValueAsBytes(data)); } else { out.write(Yaml.mapper().writeValueAsBytes(data));
@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); }
@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); } }
@Test(description = "it should desserialize Long schema correctly") public void deserializeLongSchema() throws IOException { final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/oas3_2.yaml"); final OpenAPI swagger = Yaml.mapper().readValue(jsonString, OpenAPI.class); assertNotNull(swagger); Schema s = swagger.getPaths().get("/withIntegerEnum/{stage}").getGet().getParameters().get(0).getSchema(); assertEquals(s.getEnum().get(0), 2147483647); assertEquals(s.getEnum().get(1), 3147483647L); assertEquals(s.getEnum().get(2), 31474836475505055L); }
@Test(description = "it should includes the example in the arrayproperty") public void testArrayDeserialization() throws Exception { Operation operation = Yaml.mapper().readValue(yaml, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); MediaType media = response.getContent().get("*/*"); Schema responseSchema = media.getSchema(); assertTrue(media.getExamples().size() == 2); assertNotNull(responseSchema); assertTrue(responseSchema instanceof ArraySchema); ArraySchema mp = (ArraySchema) responseSchema; assertEquals(mp.getMinItems(), new Integer(3)); assertEquals(mp.getMaxItems(), new Integer(100)); } }
"x-openapi: test"; OpenAPI aa = Yaml.mapper().readValue(yaml, OpenAPI.class); SerializationMatchers.assertEqualsToYaml(aa, yaml);
.entity(pretty ? Yaml.pretty(oas) : Yaml.mapper().writeValueAsString(oas)) .type("application/yaml") .build();
"xml:\n" + " name: \"rootName\""; final Schema model = Yaml.mapper().readValue(yaml, Schema.class);
@Test(description = "it should read an example within an inlined schema") public void testIssue1261InlineSchemaExample() throws Exception { Operation operation = Yaml.mapper().readValue( " responses:\n" + " 200:\n" + " content:\n" + " '*/*':\n" + " description: OK\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int32\n" + " name:\n" + " type: string\n" + " required: [id, name]\n" + " example: ok", Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema schema = response.getContent().get("*/*").getSchema(); Object example = schema.getExample(); assertNotNull(example); assertTrue(example instanceof String); assertEquals(example, "ok"); } }
@Test(description = "Deserialize ref callback") public void testDeserializeRefCallback() throws Exception { String yaml = "openapi: 3.0.1\n" + "info:\n" + " description: info\n" + "paths:\n" + " /simplecallback:\n" + " get:\n" + " summary: Simple get operation\n" + " operationId: getWithNoParameters\n" + " responses:\n" + " 200:\n" + " description: voila!\n" + " callbacks:\n" + " testCallback1:\n" + " $ref: '#/components/callbacks/Callback'\n" + " callbacks:\n" + " testCallback1:\n" + " $ref: '#/components/callbacks/Callback'\n" + "components:\n" + " callbacks:\n" + " Callback:\n" + " /post:\n" + " description: Post Path Item\n"; OpenAPI oas = Yaml.mapper().readValue(yaml, OpenAPI.class); assertEquals(oas.getPaths().get("/simplecallback").getGet().getCallbacks().get("testCallback1").get$ref(), "#/components/callbacks/Callback"); } }