Search in sources :

Example 11 with io.swagger.v3.oas.models.media

use of io.swagger.v3.oas.models.media in project swagger-core by swagger-api.

the class AnnotationsUtils method getExternalDocumentation.

public static Optional<ExternalDocumentation> getExternalDocumentation(io.swagger.v3.oas.annotations.ExternalDocumentation externalDocumentation) {
    if (externalDocumentation == null) {
        return Optional.empty();
    }
    boolean isEmpty = true;
    ExternalDocumentation external = new ExternalDocumentation();
    if (StringUtils.isNotBlank(externalDocumentation.description())) {
        isEmpty = false;
        external.setDescription(externalDocumentation.description());
    }
    if (StringUtils.isNotBlank(externalDocumentation.url())) {
        isEmpty = false;
        external.setUrl(externalDocumentation.url());
    }
    if (externalDocumentation.extensions() != null && externalDocumentation.extensions().length > 0) {
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(externalDocumentation.extensions());
        if (extensions != null) {
            extensions.forEach(external::addExtension);
            isEmpty = false;
        }
    }
    if (isEmpty) {
        return Optional.empty();
    }
    return Optional.of(external);
}
Also used : ExternalDocumentation(io.swagger.v3.oas.models.ExternalDocumentation) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

Example 12 with io.swagger.v3.oas.models.media

use of io.swagger.v3.oas.models.media in project swagger-core by swagger-api.

the class AnnotationsUtils method getInfo.

public static Optional<Info> getInfo(io.swagger.v3.oas.annotations.info.Info info) {
    if (info == null) {
        return Optional.empty();
    }
    boolean isEmpty = true;
    Info infoObject = new Info();
    if (StringUtils.isNotBlank(info.description())) {
        infoObject.setDescription(info.description());
        isEmpty = false;
    }
    if (StringUtils.isNotBlank(info.termsOfService())) {
        infoObject.setTermsOfService(info.termsOfService());
        isEmpty = false;
    }
    if (StringUtils.isNotBlank(info.title())) {
        infoObject.setTitle(info.title());
        isEmpty = false;
    }
    if (StringUtils.isNotBlank(info.version())) {
        infoObject.setVersion(info.version());
        isEmpty = false;
    }
    if (info.extensions() != null && info.extensions().length > 0) {
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(info.extensions());
        if (extensions != null) {
            extensions.forEach(infoObject::addExtension);
            isEmpty = false;
        }
    }
    if (isEmpty) {
        return Optional.empty();
    }
    getContact(info.contact()).ifPresent(infoObject::setContact);
    getLicense(info.license()).ifPresent(infoObject::setLicense);
    return Optional.of(infoObject);
}
Also used : ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject) Info(io.swagger.v3.oas.models.info.Info)

Example 13 with io.swagger.v3.oas.models.media

use of io.swagger.v3.oas.models.media in project swagger-core by swagger-api.

the class AnnotationsUtils method getServer.

public static Optional<Server> getServer(io.swagger.v3.oas.annotations.servers.Server server) {
    if (server == null) {
        return Optional.empty();
    }
    Server serverObject = new Server();
    boolean isEmpty = true;
    if (StringUtils.isNotBlank(server.url())) {
        serverObject.setUrl(server.url());
        isEmpty = false;
    }
    if (StringUtils.isNotBlank(server.description())) {
        serverObject.setDescription(server.description());
        isEmpty = false;
    }
    if (server.extensions().length > 0) {
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(server.extensions());
        if (extensions != null) {
            extensions.forEach(serverObject::addExtension);
        }
        isEmpty = false;
    }
    if (isEmpty) {
        return Optional.empty();
    }
    io.swagger.v3.oas.annotations.servers.ServerVariable[] serverVariables = server.variables();
    ServerVariables serverVariablesObject = new ServerVariables();
    for (io.swagger.v3.oas.annotations.servers.ServerVariable serverVariable : serverVariables) {
        ServerVariable serverVariableObject = new ServerVariable();
        if (StringUtils.isNotBlank(serverVariable.description())) {
            serverVariableObject.setDescription(serverVariable.description());
        }
        if (StringUtils.isNotBlank(serverVariable.defaultValue())) {
            serverVariableObject.setDefault(serverVariable.defaultValue());
        }
        if (serverVariable.allowableValues() != null && serverVariable.allowableValues().length > 0) {
            if (StringUtils.isNotBlank(serverVariable.allowableValues()[0])) {
                serverVariableObject.setEnum(Arrays.asList(serverVariable.allowableValues()));
            }
        }
        if (serverVariable.extensions() != null && serverVariable.extensions().length > 0) {
            Map<String, Object> extensions = AnnotationsUtils.getExtensions(serverVariable.extensions());
            if (extensions != null) {
                extensions.forEach(serverVariableObject::addExtension);
            }
        }
        serverVariablesObject.addServerVariable(serverVariable.name(), serverVariableObject);
    }
    serverObject.setVariables(serverVariablesObject);
    return Optional.of(serverObject);
}
Also used : ServerVariables(io.swagger.v3.oas.models.servers.ServerVariables) Server(io.swagger.v3.oas.models.servers.Server) ServerVariable(io.swagger.v3.oas.models.servers.ServerVariable) ExampleObject(io.swagger.v3.oas.annotations.media.ExampleObject)

