use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method test30.
@Test
public void test30(@Injectable final List<AuthorizationValue> auths) throws Exception {
String pathFile = FileUtils.readFileToString(new File("src/test/resources/oas3.yaml.template"));
pathFile = pathFile.replace("${dynamicPort}", String.valueOf(this.serverPort));
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = new OpenAPIV3Parser().readContents(pathFile, auths, options);
Assert.assertNotNull(result);
Assert.assertNotNull(result.getOpenAPI());
assertEquals(result.getOpenAPI().getOpenapi(), "3.0.1");
assertEquals(result.getOpenAPI().getComponents().getSchemas().get("OrderRef").getType(), "object");
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project flow by vaadin.
the class OpenAPIParser method parseOpenAPI.
OpenAPI parseOpenAPI() {
SwaggerParseResult result;
try {
List<AuthorizationValue> authorizationValues = AuthParser.parse(configurator.getAuth());
String inputSpec = configurator.loadSpecContent(configurator.getInputSpecURL(), authorizationValues);
ParseOptions options = new ParseOptions();
options.setResolve(true);
result = new io.swagger.parser.OpenAPIParser().readContents(inputSpec, authorizationValues, options);
} catch (Exception e) {
throw new IllegalStateException(String.format("Can't read file '%s'", configurator.getInputSpecURL()), e);
}
if (result == null || !result.getMessages().isEmpty()) {
String error = result == null ? "" : String.join("", result.getMessages());
throw new IllegalStateException("Unexpected error while generating Vaadin TypeScript endpoint wrappers." + " The input file " + configurator.getInputSpecURL() + " might be corrupted, please try running the generating tasks again. " + error);
}
OpenAPI openAPI = result.getOpenAPI();
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
return openAPI;
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testAllOfSchema.
@Test
public void testAllOfSchema(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: '3.0'\n" + "components:\n" + " schemas:\n" + " Pet:\n" + " type: object\n" + " required:\n" + " - pet_type\n" + " properties:\n" + " pet_type:\n" + " type: string\n" + " Cat:\n" + " allOf:\n" + " - $ref: '#/components/schemas/Pet'\n" + " - type: object\n" + " # all other properties specific to a `Cat`\n" + " properties:\n" + " name:\n" + " type: string\n";
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);
Schema catSchema = result.getOpenAPI().getComponents().getSchemas().get("Cat");
assertTrue(catSchema != null);
assertTrue(catSchema instanceof ComposedSchema);
ComposedSchema catCompSchema = (ComposedSchema) catSchema;
List<Schema> allOfSchemas = catCompSchema.getAllOf();
assertTrue(allOfSchemas != null);
assertEquals(allOfSchemas.size(), 2);
Schema refPetSchema = allOfSchemas.get(0);
assertTrue(refPetSchema != null);
assertEquals(refPetSchema.get$ref(), "#/components/schemas/Pet");
Schema otherSchema = allOfSchemas.get(1);
assertTrue(otherSchema != null);
assertTrue(otherSchema.getProperties() != null);
Schema nameProp = (Schema) otherSchema.getProperties().get("name");
assertTrue(nameProp != null);
assertEquals(nameProp.getType(), "string");
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testSchemaExample.
@Test
public void testSchemaExample(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: '3.0.1'\n" + "components:\n" + " schemas:\n" + " Address:\n" + " required:\n" + " - street\n" + " type: object\n" + " x-swagger-router-model: io.swagger.oas.test.models.Address\n" + " properties:\n" + " street:\n" + " type: string\n" + " example: 12345 El Monte Road\n" + " city:\n" + " type: string\n" + " example: Los Altos Hills\n" + " state:\n" + " type: string\n" + " example: CA\n" + " zip:\n" + " type: string\n" + " example: '94022'";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
OpenAPI openAPI = result.getOpenAPI();
Schema stateSchemaProperty = (Schema) openAPI.getComponents().getSchemas().get("Address").getProperties().get("state");
Assert.assertNotNull(stateSchemaProperty.getExample());
Assert.assertEquals(stateSchemaProperty.getExample(), "CA");
}
use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method testExamples.
@Test
public void testExamples(@Injectable List<AuthorizationValue> auths) {
String yaml = "openapi: 3.0.1\n" + "info:\n" + " title: httpbin\n" + " version: 0.0.0\n" + "servers:\n" + " - url: http://httpbin.org\n" + "paths:\n" + " /post:\n" + " post:\n" + " summary: Returns the POSTed data\n" + " requestBody:\n" + " content:\n" + " application/json:\n" + " schema:\n" + " $ref: '#/components/schemas/AnyValue'\n" + " examples:\n" + " AnObject:\n" + " $ref: '#/components/examples/AnObject'\n" + " ANull:\n" + " $ref: '#/components/examples/ANull'\n" + " application/yaml:\n" + " schema:\n" + " $ref: '#/components/schemas/AnyValue'\n" + " examples:\n" + " AString:\n" + " $ref: '#/components/examples/AString'\n" + " AnArray:\n" + " $ref: '#/components/examples/AnArray'\n" + " text/plain:\n" + " schema:\n" + " type: string\n" + " example: Hi there\n" + " application/x-www-form-urlencoded:\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " name:\n" + " type: string\n" + " example:\n" + " id: 42\n" + " name: Arthur Dent\n" + " responses:\n" + " '200':\n" + " description: OK\n" + " content:\n" + " application/json:\n" + " schema:\n" + " type: object\n" + "\n" + " #/response-headers:\n" + " /:\n" + " get:\n" + " summary: Returns a response with the specified headers\n" + " parameters:\n" + " - in: header\n" + " name: Server\n" + " required: true\n" + " schema:\n" + " type: string\n" + " examples:\n" + " httpbin:\n" + " value: httpbin\n" + " unicorn:\n" + " value: unicorn\n" + " - in: header\n" + " name: X-Request-Id\n" + " required: true\n" + " schema:\n" + " type: integer\n" + " example: 37\n" + " responses:\n" + " '200':\n" + " description: A response with the specified headers\n" + " headers:\n" + " Server:\n" + " schema:\n" + " type: string\n" + " examples:\n" + " httpbin:\n" + " value: httpbin\n" + " unicorn:\n" + " value: unicorn\n" + " X-Request-Id:\n" + " schema:\n" + " type: integer\n" + " example: 37\n" + "\n" + "components:\n" + " schemas:\n" + " AnyValue:\n" + " nullable: true\n" + " description: Can be anything - string, object, array, null, etc.\n" + "\n" + " examples:\n" + " AString:\n" + " value: Hi there\n" + " ANumber:\n" + " value: 42\n" + " ANull:\n" + " value: null\n" + " AnArray:\n" + " value: [1, 2, 3]\n" + " AnObject:\n" + " value:\n" + " id: 42\n" + " name: Arthur Dent";
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
SwaggerParseResult result = parser.readContents(yaml, auths, options);
OpenAPI openAPI = result.getOpenAPI();
MediaType mediaTypeJson = openAPI.getPaths().get("/post").getPost().getRequestBody().getContent().get("application/json");
Header header1 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("Server");
Header header2 = openAPI.getPaths().get("/").getGet().getResponses().get("200").getHeaders().get("X-Request-Id");
Parameter parameter1 = openAPI.getPaths().get("/").getGet().getParameters().get(0);
Parameter parameter2 = openAPI.getPaths().get("/").getGet().getParameters().get(1);
Assert.assertNotNull(mediaTypeJson.getExamples());
Assert.assertEquals(mediaTypeJson.getExamples().get("AnObject").get$ref(), "#/components/examples/AnObject");
Assert.assertNotNull(header1.getExamples());
Assert.assertEquals(header1.getExamples().get("httpbin").getValue(), "httpbin");
Assert.assertNotNull(header2.getExample());
Assert.assertEquals(header2.getExample(), 37);
Assert.assertNotNull(parameter1.getExamples());
Assert.assertEquals(parameter1.getExamples().get("unicorn").getValue(), "unicorn");
Assert.assertNotNull(parameter2.getExample());
Assert.assertEquals(parameter2.getExample(), 37);
}
Aggregations