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 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; } }
public ModelConverters() { converters = new CopyOnWriteArrayList<>(); converters.add(new ModelResolver(Json.mapper())); }
public static ObjectWriter pretty() { return mapper().writer(new DefaultPrettyPrinter()); }
protected Object resolveExample(Annotated a, Annotation[] annotations, io.swagger.v3.oas.annotations.media.Schema schema) { if (schema != null) { if (!schema.example().isEmpty()) { try { return Json.mapper().readTree(schema.example()); } catch (IOException e) { return schema.example(); } } } return null; }
@Test(description = "it should read the uber api") public void readUberApi() throws IOException { final String jsonString = ResourceUtils.loadClassResource(getClass(), "uber.json"); final OpenAPI swagger = Json.mapper().readValue(jsonString, OpenAPI.class); assertNotNull(swagger); }
private OpenAPI getOpenAPI(String path) throws IOException { final String json = ResourceUtils.loadClassResource(getClass(), path); return Json.mapper().readValue(json, OpenAPI.class); } }
@Test public void testIssue2064Neg() throws Exception { String json = "{\n" + " \"type\": \"string\",\n" + " \"uniqueItems\": false\n" + "}"; final Schema model = Json.mapper().readValue(json, Schema.class); assertFalse(model.getUniqueItems()); }
@Test public void testIssue2064() throws Exception { String json = "{\n" + " \"type\": \"string\",\n" + " \"uniqueItems\": true\n" + "}"; final Schema model = Json.mapper().readValue(json, Schema.class); assertTrue(model.getUniqueItems()); }
public static <T> T deserializeJsonFileFromClasspath(String path, Class<T> type) { return deserializeFileFromClasspath(path, type, Json.mapper()); }
@Test public void testIssue1852() throws Exception { String json = "{\n" + " \"type\": \"integer\",\n" + " \"minimum\": 10,\n" + " \"maximum\": 20,\n" + " \"default\": 15\n" + "}"; final Schema model = Json.mapper().readValue(json, Schema.class); assertEquals(model.getMinimum().intValue(), 10); assertEquals(model.getMaximum().intValue(), 20); assertEquals(model.getDefault(), 15); }
@Test public void prettyPrintTest() throws IOException { final String json = ResourceUtils.loadClassResource(getClass(), "uber.json"); final OpenAPI swagger = Json.mapper().readValue(json, OpenAPI.class); final String outputStream = OutputReplacer.OUT.run(new OutputReplacer.Function() { @Override public void run() { Json.prettyPrint(swagger); } }); SerializationMatchers.assertEqualsToJson(swagger, outputStream); }
@Test public void testExtension() throws Exception { OpenAPI swagger = new OpenAPI(); swagger.addExtension("x-foo-bar", "foo bar"); swagger.setInfo(new Info()); swagger.getInfo().addExtension("x-foo-bar", "foo bar"); String swaggerJson = Json.mapper().writeValueAsString(swagger); assertFalse(swaggerJson.contains("extensions")); OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class); assertEquals(rebuilt.getExtensions().values().iterator().next(), "foo bar"); assertEquals(rebuilt.getInfo().getExtensions().values().iterator().next(), "foo bar"); }
@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(description = "vendor extensions should be included with object type") public void testMapDeserializationVendorExtensions() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); MapSchema mp = (MapSchema) responseSchema; assertTrue(mp.getExtensions().size() > 0); assertNotNull(mp.getExtensions().get("x-foo")); assertEquals(mp.getExtensions().get("x-foo"), "vendor x"); }
@Test(description = "it should deserialize a response per #1349") public void testMapDeserialization() throws Exception { Operation operation = Json.mapper().readValue(json, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); assertTrue(responseSchema instanceof MapSchema); MapSchema mp = (MapSchema) responseSchema; assertTrue(mp.getAdditionalProperties() instanceof IntegerSchema); }
@Test(description = "it should deserialize a boolean additionalProperties") public void testBooleanAdditionalPropertiesDeserialization() throws Exception { Operation operation = Json.mapper().readValue(jsonAdditionalPropertiesBoolean, Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema responseSchema = response.getContent().get("*/*").getSchema(); assertNotNull(responseSchema); assertTrue(responseSchema instanceof ObjectSchema); assertTrue(responseSchema.getAdditionalProperties() instanceof Boolean); }
@Test public void testSerializeASpecWithPathReferences() throws Exception { OpenAPI swagger = new OpenAPI() .addServersItem(new Server().url("http://petstore.swagger.io")); PathItem expectedPath = new PathItem().$ref("http://my.company.com/paths/health.json"); swagger.path("/health", expectedPath); String swaggerJson = Json.mapper().writeValueAsString(swagger); OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class); final PathItem path = rebuilt.getPaths().get("/health"); assertEquals(path, expectedPath); }
@Test(description = "it should clone everything from JSON without models") public void cloneWithoutModels() throws IOException { final String json = ResourceUtils.loadClassResource(getClass(), RESOURCE_PATH_WITHOUT_MODELS); final OpenAPI openAPI = Json.mapper().readValue(json, OpenAPI.class); final OpenAPI filtered = new SpecFilter().filter(openAPI, new NoOpOperationsFilter(), null, null, null); SerializationMatchers.assertEqualsToJson(filtered, json); }
@Test(description = "it should not create an xml object for $ref") public void shouldNotCreateXmlObjectForRef() throws IOException { final Schema model = new Schema().$ref("Monster"); model.setDescription("oops"); model.setExternalDocs(new ExternalDocumentation() .description("external docs") .url("http://swagger.io")); assertEquals(Json.mapper().writeValueAsString(model), "{\"$ref\":\"#/components/schemas/Monster\"}"); }