Search in sources :

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

use of io.swagger.v3.oas.models.security 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 12 with io.swagger.v3.oas.models.security

use of io.swagger.v3.oas.models.security 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 13 with io.swagger.v3.oas.models.security

use of io.swagger.v3.oas.models.security 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)

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

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

the class ParametersTest method scanClassAndFieldLevelAnnotations.

@Test(description = "scan class level and field level annotations")
public void scanClassAndFieldLevelAnnotations() {
    Reader reader = new Reader(new OpenAPI());
    OpenAPI openAPI = reader.read(ResourceWithKnownInjections.class);
    List<io.swagger.v3.oas.models.parameters.Parameter> resourceParameters = openAPI.getPaths().get("/resource/{id}").getGet().getParameters();
    assertNotNull(resourceParameters);
    assertEquals(resourceParameters.size(), 3);
    assertEquals(resourceParameters.get(0).getName(), "id");
    assertEquals(resourceParameters.get(1).getName(), "fieldParam");
    assertEquals(resourceParameters.get(2).getName(), "methodParam");
}
Also used : Reader(io.swagger.v3.jaxrs2.Reader) Parameter(io.swagger.v3.oas.annotations.Parameter) OpenAPI(io.swagger.v3.oas.models.OpenAPI) AbstractAnnotationTest(io.swagger.v3.jaxrs2.annotations.AbstractAnnotationTest) Test(org.testng.annotations.Test)

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

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

the class OperationParser method getRequestBody.

public static Optional<RequestBody> getRequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody requestBody, Consumes classConsumes, Consumes methodConsumes, Components components, JsonView jsonViewAnnotation) {
    if (requestBody == null) {
        return Optional.empty();
    }
    RequestBody requestBodyObject = new RequestBody();
    boolean isEmpty = true;
    if (StringUtils.isNotBlank(requestBody.ref())) {
        requestBodyObject.set$ref(requestBody.ref());
        return Optional.of(requestBodyObject);
    }
    if (StringUtils.isNotBlank(requestBody.description())) {
        requestBodyObject.setDescription(requestBody.description());
        isEmpty = false;
    }
    if (requestBody.required()) {
        requestBodyObject.setRequired(requestBody.required());
        isEmpty = false;
    }
    if (requestBody.extensions().length > 0) {
        Map<String, Object> extensions = AnnotationsUtils.getExtensions(requestBody.extensions());
        if (extensions != null) {
            extensions.forEach(requestBodyObject::addExtension);
        }
        isEmpty = false;
    }
    if (requestBody.content().length > 0) {
        isEmpty = false;
    }
    if (isEmpty) {
        return Optional.empty();
    }
    AnnotationsUtils.getContent(requestBody.content(), classConsumes == null ? new String[0] : classConsumes.value(), methodConsumes == null ? new String[0] : methodConsumes.value(), null, components, jsonViewAnnotation).ifPresent(requestBodyObject::setContent);
    return Optional.of(requestBodyObject);
}
Also used : RequestBody(io.swagger.v3.oas.models.parameters.RequestBody)

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