Search in sources :

Example 1 with ApiResponses

use of io.swagger.v3.oas.annotations.responses.ApiResponses in project swagger-core by swagger-api.

the class ApiResponsesDeserializer method deserialize.

@Override
public ApiResponses deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    final ObjectMapper mapper;
    if (openapi31) {
        mapper = Json31.mapper();
    } else {
        mapper = Json.mapper();
    }
    ApiResponses result = new ApiResponses();
    JsonNode node = jp.getCodec().readTree(jp);
    ObjectNode objectNode = (ObjectNode) node;
    Map<String, Object> extensions = new LinkedHashMap<>();
    for (Iterator<String> it = objectNode.fieldNames(); it.hasNext(); ) {
        String childName = it.next();
        JsonNode child = objectNode.get(childName);
        // if name start with `x-` consider it an extension
        if (childName.startsWith("x-")) {
            extensions.put(childName, mapper.convertValue(child, Object.class));
        } else {
            result.put(childName, mapper.convertValue(child, ApiResponse.class));
        }
    }
    if (!extensions.isEmpty()) {
        result.setExtensions(extensions);
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ApiResponses

use of io.swagger.v3.oas.annotations.responses.ApiResponses in project swagger-core by swagger-api.

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.addExtension("x-test2", map);
    info.addExtension("x-test", "value");
    final OpenAPI swagger = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id").deprecated(true);
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Person")).example("fun")));
    final ApiResponse errorResponse = new ApiResponse().description("error response").addLink("myLink", new Link().description("a link").operationId("theLinkedOperationId").addParameter("userId", "gah")).content(new Content().addMediaType("application/json", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse));
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").addTagsItem("Pet Operations").operationId("add pet").responses(new ApiResponses().addApiResponse("default", errorResponse)).requestBody(new RequestBody().description("the pet to add").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person")))));
    swagger.paths(new Paths().addPathItem("/pets", new PathItem().get(get).post(post)));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    Json.prettyPrint(swagger);
    final OpenAPI rebuilt = Json.mapper().readValue(swaggerJson, OpenAPI.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) HashMap(java.util.HashMap) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Paths(io.swagger.v3.oas.models.Paths) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Link(io.swagger.v3.oas.models.links.Link) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 3 with ApiResponses

use of io.swagger.v3.oas.annotations.responses.ApiResponses in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testPathItemsRefSerialization.

