use of io.swagger.models.Response in project swagger-core by swagger-api.
the class ResponsesTest method applyResponsesTest.
@Test(dataProvider = "resourceWithAnnotations")
public void applyResponsesTest(String methodName, Response expected) throws NoSuchMethodException {
final Operation operation = new Operation();
final ReaderContext context = createDefaultContext();
extension.applyResponses(context, operation, findMethod(context, methodName));
if (expected == null) {
Assert.assertNull(operation.getResponses());
} else {
final Response response = operation.getResponses().get("200");
Assert.assertEquals(response.getDescription(), expected.getDescription());
}
}
use of io.swagger.models.Response in project swagger-core by swagger-api.
the class ResponsesTest method detailedResponsesTest.
@Test
public void detailedResponsesTest() throws NoSuchMethodException {
final Operation operation = new Operation();
final ReaderContext context = createDefaultContext();
extension.applyResponses(context, operation, findMethod(context, "testMethod3"));
final Map<String, Response> responses = operation.getResponses();
Assert.assertEquals(responses.size(), 7);
for (Map.Entry<String, String> entry : ResponsesTest.responses.entrySet()) {
Assert.assertEquals(responses.get(entry.getKey()).getDescription(), entry.getValue());
}
}
use of io.swagger.models.Response in project swagger-core by swagger-api.
the class Reader method parseMethod.
private Operation parseMethod(Class<?> cls, Method method, AnnotatedMethod annotatedMethod, List<Parameter> globalParameters, List<ApiResponse> classApiResponses) {
Operation operation = new Operation();
if (annotatedMethod != null) {
method = annotatedMethod.getAnnotated();
}
ApiOperation apiOperation = ReflectionUtils.getAnnotation(method, ApiOperation.class);
ApiResponses responseAnnotation = ReflectionUtils.getAnnotation(method, ApiResponses.class);
String operationId = null;
// check if it's an inherited or implemented method.
boolean methodInSuperType = false;
if (!cls.isInterface()) {
methodInSuperType = ReflectionUtils.findMethod(method, cls.getSuperclass()) != null;
}
if (!methodInSuperType) {
for (Class<?> implementedInterface : cls.getInterfaces()) {
methodInSuperType = ReflectionUtils.findMethod(method, implementedInterface) != null;
if (methodInSuperType) {
break;
}
}
}
if (!methodInSuperType) {
operationId = method.getName();
} else {
operationId = this.getOperationId(method.getName());
}
String responseContainer = null;
Type responseType = null;
Map<String, Property> defaultResponseHeaders = new LinkedHashMap<String, Property>();
if (apiOperation != null) {
if (apiOperation.hidden()) {
return null;
}
if (!apiOperation.nickname().isEmpty()) {
operationId = apiOperation.nickname();
}
defaultResponseHeaders = parseResponseHeaders(apiOperation.responseHeaders());
operation.summary(apiOperation.value()).description(apiOperation.notes());
if (!isVoid(apiOperation.response())) {
responseType = apiOperation.response();
}
if (!apiOperation.responseContainer().isEmpty()) {
responseContainer = apiOperation.responseContainer();
}
List<SecurityRequirement> securities = new ArrayList<SecurityRequirement>();
for (Authorization auth : apiOperation.authorizations()) {
if (!auth.value().isEmpty()) {
SecurityRequirement security = new SecurityRequirement();
security.setName(auth.value());
for (AuthorizationScope scope : auth.scopes()) {
if (!scope.scope().isEmpty()) {
security.addScope(scope.scope());
}
}
securities.add(security);
}
}
for (SecurityRequirement sec : securities) {
operation.security(sec);
}
if (!apiOperation.consumes().isEmpty()) {
String[] consumesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.consumes() });
for (String consume : consumesAr) {
operation.consumes(consume);
}
}
if (!apiOperation.produces().isEmpty()) {
String[] producesAr = ReaderUtils.splitContentValues(new String[] { apiOperation.produces() });
for (String produce : producesAr) {
operation.produces(produce);
}
}
}
if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.responseReference())) {
Response response = new Response().description(SUCCESSFUL_OPERATION);
response.schema(new RefProperty(apiOperation.responseReference()));
operation.addResponse(String.valueOf(apiOperation.code()), response);
} else if (responseType == null) {
// pick out response from method declaration
LOGGER.debug("picking up response class from method {}", method);
responseType = method.getGenericReturnType();
}
if (isValidResponse(responseType)) {
final Property property = ModelConverters.getInstance().readAsProperty(responseType);
if (property != null) {
final Property responseProperty = ContainerWrapper.wrapContainer(responseContainer, property);
final int responseCode = (apiOperation == null) ? 200 : apiOperation.code();
operation.response(responseCode, new Response().description(SUCCESSFUL_OPERATION).schema(responseProperty).headers(defaultResponseHeaders));
appendModels(responseType);
}
}
operation.operationId(operationId);
if (operation.getConsumes() == null || operation.getConsumes().isEmpty()) {
final Consumes consumes = ReflectionUtils.getAnnotation(method, Consumes.class);
if (consumes != null) {
for (String mediaType : ReaderUtils.splitContentValues(consumes.value())) {
operation.consumes(mediaType);
}
}
}
if (operation.getProduces() == null || operation.getProduces().isEmpty()) {
final Produces produces = ReflectionUtils.getAnnotation(method, Produces.class);
if (produces != null) {
for (String mediaType : ReaderUtils.splitContentValues(produces.value())) {
operation.produces(mediaType);
}
}
}
List<ApiResponse> apiResponses = new ArrayList<ApiResponse>();
if (responseAnnotation != null) {
apiResponses.addAll(Arrays.asList(responseAnnotation.value()));
}
Class<?>[] exceptionTypes = method.getExceptionTypes();
for (Class<?> exceptionType : exceptionTypes) {
ApiResponses exceptionResponses = ReflectionUtils.getAnnotation(exceptionType, ApiResponses.class);
if (exceptionResponses != null) {
apiResponses.addAll(Arrays.asList(exceptionResponses.value()));
}
}
for (ApiResponse apiResponse : apiResponses) {
addResponse(operation, apiResponse);
}
// merge class level @ApiResponse
for (ApiResponse apiResponse : classApiResponses) {
String key = (apiResponse.code() == 0) ? "default" : String.valueOf(apiResponse.code());
if (operation.getResponses() != null && operation.getResponses().containsKey(key)) {
continue;
}
addResponse(operation, apiResponse);
}
if (ReflectionUtils.getAnnotation(method, Deprecated.class) != null) {
operation.setDeprecated(true);
}
// process parameters
for (Parameter globalParameter : globalParameters) {
operation.parameter(globalParameter);
}
Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method);
if (annotatedMethod == null) {
Type[] genericParameterTypes = method.getGenericParameterTypes();
for (int i = 0; i < genericParameterTypes.length; i++) {
final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]));
for (Parameter parameter : parameters) {
operation.parameter(parameter);
}
}
} else {
for (int i = 0; i < annotatedMethod.getParameterCount(); i++) {
AnnotatedParameter param = annotatedMethod.getParameter(i);
final Type type = TypeFactory.defaultInstance().constructType(param.getParameterType(), cls);
List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]));
for (Parameter parameter : parameters) {
operation.parameter(parameter);
}
}
}
if (operation.getResponses() == null) {
Response response = new Response().description(SUCCESSFUL_OPERATION);
operation.defaultResponse(response);
}
processOperationDecorator(operation, method);
return operation;
}
use of io.swagger.models.Response in project swagger-core by swagger-api.
the class MapPropertyDeserializerTest method testMapDeserialization.
@Test(description = "it should deserialize a response per #1349")
public void testMapDeserialization() throws Exception {
Operation operation = Json.mapper().readValue(json, Operation.class);
Response response = operation.getResponses().get("200");
assertNotNull(response);
Property responseSchema = response.getSchema();
assertNotNull(responseSchema);
assertTrue(responseSchema instanceof MapProperty);
MapProperty mp = (MapProperty) responseSchema;
assertTrue(mp.getAdditionalProperties() instanceof IntegerProperty);
}
use of io.swagger.models.Response in project swagger-core by swagger-api.
the class MapPropertyDeserializerTest method testIssue1261InlineSchemaExample.
@Test(description = "it should read an example within an inlined schema")
public void testIssue1261InlineSchemaExample() throws Exception {
Operation operation = Yaml.mapper().readValue(" produces:\n" + " - application/json\n" + " responses:\n" + " 200:\n" + " description: OK\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int32\n" + " name:\n" + " type: string\n" + " required: [id, name]\n" + " example:\n" + " id: 42\n" + " name: Arthur Dent\n", Operation.class);
Response response = operation.getResponses().get("200");
assertNotNull(response);
Property schema = response.getSchema();
Object example = schema.getExample();
assertNotNull(example);
assertTrue(example instanceof ObjectNode);
ObjectNode objectNode = (ObjectNode) example;
assertEquals(objectNode.get("id").intValue(), 42);
assertEquals(objectNode.get("name").textValue(), "Arthur Dent");
}
Aggregations