use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-parser by swagger-api.
the class OpenAPIV3ParserTest method testIssue450.
@Test
public void testIssue450() {
String desc = "An array of Pets";
String xTag = "x-my-tag";
String xVal = "An extension tag";
String yaml = "openapi: 3.0.0\n" + "servers: []\n" + "info:\n" + " version: 0.0.0\n" + " title: Simple API\n" + "paths:\n" + " /:\n" + " get:\n" + " responses:\n" + " '200':\n" + " description: OK\n" + "components:\n" + " schemas:\n" + " PetArray:\n" + " type: array\n" + " items:\n" + " $ref: '#/components/schemas/Pet'\n" + " description: An array of Pets\n" + " x-my-tag: An extension tag\n" + " Pet:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: string";
SwaggerParseResult result = new OpenAPIV3Parser().readContents(yaml, null, null);
assertNotNull(result.getOpenAPI());
final OpenAPI openAPI = result.getOpenAPI();
Schema petArray = openAPI.getComponents().getSchemas().get("PetArray");
assertNotNull(petArray);
assertTrue(petArray instanceof ArraySchema);
assertEquals(petArray.getDescription(), desc);
assertNotNull(petArray.getExtensions());
assertNotNull(petArray.getExtensions().get(xTag));
assertEquals(petArray.getExtensions().get(xTag), xVal);
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-parser by swagger-api.
the class OpenAPIDeserializerTest method readInfoObject.
@Test(dataProvider = "data")
public void readInfoObject(JsonNode rootNode) throws Exception {
final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
final SwaggerParseResult result = deserializer.deserialize(rootNode);
Assert.assertNotNull(result);
final OpenAPI openAPI = result.getOpenAPI();
Assert.assertNotNull(openAPI);
Assert.assertEquals(openAPI.getOpenapi(), "3.0.1");
final Info info = openAPI.getInfo();
Assert.assertNotNull(info);
Assert.assertEquals(info.getTitle(), "Sample Pet Store App");
Assert.assertEquals(info.getDescription(), "This is a sample server Petstore");
Assert.assertEquals(info.getTermsOfService(), "http://swagger.io/terms/");
Assert.assertNotNull(info.getExtensions().get("x-info"));
Assert.assertEquals(info.getExtensions().get("x-info").toString(), "info extension");
final Contact contact = info.getContact();
Assert.assertNotNull(contact);
Assert.assertEquals(contact.getName(), "API Support");
Assert.assertEquals(contact.getUrl(), "http://www.example.com/support");
Assert.assertEquals(contact.getEmail(), "support@example.com");
Assert.assertNotNull(contact.getExtensions().get("x-contact"));
Assert.assertEquals(contact.getExtensions().get("x-contact").toString(), "contact extension");
final License license = info.getLicense();
Assert.assertNotNull(license);
Assert.assertEquals(license.getName(), "Apache 2.0");
Assert.assertEquals(license.getUrl(), "http://www.apache.org/licenses/LICENSE-2.0.html");
Assert.assertNotNull(license.getExtensions());
Assert.assertEquals(info.getVersion(), "1.0.1");
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class OpenAPI3_1DeserializationTest method testDeserializationOnOAS31.
@Test
public void testDeserializationOnOAS31() throws IOException {
final String jsonString = ResourceUtils.loadClassResource(getClass(), "specFiles/3.1.0/petstore-3.1_sample.yaml");
OpenAPI openAPI = Yaml31.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(openAPI);
assertEquals(openAPI.getInfo().getTitle(), "Swagger Petstore");
assertEquals(openAPI.getInfo().getVersion(), "1.0.0");
assertEquals(openAPI.getInfo().getSummary(), "petstore sample for OAS 3.1.0");
assertEquals(openAPI.getInfo().getLicense().getName(), "MIT");
assertEquals(openAPI.getInfo().getLicense().getIdentifier(), "test");
assertNotNull(openAPI.getWebhooks());
assertFalse(openAPI.getWebhooks().isEmpty());
assertNotNull(openAPI.getWebhooks().get("newPet"));
assertNotNull(openAPI.getWebhooks().get("newPet").getPost());
assertNotNull(openAPI.getComponents().getPathItems());
assertNotNull(openAPI.getComponents().getPathItems().get("/pet"));
assertEquals(openAPI.getComponents().getPathItems().get("/pet").getDescription(), "get a pet");
assertNotNull(openAPI.getComponents().getPathItems().get("/pet").getGet());
assertEquals(openAPI.getComponents().getPathItems().get("/pet").getGet().getOperationId(), "getPet");
assertNotNull(openAPI.getComponents().getSchemas());
assertNotNull(openAPI.getComponents().getSchemas().get("Pet"));
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator());
assertNotNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
assertEquals(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions().get("x-test-extension"), "extended");
assertNotNull(openAPI.getComponents().getResponses());
assertNotNull(openAPI.getComponents().getResponses().get("201"));
assertEquals(openAPI.getComponents().getResponses().get("201").getDescription(), "api response description");
assertNotNull(openAPI.getComponents().getParameters());
assertNotNull(openAPI.getComponents().getParameters().get("param"));
assertEquals(openAPI.getComponents().getParameters().get("param").getIn(), "query");
assertEquals(openAPI.getComponents().getParameters().get("param").getName(), "param0");
assertEquals(openAPI.getComponents().getParameters().get("param").getDescription(), "parameter description");
assertNotNull(openAPI.getComponents().getExamples());
assertNotNull(openAPI.getComponents().getExamples().get("example"));
assertEquals(openAPI.getComponents().getExamples().get("example").getDescription(), "example description");
assertEquals(openAPI.getComponents().getExamples().get("example").getSummary(), "example summary");
assertEquals(openAPI.getComponents().getExamples().get("example").getValue(), "This is an example");
assertNotNull(openAPI.getComponents().getRequestBodies());
assertNotNull(openAPI.getComponents().getRequestBodies().get("body"));
assertNotNull(openAPI.getComponents().getHeaders());
assertNotNull(openAPI.getComponents().getHeaders().get("test-head"));
assertEquals(openAPI.getComponents().getHeaders().get("test-head").getDescription(), "test header description");
assertNotNull(openAPI.getComponents().getSecuritySchemes());
assertNotNull(openAPI.getComponents().getSecuritySchemes().get("basic"));
assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getDescription(), "security description");
assertEquals(openAPI.getComponents().getSecuritySchemes().get("basic").getType().toString(), "http");
assertNotNull(openAPI.getComponents().getLinks());
assertNotNull(openAPI.getComponents().getLinks().get("Link"));
assertEquals(openAPI.getComponents().getLinks().get("Link").getOperationRef(), "#/paths/~12.0~1repositories~1{username}/get");
assertNotNull(openAPI.getComponents().getCallbacks());
assertNotNull(openAPI.getComponents().getCallbacks().get("TestCallback"));
openAPI = Yaml.mapper().readValue(jsonString, OpenAPI.class);
assertNotNull(openAPI);
assertNull(openAPI.getInfo().getSummary());
assertNull(openAPI.getWebhooks());
assertNull(openAPI.getComponents().getPathItems());
assertNull(openAPI.getComponents().getSchemas().get("Pet").getDiscriminator().getExtensions());
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class AnnotationsUtilsExtensionsTest method extensionsTest.
@Test(dataProvider = "expectedData")
public void extensionsTest(String methodName, Map<String, Object> expected) throws NoSuchMethodException {
final Method method = getClass().getDeclaredMethod(methodName);
final Extension[] extensions = method.getAnnotation(Operation.class).extensions();
final Map<String, Object> map = AnnotationsUtils.getExtensions(extensions);
Assert.assertEquals(map, expected);
}
use of io.swagger.v3.oas.annotations.extensions.Extension in project swagger-core by swagger-api.
the class SwaggerMojo method readStructuredDataFromFile.
/**
* Read the content of given file as either json or yaml and maps it to given class
*
* @param filePath to read content from
* @param outputClass to map to
* @param configName for logging, what user config will be read
* @param <T> mapped type
* @return empty optional if not path was given or the file was empty, read instance otherwis
* @throws MojoFailureException if given path is not file, could not be read or is not proper json or yaml
*/
private <T> Optional<T> readStructuredDataFromFile(String filePath, Class<T> outputClass, String configName) throws MojoFailureException {
try {
// ignore if config is not provided
if (StringUtils.isBlank(filePath)) {
return Optional.empty();
}
Path pathObj = Paths.get(filePath);
// if file does not exist or is not an actual file, finish with error
if (!pathObj.toFile().exists() || !pathObj.toFile().isFile()) {
throw new IllegalArgumentException(format("passed path does not exist or is not a file: '%s'", filePath));
}
String fileContent = new String(Files.readAllBytes(pathObj), encoding);
// if provided file is empty, log warning and finish
if (StringUtils.isBlank(fileContent)) {
getLog().warn(format("It seems that file '%s' defined in config %s is empty", pathObj.toString(), configName));
return Optional.empty();
}
// get mappers in the order based on file extension
List<BiFunction<String, Class<T>, T>> mappers = getSortedMappers(pathObj);
T instance = null;
Throwable caughtEx = null;
// iterate through mappers and see if one is able to parse
for (BiFunction<String, Class<T>, T> mapper : mappers) {
try {
instance = mapper.apply(fileContent, outputClass);
break;
} catch (Exception e) {
caughtEx = e;
}
}
// if no mapper could read the content correctly, finish with error
if (instance == null) {
if (caughtEx == null) {
caughtEx = new IllegalStateException("undefined state");
}
getLog().error(format("Could not read file '%s' for config %s", pathObj.toString(), configName), caughtEx);
throw new IllegalStateException(caughtEx.getMessage(), caughtEx);
}
return Optional.of(instance);
} catch (Exception e) {
getLog().error(format("Error reading/deserializing config %s file", configName), e);
throw new MojoFailureException(e.getMessage(), e);
}
}
Aggregations