Search in sources :

Example 16 with Callback

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

the class OpenAPI3_1SerializationTest method testCallRefSerialization.

@Test
public void testCallRefSerialization() {
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").path("/test", new PathItem().description("test path item").post(new Operation().operationId("testPathItem").addCallback("callbackSample", new Callback().$ref("#/components/callbacks/TestCallback")))).components(new Components().addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "paths:\n" + "  /test:\n" + "    description: test path item\n" + "    post:\n" + "      operationId: testPathItem\n" + "      callbacks:\n" + "        callbackSample:\n" + "          $ref: '#/components/callbacks/TestCallback'\n" + "components:\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem");
    SerializationMatchers.assertEqualsToJson31(openAPI, "{\n" + "  \"openapi\" : \"3.1.0\",\n" + "  \"paths\" : {\n" + "    \"/test\" : {\n" + "      \"description\" : \"test path item\",\n" + "      \"post\" : {\n" + "        \"operationId\" : \"testPathItem\",\n" + "        \"callbacks\" : {\n" + "          \"callbackSample\" : {\n" + "            \"$ref\" : \"#/components/callbacks/TestCallback\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  },\n" + "  \"components\" : {\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 17 with Callback

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

the class OpenAPI3_1SerializationTest method testComponentPathItemsSerialization.

@Test
public void testComponentPathItemsSerialization() {
    Schema schema = new StringSchema();
    schema.addType(schema.getType());
    OpenAPI openAPI = new OpenAPI().openapi("3.1.0").components(new Components().addSchemas("stringTest", schema).addPathItem("/pathTest", new PathItem().description("test path item").get(new Operation().operationId("testPathItem").responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("response description"))))).addResponses("201", new ApiResponse().description("api response description")).addParameters("param", new Parameter().in("query").description("parameter description").schema(schema)).addExamples("example", new Example().summary("example summary").value("This is an example/").description("example description")).addRequestBodies("body", new RequestBody().content(new Content().addMediaType("application/json", new MediaType().schema(new ObjectSchema())))).addHeaders("test-head", new Header().description("test header description")).addSecuritySchemes("basic", new SecurityScheme().in(SecurityScheme.In.HEADER).scheme("http").description("ref security description")).addLinks("Link", new Link().operationRef("#/paths/~12.0~1repositories~1{username}/get")).addCallbacks("TestCallback", new Callback().addPathItem("{$request.query.queryUrl}", new PathItem().description("test path item").post(new Operation().operationId("testPathItem")))));
    SerializationMatchers.assertEqualsToYaml31(openAPI, "openapi: 3.1.0\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema: {}\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem\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" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : { }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    },\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" + "}");
    openAPI.openapi("3.0.3");
    SerializationMatchers.assertEqualsToYaml(openAPI, "openapi: 3.0.3\n" + "components:\n" + "  schemas:\n" + "    stringTest:\n" + "      type: string\n" + "  responses:\n" + "    \"201\":\n" + "      description: api response description\n" + "  parameters:\n" + "    param:\n" + "      in: query\n" + "      description: parameter description\n" + "      schema:\n" + "        type: string\n" + "  examples:\n" + "    example:\n" + "      summary: example summary\n" + "      description: example description\n" + "      value: This is an example/\n" + "  requestBodies:\n" + "    body:\n" + "      content:\n" + "        application/json:\n" + "          schema:\n" + "            type: object\n" + "  headers:\n" + "    test-head:\n" + "      description: test header description\n" + "  securitySchemes:\n" + "    basic:\n" + "      description: ref security description\n" + "      in: header\n" + "      scheme: http\n" + "  links:\n" + "    Link:\n" + "      operationRef: \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "  callbacks:\n" + "    TestCallback:\n" + "      '{$request.query.queryUrl}':\n" + "        description: test path item\n" + "        post:\n" + "          operationId: testPathItem");
    SerializationMatchers.assertEqualsToJson(openAPI, "{\n" + "  \"openapi\" : \"3.0.3\",\n" + "  \"components\" : {\n" + "    \"schemas\" : {\n" + "      \"stringTest\" : {\n" + "        \"type\" : \"string\"\n" + "      }\n" + "    },\n" + "    \"responses\" : {\n" + "      \"201\" : {\n" + "        \"description\" : \"api response description\"\n" + "      }\n" + "    },\n" + "    \"parameters\" : {\n" + "      \"param\" : {\n" + "        \"in\" : \"query\",\n" + "        \"description\" : \"parameter description\",\n" + "        \"schema\" : {\n" + "          \"type\" : \"string\"\n" + "        }\n" + "      }\n" + "    },\n" + "    \"examples\" : {\n" + "      \"example\" : {\n" + "        \"summary\" : \"example summary\",\n" + "        \"description\" : \"example description\",\n" + "        \"value\" : \"This is an example/\"\n" + "      }\n" + "    },\n" + "    \"requestBodies\" : {\n" + "      \"body\" : {\n" + "        \"content\" : {\n" + "          \"application/json\" : {\n" + "            \"schema\" : {\n" + "              \"type\" : \"object\"\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    },\n" + "    \"headers\" : {\n" + "      \"test-head\" : {\n" + "        \"description\" : \"test header description\"\n" + "      }\n" + "    },\n" + "    \"securitySchemes\" : {\n" + "      \"basic\" : {\n" + "        \"description\" : \"ref security description\",\n" + "        \"in\" : \"header\",\n" + "        \"scheme\" : \"http\"\n" + "      }\n" + "    },\n" + "    \"links\" : {\n" + "      \"Link\" : {\n" + "        \"operationRef\" : \"#/paths/~12.0~1repositories~1{username}/get\"\n" + "      }\n" + "    },\n" + "    \"callbacks\" : {\n" + "      \"TestCallback\" : {\n" + "        \"{$request.query.queryUrl}\" : {\n" + "          \"description\" : \"test path item\",\n" + "          \"post\" : {\n" + "            \"operationId\" : \"testPathItem\"\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}");
}
Also used : Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Header(io.swagger.v3.oas.models.headers.Header) Content(io.swagger.v3.oas.models.media.Content) Example(io.swagger.v3.oas.models.examples.Example) Parameter(io.swagger.v3.oas.models.parameters.Parameter) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses) Link(io.swagger.v3.oas.models.links.Link) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 18 with Callback

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