Example 14 with io.swagger.v3.oas.models.media

use of io.swagger.v3.oas.models.media in project swagger-core by swagger-api.

the class RequestBodyTest method testRequestBodyAnnotationPriority.

@Test(description = "scan class with requesbody annotation")
public void testRequestBodyAnnotationPriority() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(RequestBodyResource.class);
    PathItem userPathItem = openAPI.getPaths().get(USER_PATH);
    io.swagger.v3.oas.models.parameters.RequestBody getRequestBody = userPathItem.getGet().getRequestBody();
    assertNotNull(getRequestBody);
    assertEquals(getRequestBody.getDescription(), REQUEST_BODY_IN_ANNOTATION);
    io.swagger.v3.oas.models.parameters.RequestBody postRequestBody = userPathItem.getPost().getRequestBody();
    assertNotNull(postRequestBody);
    assertEquals(postRequestBody.getDescription(), REQUEST_BODY_IN_ANNOTATION);
    io.swagger.v3.oas.models.parameters.RequestBody putRequestBody = userPathItem.getPut().getRequestBody();
    assertNotNull(putRequestBody);
    assertEquals(putRequestBody.getDescription(), REQUEST_BODY_IN_METHOD);
    io.swagger.v3.oas.models.parameters.RequestBody deleteRequestBody = userPathItem.getDelete().getRequestBody();
    assertNotNull(deleteRequestBody);
    assertEquals(deleteRequestBody.getDescription(), REQUEST_BODY_IN_METHOD);
    io.swagger.v3.oas.models.parameters.RequestBody patchRequestBody = userPathItem.getPatch().getRequestBody();
    assertNotNull(patchRequestBody);
    assertEquals(patchRequestBody.getDescription(), REQUEST_BODY_IN_METHOD);
    userPathItem = openAPI.getPaths().get(USER_PATH + "/deleteUserMethod_Param_RequestBody");
    deleteRequestBody = userPathItem.getDelete().getRequestBody();
    assertNotNull(deleteRequestBody);
    assertEquals(deleteRequestBody.getDescription(), REQUEST_BODY_IN_PARAMETER);
    userPathItem = openAPI.getPaths().get(USER_PATH + "/deleteUserOperation_Method_Param_RequestBody");
    deleteRequestBody = userPathItem.getDelete().getRequestBody();
    assertNotNull(deleteRequestBody);
    assertEquals(deleteRequestBody.getDescription(), REQUEST_BODY_IN_PARAMETER);
    userPathItem = openAPI.getPaths().get(USER_PATH + "/deleteUserOperation_RequestBody");
    deleteRequestBody = userPathItem.getDelete().getRequestBody();
    assertNotNull(deleteRequestBody);
    assertEquals(deleteRequestBody.getDescription(), REQUEST_BODY_IN_PARAMETER);
    userPathItem = openAPI.getPaths().get(USER_PATH + "/deleteUserOperation_Method_Param");
    deleteRequestBody = userPathItem.getDelete().getRequestBody();
    assertNotNull(deleteRequestBody);
    assertEquals(deleteRequestBody.getDescription(), REQUEST_BODY_IN_METHOD);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Reader(io.swagger.v3.jaxrs2.Reader) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test) AbstractAnnotationTest(io.swagger.v3.jaxrs2.annotations.AbstractAnnotationTest)

Example 15 with io.swagger.v3.oas.models.media

use of io.swagger.v3.oas.models.media in project swagger-core by swagger-api.

the class ParametersTest method testJacksonFeatures.

@Test(description = "JsonUnwrapped, JsonIgnore, JsonValue should be honoured")
public void testJacksonFeatures() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(ResourceWithJacksonBean.class);
    io.swagger.v3.oas.models.media.Schema o = openAPI.getComponents().getSchemas().get("JacksonBean");
    assertEquals(o.getProperties().keySet(), Stream.of("identity", "bean", "code", "message", "precodesuf", "premessagesuf").collect(Collectors.toSet()));
}
Also used : Reader(io.swagger.v3.jaxrs2.Reader) OpenAPI(io.swagger.v3.oas.models.OpenAPI) AbstractAnnotationTest(io.swagger.v3.jaxrs2.annotations.AbstractAnnotationTest) Test(org.testng.annotations.Test)

Aggregations

Schema (io.swagger.v3.oas.models.media.Schema)13 ExampleObject (io.swagger.v3.oas.annotations.media.ExampleObject)11 OpenAPI (io.swagger.v3.oas.models.OpenAPI)10 Annotation (java.lang.annotation.Annotation)8 Test (org.testng.annotations.Test)8 ResolvedSchema (io.swagger.v3.core.converter.ResolvedSchema)7 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)7 Content (io.swagger.v3.oas.models.media.Content)7 ArrayList (java.util.ArrayList)7 PathItem (io.swagger.v3.oas.models.PathItem)6 Parameter (io.swagger.v3.oas.models.parameters.Parameter)6 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)5 AnnotatedType (io.swagger.v3.core.converter.AnnotatedType)5 Hidden (io.swagger.v3.oas.annotations.Hidden)5 MediaType (io.swagger.v3.oas.models.media.MediaType)5 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)5 Map (java.util.Map)5 JavaType (com.fasterxml.jackson.databind.JavaType)4 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)4