use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method componentsResolver.
@Test
public void componentsResolver() throws Exception {
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
final JsonNode rootNode = mapper.readTree(pathFile.getBytes());
final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
final SwaggerParseResult result = deserializer.deserialize(rootNode);
Assert.assertNotNull(result);
final OpenAPI openAPI = result.getOpenAPI();
Assert.assertNotNull(openAPI);
assertEquals(new OpenAPIResolver(openAPI, new ArrayList<>(), null).resolve(), openAPI);
Map<String, Schema> schemas = openAPI.getComponents().getSchemas();
// internal url schema
Schema pet = schemas.get("Pet");
Schema category = (Schema) pet.getProperties().get("category");
assertEquals(category.get$ref(), "#/components/schemas/Category");
// remote url schema
Schema user = (Schema) pet.getProperties().get("user");
assertEquals(user.get$ref(), "#/components/schemas/User");
// ArraySchema items
ArraySchema tagsProperty = (ArraySchema) pet.getProperties().get("tags");
assertEquals(tagsProperty.getItems().get$ref(), "#/components/schemas/ExampleSchema");
assertEquals(tagsProperty.getType(), "array");
Assert.assertNotNull(openAPI.getComponents().getSchemas().get("ExampleSchema"));
// Schema not
assertEquals(schemas.get("OrderRef").getNot().get$ref(), "#/components/schemas/Category");
// Schema additionalProperties
assertTrue(schemas.get("OrderRef").getAdditionalProperties() instanceof Schema);
Schema additionalProperties = (Schema) schemas.get("OrderRef").getAdditionalProperties();
assertEquals(additionalProperties.get$ref(), "#/components/schemas/User");
// AllOfSchema
ComposedSchema extended = (ComposedSchema) schemas.get("ExtendedErrorModel");
Schema root = (Schema) extended.getAllOf().get(0).getProperties().get("rootCause");
assertEquals(root.get$ref(), "#/components/schemas/Category");
Map<String, ApiResponse> responses = openAPI.getComponents().getResponses();
// internal response headers
ApiResponse illegalInput = responses.get("IllegalInput");
assertEquals(illegalInput.getHeaders().get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
// internal response links
assertEquals(illegalInput.getLinks().get("address").get$ref(), "#/components/links/unsubscribe");
// internal url response schema
MediaType generalError = responses.get("GeneralError").getContent().get("application/json");
assertEquals(generalError.getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
Map<String, RequestBody> requestBodies = openAPI.getComponents().getRequestBodies();
// internal url requestBody schema
RequestBody requestBody1 = requestBodies.get("requestBody1");
MediaType xmlMedia = requestBody1.getContent().get("application/json");
assertEquals(xmlMedia.getSchema().get$ref(), "#/components/schemas/Pet");
// internal url requestBody ArraySchema
RequestBody requestBody2 = requestBodies.get("requestBody2");
MediaType jsonMedia = requestBody2.getContent().get("application/json");
ArraySchema items = (ArraySchema) jsonMedia.getSchema();
assertEquals(items.getItems().get$ref(), "#/components/schemas/User");
// internal request body
assertEquals("#/components/requestBodies/requestBody2", requestBodies.get("requestBody3").get$ref());
// remote request body url
assertEquals(requestBodies.get("reference").get$ref(), "#/components/requestBodies/remote_requestBody");
Map<String, Parameter> parameters = openAPI.getComponents().getParameters();
// remote url parameter
assertEquals(parameters.get("remoteParameter").get$ref(), "#/components/parameters/parameter");
// internal Schema Parameter
assertEquals(parameters.get("newParam").getSchema().get$ref(), "#/components/schemas/Tag");
// parameter examples
assertEquals(parameters.get("contentParameter").getExamples().get("cat"), openAPI.getComponents().getExamples().get("cat"));
// parameter content schema
assertEquals(parameters.get("contentParameter").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
// internal Schema header
Map<String, Header> headers = openAPI.getComponents().getHeaders();
// header remote schema ref
assertEquals(headers.get("X-Rate-Limit-Remaining").getSchema().get$ref(), "#/components/schemas/User");
// header examples
assertEquals(headers.get("X-Rate-Limit-Reset").getExamples().get("headerExample").get$ref(), "#/components/examples/dog");
// remote header ref
assertEquals(headers.get("X-Ref-Limit-Limit").get$ref(), "#/components/headers/X-Rate-Limit-Reset");
// header content
assertEquals(headers.get("X-Rate-Limit-Reset").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ExtendedErrorModel");
Map<String, Example> examples = openAPI.getComponents().getExamples();
// internal url example
Example frogExample = examples.get("frog");
assertEquals(frogExample.get$ref(), "#/components/examples/cat");
// remote example url
assertEquals(examples.get("referenceCat").get$ref(), "#/components/examples/example");
// internal url securityScheme
SecurityScheme scheme = openAPI.getComponents().getSecuritySchemes().get("reference");
assertEquals(scheme.getType(), SecurityScheme.Type.APIKEY);
SecurityScheme remoteScheme = openAPI.getComponents().getSecuritySchemes().get("remote_reference");
assertEquals(remoteScheme.getType(), SecurityScheme.Type.OAUTH2);
Map<String, Link> links = openAPI.getComponents().getLinks();
// internal link
assertEquals(openAPI.getComponents().getLinks().get("referenced").get$ref(), "#/components/links/unsubscribe");
// remote ref link
assertEquals(openAPI.getComponents().getLinks().get("subscribe").get$ref(), "#/components/links/link");
Map<String, Callback> callbacks = openAPI.getComponents().getCallbacks();
// internal callback reference
assertEquals(callbacks.get("referenced").get$ref(), "#/components/callbacks/failed");
// callback pathItem -> operation ->requestBody
assertEquals(callbacks.get("heartbeat").get("$request.query.heartbeat-url").getPost().getRequestBody().get$ref(), "#/components/requestBodies/requestBody3");
// remote callback ref
assertEquals(callbacks.get("remoteCallback").get$ref(), "#/components/callbacks/callback");
}
use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIResolverTest method testSharedSwaggerParametersTest.
@Test(description = "resolve top-level parameters")
public void testSharedSwaggerParametersTest() {
final OpenAPI swagger = new OpenAPI();
List<Parameter> parameters = new ArrayList<>();
parameters.add(0, new Parameter().$ref("username"));
swagger.path("/fun", new PathItem().get(new Operation().parameters(parameters).responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("ok!")))));
swagger.components(new Components().addParameters("username", new QueryParameter().name("username").schema(new StringSchema())));
final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
assertTrue(resolved.getComponents().getParameters().size() == 1);
assertTrue(resolved.getPaths().get("/fun").getGet().getParameters().size() == 1);
}
use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method testIssue948.
@Test
public void testIssue948() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("Issue_948.json", null, options);
new OpenAPIResolver(result.getOpenAPI()).resolve();
assertNotNull(result.getOpenAPI());
}
use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDeserializeEnum.
@Test
public void testDeserializeEnum() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: 0.0.0\n" + " title: your title\n" + "paths:\n" + " /persons:\n" + " get:\n" + " description: a test\n" + " responses:\n" + " '200':\n" + " description: Successful response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " type: object\n" + " properties:\n" + " se:\n" + " $ref: '#/components/schemas/StringEnum'\n" + " ie:\n" + " $ref: '#/components/schemas/IntegerEnum'\n" + " ne:\n" + " $ref: '#/components/schemas/NumberEnum'\n" + " be:\n" + " $ref: '#/components/schemas/BooleanEnum'\n" + " ae:\n" + " $ref: '#/components/schemas/ArrayEnum'\n" + " oe:\n" + " $ref: '#/components/schemas/ObjectEnum'\n" + "components:\n" + " schemas:\n" + " StringEnum:\n" + " type: string\n" + " default: foo\n" + " enum:\n" + " - First\n" + " - Second\n" + " BooleanEnum:\n" + " enum:\n" + " - true \n" + " - false \n" + " IntegerEnum:\n" + " type: integer\n" + " default: 1\n" + " enum:\n" + " - -1\n" + " - 0\n" + " - 1\n" + " NumberEnum:\n" + " type: number\n" + " default: 3.14\n" + " enum:\n" + " - -1.151\n" + " - 0\n" + " - 1.6161\n" + " - 3.14\n" + " ArrayEnum:\n" + " type: array\n" + " items:\n" + " type: string\n" + " enum:\n" + " - - Camry\n" + " - Prius\n" + " - null\n" + " - - Pilot\n" + " - Passport\n" + " - - Rogue\n" + " - Leaf\n" + " ObjectEnum:\n" + " type: object\n" + " enum:\n" + " - make: Toyota\n" + " model: Prius\n" + " - make: Honda\n" + " model: Pilot\n" + " - make: Nissan\n" + " model: Leaf\n" + " - null\n";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
Schema stringModel = resolved.getComponents().getSchemas().get("StringEnum");
assertTrue(stringModel instanceof Schema);
Schema stringImpl = stringModel;
List<String> stringValues = stringImpl.getEnum();
assertEquals(2, stringValues.size());
assertEquals("First", stringValues.get(0));
assertEquals("Second", stringValues.get(1));
Schema integerModel = resolved.getComponents().getSchemas().get("IntegerEnum");
assertTrue(integerModel instanceof Schema);
Schema integerImpl = integerModel;
List<String> integerValues = integerImpl.getEnum();
assertEquals(3, integerValues.size());
assertEquals(-1, integerValues.get(0));
assertEquals(0, integerValues.get(1));
assertEquals(1, integerValues.get(2));
assertEquals(integerImpl.getDefault(), 1);
Schema numberModel = resolved.getComponents().getSchemas().get("NumberEnum");
assertTrue(numberModel instanceof Schema);
Schema numberImpl = numberModel;
List<String> numberValues = numberImpl.getEnum();
assertEquals(4, numberValues.size());
assertEquals(new BigDecimal("-1.151"), numberValues.get(0));
assertEquals(new BigDecimal("0"), numberValues.get(1));
assertEquals(new BigDecimal("1.6161"), numberValues.get(2));
assertEquals(new BigDecimal("3.14"), numberValues.get(3));
assertEquals(numberImpl.getDefault(), new BigDecimal("3.14"));
Schema booleanModel = resolved.getComponents().getSchemas().get("BooleanEnum");
assertEquals("boolean", booleanModel.getType());
List<Object> booleanValues = booleanModel.getEnum();
assertEquals(2, booleanValues.size());
assertEquals(Boolean.TRUE, booleanValues.get(0));
assertEquals(Boolean.FALSE, booleanValues.get(1));
Schema arrayModel = resolved.getComponents().getSchemas().get("ArrayEnum");
assertEquals("array", arrayModel.getType());
List<Object> arrayValues = arrayModel.getEnum();
assertEquals(arrayValues.size(), 4);
assertEquals(arrayValues.get(0), JsonNodeFactory.instance.arrayNode().add("Camry").add("Prius"));
assertEquals(arrayValues.get(1), null);
assertEquals(arrayValues.get(2), JsonNodeFactory.instance.arrayNode().add("Pilot").add("Passport"));
assertEquals(arrayValues.get(3), JsonNodeFactory.instance.arrayNode().add("Rogue").add("Leaf"));
Schema objectModel = resolved.getComponents().getSchemas().get("ObjectEnum");
assertEquals("object", objectModel.getType());
List<Object> objectValues = objectModel.getEnum();
assertEquals(objectValues.size(), 4);
assertEquals(objectValues.get(0), JsonNodeFactory.instance.objectNode().put("make", "Toyota").put("model", "Prius"));
assertEquals(objectValues.get(1), JsonNodeFactory.instance.objectNode().put("make", "Honda").put("model", "Pilot"));
assertEquals(objectValues.get(2), JsonNodeFactory.instance.objectNode().put("make", "Nissan").put("model", "Leaf"));
assertEquals(objectValues.get(3), null);
}
use of io.swagger.v3.parser.OpenAPIResolver in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testDeserializeDateString.
@Test
public void testDeserializeDateString() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: 0.0.0\n" + " title: My Title\n" + "paths:\n" + " /persons:\n" + " get:\n" + " description: a test\n" + " responses:\n" + " '200':\n" + " description: Successful response\n" + " content:\n" + " '*/*':\n" + " schema:\n" + " type: object\n" + " properties:\n" + " date:\n" + " $ref: '#/components/schemas/DateString'\n" + "components:\n" + " schemas:\n" + " DateString:\n" + " type: string\n" + " format: date\n" + " default: 2019-01-01\n" + " enum:\n" + " - 2019-01-01\n" + " - Nope\n" + " - 2018-02-02\n" + " - 2017-03-03\n" + " - null\n" + "";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
final OpenAPI resolved = new OpenAPIResolver(result.getOpenAPI(), null).resolve();
Schema dateModel = resolved.getComponents().getSchemas().get("DateString");
assertTrue(dateModel instanceof DateSchema);
List<Date> dateValues = dateModel.getEnum();
assertEquals(dateValues.size(), 4);
assertEquals(dateValues.get(0), new Calendar.Builder().setDate(2019, 0, 1).build().getTime());
assertEquals(dateValues.get(1), new Calendar.Builder().setDate(2018, 1, 2).build().getTime());
assertEquals(dateValues.get(2), new Calendar.Builder().setDate(2017, 2, 3).build().getTime());
assertEquals(dateValues.get(3), null);
assertEquals(dateModel.getDefault(), new Calendar.Builder().setDate(2019, 0, 1).build().getTime());
assertEquals(result.getMessages(), Arrays.asList("attribute components.schemas.DateString.enum=`Nope` is not of type `date`"));
}
Aggregations