use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testPathsWithRefResponse.
@Test
public void testPathsWithRefResponse() {
String json = "{\n" + " \"openapi\": \"3.0.0\",\n" + " \"paths\": {\n" + " \"/pet\": {\n" + " \"get\": {\n" + " \"responses\": {\n" + " \"200\": {\n" + " \"$ref\": \"#/components/responses/OK\"" + " }\n" + " }\n" + " }\n" + " }\n" + " }\n" + "}";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(json, null, null);
OpenAPI openAPI = result.getOpenAPI();
PathItem path = openAPI.getPaths().get("/pet");
assertNotNull(path);
Operation operation = path.getGet();
assertNotNull(operation);
assertTrue(operation.getResponses().containsKey("200"));
assertEquals(ApiResponse.class, operation.getResponses().get("200").getClass());
ApiResponse refResponse = operation.getResponses().get("200");
assertEquals("#/components/responses/OK", refResponse.get$ref());
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testIssue151.
@Test
public void testIssue151() throws Exception {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: 2.0.0\n" + " title: Test Issue 151\n" + " description: Tests that ComposedSchema vendor extensions are deserialized correctly.\n" + "paths:\n" + " /:\n" + " get:\n" + " responses:\n" + " '200':\n" + " description: OK\n" + "components:\n" + " schemas:\n" + " Pet:\n" + " type: object\n" + " required:\n" + " - id\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int64\n" + " Dog:\n" + " type: object\n" + " allOf:\n" + " - $ref: '#/components/schemas/Pet'\n" + " - required:\n" + " - name\n" + " properties:\n" + " name:\n" + " type: string\n" + " x-vendor-ext: some data";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
assertTrue(result.getMessages().isEmpty());
OpenAPI openAPI = result.getOpenAPI();
Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
assertTrue(definitions.size() == 2);
Schema allOfModel = definitions.get("Dog");
assertTrue(allOfModel instanceof ComposedSchema);
assertFalse(allOfModel.getExtensions().isEmpty());
assertEquals("some data", allOfModel.getExtensions().get("x-vendor-ext"));
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testSecurityDefinition.
@Test
public void testSecurityDefinition() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "paths:\n" + " /pet:\n" + " get:\n" + " security:\n" + " - basic_auth: []\n" + " api_key: []\n" + " responses:\n" + " default:\n" + " description: Default response\n" + "info:\n" + " version: ''\n" + " title: ''\n" + "components:\n" + " securitySchemes:\n" + " basic_auth:\n" + " type: http\n" + " x-foo: basicBar\n" + " scheme: basic\n" + " api_key:\n" + " type: apiKey\n" + " name: api_key\n" + " in: header\n" + " description: api key description\n" + " x-foo: apiKeyBar";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
OpenAPI openAPI = result.getOpenAPI();
assertNotNull(openAPI.getComponents().getSecuritySchemes());
assertTrue(openAPI.getComponents().getSecuritySchemes().keySet().size() == 2);
// Basic Authentication
SecurityScheme definitionBasic = openAPI.getComponents().getSecuritySchemes().get("basic_auth");
assertNotNull(definitionBasic);
assertEquals(definitionBasic.getType(), SecurityScheme.Type.HTTP);
assertEquals(definitionBasic.getExtensions().get("x-foo"), "basicBar");
// API Key Authentication
SecurityScheme definition = openAPI.getComponents().getSecuritySchemes().get("api_key");
assertNotNull(definition);
assertEquals(definition.getType(), SecurityScheme.Type.APIKEY);
SecurityScheme apiKey = definition;
assertEquals(apiKey.getName(), "api_key");
assertEquals(apiKey.getIn(), SecurityScheme.In.HEADER);
assertEquals(apiKey.getDescription(), "api key description");
assertEquals(apiKey.getExtensions().get("x-foo"), "apiKeyBar");
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testAlmostEmpty.
@Test
public void testAlmostEmpty(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: '3.0.1'\n" + "new: extra";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<>(messageList);
assertTrue(messages.contains("attribute info is missing"));
assertTrue(messages.contains("attribute paths is missing"));
assertTrue(messages.contains("attribute new is unexpected"));
}
use of io.swagger.v3.parser.OpenAPIV3Parser in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testContact.
@Test
public void testContact() {
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " title: title\n" + " bad: bad\n" + " x-foo: bar\n" + " description: description\n" + " termsOfService: tos\n" + " contact:\n" + " name: tony\n" + " url: url\n" + " email: email\n" + " invalid: invalid\n" + " x-fun: true\n" + " version: version\n" + "paths: {}";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
SwaggerParseResult result = parser.readContents(yaml, null, null);
List<String> messageList = result.getMessages();
Set<String> messages = new HashSet<>(messageList);
assertEquals(result.getOpenAPI().getInfo().getTitle(), "title");
assertEquals(result.getOpenAPI().getInfo().getDescription(), "description");
assertEquals(result.getOpenAPI().getInfo().getTermsOfService(), "tos");
assertEquals(result.getOpenAPI().getInfo().getVersion(), "version");
Contact contact = result.getOpenAPI().getInfo().getContact();
assertEquals(contact.getName(), "tony");
assertEquals(contact.getUrl(), "url");
assertEquals(contact.getEmail(), "email");
assertTrue(messages.contains("attribute info.bad is unexpected"));
assertTrue(messages.contains("attribute info.contact.invalid is unexpected"));
assertEquals(result.getOpenAPI().getInfo().getExtensions().get("x-foo").toString(), "bar");
}
Aggregations