Search in sources :

Example 1 with ResponseMessage

use of io.swagger.models.apideclaration.ResponseMessage in project swagger-parser by swagger-api.

the class OperationConverterTest method convertOperation1.

@Test
public void convertOperation1() throws Exception {
    io.swagger.models.apideclaration.Operation operation = new io.swagger.models.apideclaration.Operation();
    operation.setMethod(Method.GET);
    operation.setSummary("the summary");
    operation.setNotes("the notes");
    operation.setNickname("getFun");
    List<String> produces = new ArrayList<String>();
    produces.add("application/json");
    operation.setProduces(produces);
    // response type
    operation.setRef("Cat");
    // response messages
    List<ResponseMessage> responses = new ArrayList<ResponseMessage>();
    ResponseMessage message400 = new ResponseMessage();
    message400.setCode(400);
    message400.setMessage("got a 400");
    responses.add(message400);
    operation.setResponseMessages(responses);
    // parameters
    io.swagger.models.apideclaration.Parameter param = new io.swagger.models.apideclaration.Parameter();
    param.setParamType(ParamType.QUERY);
    param.setDescription("a string query param");
    param.setRequired(false);
    param.setAllowMultiple(false);
    param.setType("string");
    List<io.swagger.models.apideclaration.Parameter> parameters = new ArrayList<io.swagger.models.apideclaration.Parameter>();
    parameters.add(param);
    operation.setParameters(parameters);
    Operation converted = converter.convertOperation("tag", operation, new ApiDeclaration());
    assertTrue(converted.getTags().size() == 1);
    assertEquals(converted.getTags().get(0), "tag");
    assertEquals(operation.getSummary(), converted.getSummary());
    assertEquals(operation.getNotes(), converted.getDescription());
    assertEquals(operation.getNickname(), converted.getOperationId());
    assertTrue(converted.getProduces().size() == 1);
    assertEquals(converted.getProduces().get(0), "application/json");
    assertTrue(converted.getParameters().size() == 1);
    assertTrue(converted.getResponses().size() == 2);
    Response response = converted.getResponses().get("200");
    assertNotNull(response);
    assertEquals(response.getDescription(), "success");
    Model schema = response.getResponseSchema();
    assertNotNull(schema);
    assertTrue(schema.getClass().equals(RefModel.class));
    RefModel ref = (RefModel) schema;
    assertEquals(ref.getSimpleRef(), "Cat");
}
Also used : ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) RefModel(io.swagger.models.RefModel) Operation(io.swagger.models.Operation) ResponseMessage(io.swagger.models.apideclaration.ResponseMessage) Response(io.swagger.models.Response) RefModel(io.swagger.models.RefModel) Model(io.swagger.models.Model) Test(org.testng.annotations.Test)

Example 2 with ResponseMessage

use of io.swagger.models.apideclaration.ResponseMessage in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method convertOperation.

public Operation convertOperation(String tag, io.swagger.models.apideclaration.Operation operation, ApiDeclaration apiDeclaration) {
    Method method;
    if (operation.getMethod() == null) {
        JsonNode node = (JsonNode) operation.getExtraFields().get("httpMethod");
        method = Method.forValue(node.asText());
        operation.setMethod(method);
    }
    Operation output = new Operation().summary(operation.getSummary()).description(operation.getNotes()).operationId(operation.getNickname());
    if (tag != null) {
        output.tag(tag);
    }
    for (io.swagger.models.apideclaration.Parameter parameter : operation.getParameters()) {
        output.parameter(convertParameter(parameter));
    }
    if (operation.getConsumes() != null && !operation.getConsumes().isEmpty()) {
        for (String consumes : operation.getConsumes()) {
            output.consumes(consumes);
        }
    } else if (apiDeclaration.getConsumes() != null) {
        for (String consumes : apiDeclaration.getConsumes()) {
            output.consumes(consumes);
        }
    }
    if (operation.getProduces() != null && !operation.getProduces().isEmpty()) {
        for (String produces : operation.getProduces()) {
            output.produces(produces);
        }
    } else if (apiDeclaration.getProduces() != null) {
        for (String produces : apiDeclaration.getProduces()) {
            output.produces(produces);
        }
    }
    for (ResponseMessage message : operation.getResponseMessages()) {
        Response response = new Response().description(message.getMessage());
        Model responseModel = null;
        if (message.getResponseModel() != null) {
            response.schema(new RefProperty(message.getResponseModel()));
        }
        output.response(message.getCode(), response);
    }
    // default response type
    Property responseProperty = propertyFromTypedObject(operation);
    Response response = new Response().description("success").schema(responseProperty);
    if (output.getResponses() == null) {
        output.defaultResponse(response);
    } else if (responseProperty != null) {
        output.response(200, response);
    }
    Map<String, List<AuthorizationScope>> auths = operation.getAuthorizations();
    for (String securityName : auths.keySet()) {
        List<AuthorizationScope> scopes = auths.get(securityName);
        List<String> updatedScopes = new ArrayList<String>();
        for (AuthorizationScope s : scopes) {
            updatedScopes.add(s.getScope());
        }
        output.addSecurity(securityName, updatedScopes);
    }
    return output;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Method(io.swagger.models.Method) Operation(io.swagger.models.Operation) ResponseMessage(io.swagger.models.apideclaration.ResponseMessage) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) List(java.util.List) ArrayList(java.util.ArrayList) AuthorizationScope(io.swagger.models.AuthorizationScope) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) ModelProperty(io.swagger.models.apideclaration.ModelProperty) RefProperty(io.swagger.models.properties.RefProperty) UntypedProperty(io.swagger.models.properties.UntypedProperty)

Aggregations

Model (io.swagger.models.Model)2 Operation (io.swagger.models.Operation)2 RefModel (io.swagger.models.RefModel)2 Response (io.swagger.models.Response)2 ResponseMessage (io.swagger.models.apideclaration.ResponseMessage)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayModel (io.swagger.models.ArrayModel)1 AuthorizationScope (io.swagger.models.AuthorizationScope)1 Method (io.swagger.models.Method)1 ApiDeclaration (io.swagger.models.apideclaration.ApiDeclaration)1 ModelProperty (io.swagger.models.apideclaration.ModelProperty)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 Property (io.swagger.models.properties.Property)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 UntypedProperty (io.swagger.models.properties.UntypedProperty)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.testng.annotations.Test)1