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");
}
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;
}
Aggregations