the class ReaderTest method testGetCallbacks.

@Test(description = "Callbacks")
public void testGetCallbacks() {
    Reader reader = new Reader(new OpenAPI());
    Method[] methods = SimpleCallbackResource.class.getMethods();
    Operation callbackOperation = reader.parseMethod(methods[0], null, null);
    assertNotNull(callbackOperation);
    Map<String, Callback> callbacks = callbackOperation.getCallbacks();
    assertNotNull(callbacks);
    Callback callback = callbacks.get(CALLBACK_SUBSCRIPTION_ID);
    assertNotNull(callback);
    PathItem pathItem = callback.get(CALLBACK_URL);
    assertNotNull(pathItem);
    Operation postOperation = pathItem.getPost();
    assertNotNull(postOperation);
    assertEquals(CALLBACK_POST_OPERATION_DESCRIPTION, postOperation.getDescription());
    Operation getOperation = pathItem.getGet();
    assertNotNull(getOperation);
    assertEquals(CALLBACK_GET_OPERATION_DESCRIPTION, getOperation.getDescription());
    Operation putOperation = pathItem.getPut();
    assertNotNull(putOperation);
    assertEquals(CALLBACK_POST_OPERATION_DESCRIPTION, putOperation.getDescription());
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Method(java.lang.reflect.Method) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 19 with Callback

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

the class SpecFilter method addPathItemSchemaRef.

private void addPathItemSchemaRef(PathItem pathItem, Set<String> referencedDefinitions) {
    if (pathItem.getParameters() != null) {
        for (Parameter parameter : pathItem.getParameters()) {
            addSchemaRef(parameter.getSchema(), referencedDefinitions);
            addContentSchemaRef(parameter.getContent(), referencedDefinitions);
        }
    }
    Map<PathItem.HttpMethod, Operation> ops = pathItem.readOperationsMap();
    for (Operation op : ops.values()) {
        if (op.getRequestBody() != null) {
            addContentSchemaRef(op.getRequestBody().getContent(), referencedDefinitions);
        }
        if (op.getResponses() != null) {
            for (String keyResponses : op.getResponses().keySet()) {
                ApiResponse response = op.getResponses().get(keyResponses);
                if (response.getHeaders() != null) {
                    for (String keyHeaders : response.getHeaders().keySet()) {
                        Header header = response.getHeaders().get(keyHeaders);
                        addSchemaRef(header.getSchema(), referencedDefinitions);
                        addContentSchemaRef(header.getContent(), referencedDefinitions);
                    }
                }
                addContentSchemaRef(response.getContent(), referencedDefinitions);
            }
        }
        if (op.getParameters() != null) {
            for (Parameter parameter : op.getParameters()) {
                addSchemaRef(parameter.getSchema(), referencedDefinitions);
                addContentSchemaRef(parameter.getContent(), referencedDefinitions);
            }
        }
        if (op.getCallbacks() != null) {
            for (String keyCallback : op.getCallbacks().keySet()) {
                Callback callback = op.getCallbacks().get(keyCallback);
                for (PathItem callbackPathItem : callback.values()) {
                    addPathItemSchemaRef(callbackPathItem, referencedDefinitions);
                }
            }
        }
    }
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) Callback(io.swagger.v3.oas.models.callbacks.Callback) Header(io.swagger.v3.oas.models.headers.Header) Parameter(io.swagger.v3.oas.models.parameters.Parameter) Operation(io.swagger.v3.oas.models.Operation) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse)

Example 20 with Callback

use of io.swagger.v3.oas.models.callbacks.Callback in project swagger-parser by swagger-api.

the class CallbackProcessor method processReferenceCallback.

public void processReferenceCallback(Callback callback) {
    String $ref = callback.get$ref();
    RefFormat refFormat = computeRefFormat($ref);
    if (isAnExternalRefFormat(refFormat)) {
        final String newRef = externalRefProcessor.processRefToExternalCallback($ref, refFormat);
        if (newRef != null) {
            callback.set$ref("#/components/callbacks/" + newRef);
        }
    }
}
Also used : RefFormat(io.swagger.v3.parser.models.RefFormat) RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat)

Aggregations

Callback (io.swagger.v3.oas.models.callbacks.Callback)17 Operation (io.swagger.v3.oas.models.Operation)12 PathItem (io.swagger.v3.oas.models.PathItem)12 OpenAPI (io.swagger.v3.oas.models.OpenAPI)11 Test (org.testng.annotations.Test)9 Parameter (io.swagger.v3.oas.models.parameters.Parameter)8 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)8 Components (io.swagger.v3.oas.models.Components)6 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)5 Schema (io.swagger.v3.oas.models.media.Schema)5 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)5 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 RefFormat (io.swagger.v3.parser.models.RefFormat)4 RefUtils.computeRefFormat (io.swagger.v3.parser.util.RefUtils.computeRefFormat)4 RefUtils.isAnExternalRefFormat (io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3