Search in sources :

Example 36 with Response

use of io.swagger.models.Response in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method convertResourceToOperation.

/**
 * This method will convert ballerina @Resource to ballerina @OperationAdaptor.
 *
 * @param resource Resource array to be convert.
 * @return @OperationAdaptor of string and swagger path objects.
 */
private OperationAdaptor convertResourceToOperation(ResourceNode resource, String httpMethod) {
    OperationAdaptor op = new OperationAdaptor();
    if (resource != null) {
        // Setting default values.
        op.setHttpOperation(HttpConstants.HTTP_METHOD_GET);
        op.setPath('/' + resource.getName().getValue());
        Response response = new Response().description("Successful").example(MediaType.APPLICATION_JSON, "Ok");
        op.getOperation().response(200, response);
        op.getOperation().setOperationId(resource.getName().getValue());
        op.getOperation().setParameters(null);
        // Parsing annotations.
        this.parseHttpResourceConfig(resource, op);
        if (null != httpMethod) {
            op.setHttpOperation(httpMethod);
        }
        this.parseResourceConfigAnnotationAttachment(resource, op.getOperation());
        this.parseResourceInfoAnnotationAttachment(resource, op.getOperation());
        this.addResourceParameters(resource, op);
        this.parseParametersInfoAnnotationAttachment(resource, op.getOperation());
        this.parseResponsesAnnotationAttachment(resource, op.getOperation());
    }
    return op;
}
Also used : Response(io.swagger.models.Response)

Example 37 with Response

use of io.swagger.models.Response in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method convertResourceToOperation.

/**
 * This method will convert ballerina @Resource to ballerina @OperationAdaptor
 *
 * @param resource Resource array to be convert.
 * @return @OperationAdaptor of string and swagger path objects.
 */
private OperationAdaptor convertResourceToOperation(ResourceInfo resource) {
    OperationAdaptor op = new OperationAdaptor();
    if (resource != null) {
        // Setting default values.
        op.setHttpOperation(HttpConstants.HTTP_METHOD_GET);
        op.setPath('/' + resource.getName());
        Response response = new Response().description("Successful").example("application/json", "Ok");
        op.getOperation().response(200, response);
        op.getOperation().setOperationId(resource.getName());
        op.getOperation().setParameters(null);
        // Parsing annotations.
        this.parsePathAnnotationAttachment(resource, op);
        this.parseHttpMethodAnnotationAttachment(resource, op);
        this.parseResourceConfigAnnotationAttachment(resource, op.getOperation());
        this.parseConsumesAnnotationAttachment(resource, op.getOperation());
        this.parseProducesAnnotationAttachment(resource, op.getOperation());
        this.parseResourceInfoAnnotationAttachment(resource, op.getOperation());
        this.addResourceParameters(resource, op);
        this.parseParametersInfoAnnotationAttachment(resource, op.getOperation());
        this.parseResponsesAnnotationAttachment(resource, op.getOperation());
    }
    return op;
}
Also used : Response(io.swagger.models.Response)

Example 38 with Response

use of io.swagger.models.Response in project ballerina by ballerina-lang.

the class SwaggerResourceMapper method parseResponsesAnnotationAttachment.

/**
 * Parses the 'Responses' annotation attachment and build swagger operation.
 *
 * @param resource The ballerina resource definition.
 * @param op       The swagger operation.
 */
private void parseResponsesAnnotationAttachment(ResourceInfo resource, Operation op) {
    AnnAttachmentInfo responsesAnnotationAttachment = resource.getAnnotationAttachmentInfo(SwaggerConstants.SWAGGER_PACKAGE_PATH, "Responses");
    if (null != responsesAnnotationAttachment) {
        Map<String, AnnAttributeValue> responsesAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(responsesAnnotationAttachment);
        if (null != responsesAnnAttributeValueMap.get("value")) {
            AnnAttributeValue[] responsesValues = responsesAnnAttributeValueMap.get("value").getAttributeValueArray();
            if (responsesValues.length > 0) {
                Map<String, Response> responses = new HashMap<>();
                for (AnnAttributeValue responsesValue : responsesValues) {
                    AnnAttachmentInfo responseAnnotationAttachment = responsesValue.getAnnotationAttachmentValue();
                    Map<String, AnnAttributeValue> responseAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(responseAnnotationAttachment);
                    if (null != responseAnnAttributeValueMap.get("code")) {
                        String code = responseAnnAttributeValueMap.get("code").getStringValue();
                        Response response = new Response();
                        if (null != responseAnnAttributeValueMap.get("description")) {
                            response.setDescription(responseAnnAttributeValueMap.get("description").getStringValue());
                        }
                        // TODO: Parse 'response' attribute for $.paths./resource-path.responses[*]["code"].schema
                        this.createHeadersModel(responseAnnAttributeValueMap.get("headers"), response);
                        responses.put(code, response);
                    }
                }
                op.setResponses(responses);
            }
        }
    }
}
Also used : Response(io.swagger.models.Response) ParamAnnAttachmentInfo(org.ballerinalang.util.codegen.ParamAnnAttachmentInfo) AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) HashMap(java.util.HashMap) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue)

Example 39 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class TestSwaggerUtils method correctResponsesOperationNotChangeExistDescription.

@Test
public void correctResponsesOperationNotChangeExistDescription() {
    Response response = new Response();
    response.setDescription("description");
    Operation operation = new Operation();
    operation.addResponse("200", response);
    SwaggerUtils.correctResponses(operation);
    Assert.assertEquals("description", response.getDescription());
}
Also used : Response(io.swagger.models.Response) Operation(io.swagger.models.Operation) Test(org.junit.Test)

Example 40 with Response

use of io.swagger.models.Response in project java-chassis by ServiceComb.

the class TestSwaggerUtils method correctResponsesOperationFixEmptyDescription.

@Test
public void correctResponsesOperationFixEmptyDescription() {
    Response response = new Response();
    Operation operation = new Operation();
    operation.addResponse("200", response);
    SwaggerUtils.correctResponses(operation);
    Assert.assertEquals("response of 200", response.getDescription());
}
Also used : Response(io.swagger.models.Response) Operation(io.swagger.models.Operation) Test(org.junit.Test)

Aggregations

Response (io.swagger.models.Response)97 Operation (io.swagger.models.Operation)53 Property (io.swagger.models.properties.Property)31 Path (io.swagger.models.Path)30 Swagger (io.swagger.models.Swagger)30 ApiResponse (io.swagger.annotations.ApiResponse)24 Test (org.testng.annotations.Test)23 RefProperty (io.swagger.models.properties.RefProperty)22 ArrayProperty (io.swagger.models.properties.ArrayProperty)21 Test (org.junit.Test)17 BodyParameter (io.swagger.models.parameters.BodyParameter)15 Parameter (io.swagger.models.parameters.Parameter)14 MapProperty (io.swagger.models.properties.MapProperty)14 HashMap (java.util.HashMap)13 Model (io.swagger.models.Model)12 StringProperty (io.swagger.models.properties.StringProperty)12 RefModel (io.swagger.models.RefModel)11 PathParameter (io.swagger.models.parameters.PathParameter)11 QueryParameter (io.swagger.models.parameters.QueryParameter)10 IntegerProperty (io.swagger.models.properties.IntegerProperty)10