@Test
public void testPathItemsRefSerialization() {
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/pathTest", new PathItem().$ref("#/components/pathItems/pathTest").description("This is a ref path item").summary("ref path item")).components(new Components().addPathItem("pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + "  /pathTest:\n" + "    $ref: '#/components/pathItems/pathTest'\n" + "    description: This is a ref path item\n" + "    summary: ref path item\n" + "components:\n" + "  pathItems:\n" + "    pathTest:\n" + "      description: test path item\n" + "      get:\n" + "        operationId: testPathItem\n" + "        responses:\n" + "          \"200\":\n" + "            description: response description");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"paths\" : {\n" + "    \"/pathTest\" : {\n" + "      \"summary\" : \"ref path item\",\n" + "      \"description\" : \"This is a ref path item\",\n" + "      \"$ref\" : \"#/components/pathItems/pathTest\"\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"pathItems\" : {\n" + "      \"pathTest\" : {\n" + "        \"description\" : \"test path item\",\n" + "        \"get\" : {\n" + "          \"operationId\" : \"testPathItem\",\n" + "          \"responses\" : {\n" + "            \"200\" : {\n" + "              \"description\" : \"response description\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 4 with ApiResponses

use of io.swagger.v3.oas.annotations.responses.ApiResponses in project swagger-core by swagger-api.

the class SecurityDefinitionTest method createModelWithSecurityRequirements.

@Test(description = "it should create a model with security requirements")
public void createModelWithSecurityRequirements() throws IOException {
    final Schema personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Schema errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final OpenAPI oas = new OpenAPI().info(info).addServersItem(new Server().url("http://petstore.swagger.io")).schema("Person", personModel).schema("Error", errorModel);
    oas.schemaRequirement("githubAccessCode", new SecurityScheme().flows(new OAuthFlows().authorizationCode(new OAuthFlow().scopes(new Scopes().addString("user:email", "Grants read access to a user’s email addresses.")))));
    final Operation get = new Operation().summary("finds pets in the system").description("a longer description").addTagsItem("Pet Operations").operationId("get pet by id");
    get.addParametersItem(new Parameter().in("query").name("tags").description("tags to filter by").required(false).schema(new StringSchema()));
    get.addParametersItem(new Parameter().in("path").name("petId").description("pet to fetch").schema(new IntegerSchema().format("int64")));
    final ApiResponse response = new ApiResponse().description("pets returned").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Person"))));
    final ApiResponse errorResponse = new ApiResponse().description("error response").content(new Content().addMediaType("*/*", new MediaType().schema(new Schema().$ref("Error"))));
    get.responses(new ApiResponses().addApiResponse("200", response).addApiResponse("default", errorResponse)).addSecurityItem(new SecurityRequirement().addList("internal_oauth2", "user:email")).addSecurityItem(new SecurityRequirement().addList("api_key"));
    oas.path("/pets", new PathItem().get(get));
    final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
    SerializationMatchers.assertEqualsToJson(oas, json);
}
Also used : Server(io.swagger.v3.oas.models.servers.Server) OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Contact(io.swagger.v3.oas.models.info.Contact) PathItem(io.swagger.v3.oas.models.PathItem) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) Content(io.swagger.v3.oas.models.media.Content) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) Person(io.swagger.v3.core.oas.models.Person) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement) Test(org.testng.annotations.Test)

Example 5 with ApiResponses

use of io.swagger.v3.oas.annotations.responses.ApiResponses in project swagger-core by swagger-api.

the class OpenAPI3_1SerializationTest method testResponseRefSerialization.

@Test
public void testResponseRefSerialization() {
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/test", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("point to a $ref response").$ref("#/components/responses/okResponse"))))).components(new Components().addResponses("okResponse", new ApiResponse().description("everything is good")));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + "  /test:\n" + "    description: test path item\n" + "    get:\n" + "      operationId: testPathItem\n" + "      responses:\n" + "        \"200\":\n" + "          description: point to a $ref response\n" + "          $ref: '#/components/responses/okResponse'\n" + "components:\n" + "  responses:\n" + "    okResponse:\n" + "      description: everything is good");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"paths\" : {\n" + "    \"/test\" : {\n" + "      \"description\" : \"test path item\",\n" + "      \"get\" : {\n" + "        \"operationId\" : \"testPathItem\",\n" + "        \"responses\" : {\n" + "          \"200\" : {\n" + "            \"description\" : \"point to a $ref response\",\n" + "            \"$ref\" : \"#/components/responses/okResponse\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"responses\" : {\n" + "      \"okResponse\" : {\n" + "        \"description\" : \"everything is good\"\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)99 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)99 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)47 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)47 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)46 Operation (io.swagger.v3.oas.models.Operation)39 OpenAPI (io.swagger.v3.oas.models.OpenAPI)34 PathItem (io.swagger.v3.oas.models.PathItem)34 Test (org.testng.annotations.Test)31 ArrayList (java.util.ArrayList)23 Schema (io.swagger.v3.oas.models.media.Schema)22 StringSchema (io.swagger.v3.oas.models.media.StringSchema)21 Content (io.swagger.v3.oas.models.media.Content)20 MediaType (io.swagger.v3.oas.models.media.MediaType)20 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)19 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)17 Path (javax.ws.rs.Path)17 Produces (javax.ws.rs.Produces)17 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)16 Components (io.swagger.v3.oas.models.Components)10