Java Code Examples for io.swagger.models.Operation#getResponses()

The following examples show how to use io.swagger.models.Operation#getResponses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CppRestClientCodegen.java    From TypeScript-Microservices with MIT License 6 votes vote down vote up
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation,
        Map<String, Model> definitions, Swagger swagger) {
    CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);

    if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
        Response methodResponse = findMethodResponse(operation.getResponses());

        if (methodResponse != null) {
            if (methodResponse.getSchema() != null) {
                CodegenProperty cm = fromProperty("response", methodResponse.getSchema());
                op.vendorExtensions.put("x-codegen-response", cm);
                if(cm.datatype == "HttpContent")
                {
                    op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
                }
            }
        }
    }

    return op;
}
 
Example 2
Source File: PistacheServerCodegen.java    From TypeScript-Microservices with MIT License 6 votes vote down vote up
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation,
                                      Map<String, Model> definitions, Swagger swagger) {
    CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);

    if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
        Response methodResponse = findMethodResponse(operation.getResponses());

        if (methodResponse != null) {
            if (methodResponse.getSchema() != null) {
                CodegenProperty cm = fromProperty("response", methodResponse.getSchema());
                op.vendorExtensions.put("x-codegen-response", cm);
                if(cm.datatype == "HttpContent") {
                    op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
                }
            }
        }
    }

    String pathForPistache = path.replaceAll("\\{(.*?)}", ":$1");
    op.vendorExtensions.put("x-codegen-pistache-path", pathForPistache);

    return op;
}
 
Example 3
Source File: AbstractAdaCodegen.java    From TypeScript-Microservices with MIT License 6 votes vote down vote up
@Override
public CodegenOperation fromOperation(String path, String httpMethod, Operation operation,
                                      Map<String, Model> definitions, Swagger swagger) {
    CodegenOperation op = super.fromOperation(path, httpMethod, operation, definitions, swagger);

    if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
        Response methodResponse = findMethodResponse(operation.getResponses());

        if (methodResponse != null) {
            if (methodResponse.getSchema() != null) {
                CodegenProperty cm = fromProperty("response", methodResponse.getSchema());
                op.vendorExtensions.put("x-codegen-response", cm);
                if(cm.datatype == "HttpContent") {
                    op.vendorExtensions.put("x-codegen-response-ishttpcontent", true);
                }
            }
        }
    }
    return op;
}
 
Example 4
Source File: TestApiOperation.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
private void testBase(Path path) {
  Assert.assertEquals(1, path.getOperations().size());

  Operation operation = path.getGet();

  Assert.assertEquals("summary", operation.getSummary());
  Assert.assertEquals("notes", operation.getDescription());
  Assert.assertEquals(Arrays.asList("tag1", "tag2"), operation.getTags());
  Assert.assertEquals(Arrays.asList("application/json"), operation.getProduces());
  Assert.assertEquals(Arrays.asList("application/json"), operation.getConsumes());
  Assert.assertEquals(Arrays.asList(Scheme.HTTP, Scheme.HTTPS), operation.getSchemes());

  Map<String, Response> responseMap = operation.getResponses();
  Assert.assertEquals(2, responseMap.size());

  Response response = responseMap.get(SwaggerConst.SUCCESS_KEY);
  Assert.assertNotNull(response);
  Assert.assertEquals(null, response.getResponseSchema());

  response = responseMap.get("202");
  Assert.assertNotNull(response);
  Assert.assertEquals(null, response.getResponseSchema());

  Assert.assertEquals(1, response.getHeaders().size());
  Assert.assertEquals("integer", response.getHeaders().get("h1").getType());
}
 
Example 5
Source File: PlantUMLCodegen.java    From swagger2puml with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param operation
 * @return
 */
private String getErrorClassName(Operation operation) {
	StringBuilder errorClass = new StringBuilder();
	Map<String, Response> responses = operation.getResponses();
	for (Map.Entry<String, Response> responsesEntry : responses.entrySet()) {
		String responseCode = responsesEntry.getKey();

		if (responseCode.equalsIgnoreCase("default") || Integer.parseInt(responseCode) >= 300) {
			Property responseProperty = responsesEntry.getValue().getSchema();

			if (responseProperty instanceof RefProperty) {
				String errorClassName = ((RefProperty) responseProperty).getSimpleRef();
				if (!errorClass.toString().contains(errorClassName)) {
					if (StringUtils.isNotEmpty(errorClass)) {
						errorClass.append(",");
					}
					errorClass.append(errorClassName);
				}
			}
		}
	}

	return errorClass.toString();
}
 
Example 6
Source File: SwaggerInventory.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
public void process(Operation operation) {
    this.operations.add(operation);
    Iterator var2;
    if(operation.getParameters() != null) {
        var2 = operation.getParameters().iterator();

        while(var2.hasNext()) {
            Parameter key = (Parameter)var2.next();
            this.process(key);
        }
    }

    if(operation.getResponses() != null) {
        var2 = operation.getResponses().keySet().iterator();

        while(var2.hasNext()) {
            String key1 = (String)var2.next();
            Response response = (Response)operation.getResponses().get(key1);
            this.process(response);
        }
    }

}
 
Example 7
Source File: TestVendorExtension.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void decorateOperation(final Operation operation, final Method method, final Iterator<SwaggerExtension> chain) {

    final TestVendorAnnotation annotation = method.getAnnotation(TestVendorAnnotation.class);
    if (annotation != null) {

        Map<String, Response> map = new HashMap<String, Response>(operation.getResponses());
        final Response value = new Response();
        value.setDescription(RESPONSE_DESCRIPTION);
        map.put(RESPONSE_STATUS_401, value);
        operation.setResponses(map);
    }

    if (chain.hasNext()) {
        chain.next().decorateOperation(operation, method, chain);
    }
}
 
Example 8
Source File: VendorExtensionWithoutReader.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public void decorateOperation(final Operation operation, final Method method, final Iterator<SwaggerExtension> chain) {

    final TestVendorAnnotation annotation = method.getAnnotation(TestVendorAnnotation.class);
    if (annotation != null) {

        Map<String, Response> map = new HashMap<String, Response>(operation.getResponses());
        final Response value = new Response();
        value.setDescription(RESPONSE_DESCRIPTION);
        map.put(RESPONSE_STATUS_501, value);
        operation.setResponses(map);
    }

    if (chain.hasNext()) {
        chain.next().decorateOperation(operation, method, chain);
    }
}
 
Example 9
Source File: PlantUMLCodegen.java    From swagger2puml with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param operation
 * @return
 */
private String getInterfaceReturnType(Operation operation) {
	String returnType = "void";

	Map<String, Response> responses = operation.getResponses();
	for (Map.Entry<String, Response> responsesEntry : responses.entrySet()) {
		String responseCode = responsesEntry.getKey();

		if (!(responseCode.equalsIgnoreCase("default") || Integer.parseInt(responseCode) >= 300)) {
			Property responseProperty = responsesEntry.getValue().getSchema();

			if (responseProperty instanceof RefProperty) {
				returnType = ((RefProperty) responseProperty).getSimpleRef();
			} else if (responseProperty instanceof ArrayProperty) {
				Property arrayResponseProperty = ((ArrayProperty) responseProperty).getItems();
				if (arrayResponseProperty instanceof RefProperty) {
					returnType = new StringBuilder().append(((RefProperty) arrayResponseProperty).getSimpleRef())
							.append("[]").toString();
				}
			} else if (responseProperty instanceof ObjectProperty) {
				returnType = new StringBuilder().append(toTitleCase(operation.getOperationId())).append("Generated")
						.toString();
			}
		}
	}

	return returnType;
}
 
Example 10
Source File: SpecificationDiff.java    From swagger-diff with Apache License 2.0 5 votes vote down vote up
private static Property getResponseProperty(Operation operation) {
    Map<String, Response> responses = operation.getResponses();
    // temporary workaround for missing response messages
    if (responses == null) return null;
    Response response = responses.get("200");
    return null == response ? null : response.getSchema();
}
 
Example 11
Source File: AbstractOktaJavaClientCodegen.java    From okta-sdk-java with Apache License 2.0 5 votes vote down vote up
private Property getArrayPropertyFromOperation(Operation operation) {


        if (operation != null && operation.getResponses() != null) {
            Response response = operation.getResponses().get("200");
            if (response != null) {
                return response.getSchema();
            }
        }
        return null;
    }
 
Example 12
Source File: OpenApiSpec.java    From webtau with Apache License 2.0 4 votes vote down vote up
private static void addOperation(Map<OpenApiOperation, Set<String>> operationsAndResponses, String method, String fullUrl, Operation operation) {
    Set<String> responseCodes = operation.getResponses() == null ? Collections.emptySet() : operation.getResponses().keySet();
    operationsAndResponses.put(new OpenApiOperation(method, fullUrl), responseCodes);
}
 
Example 13
Source File: Reader.java    From jboot with Apache License 2.0 4 votes vote down vote up
private void read(ReaderContext context) {

        for (Method method : context.getCls().getDeclaredMethods()) {
            if (ReflectionUtils.isOverriddenMethod(method, context.getCls())) {
                continue;
            }
            final Operation operation = new Operation();


            final Type[] genericParameterTypes = method.getGenericParameterTypes();
            final Annotation[][] paramAnnotations = method.getParameterAnnotations();

            ControllerReaderExtension extension = new ControllerReaderExtension();

            String methodPath = "index".equals(method.getName()) ? "" : "/" + method.getName();
            String operationPath = JbootControllerManager.me().getPathByController((Class<? extends Controller>) context.getCls()) + methodPath;

            String httpMethod = extension.getHttpMethod(context, method);

            if (operationPath == null || httpMethod == null) {
                continue;
            }

            if (extension.isReadable(context)) {
                extension.setDeprecated(operation, method);
                extension.applyConsumes(context, operation, method);
                extension.applyProduces(context, operation, method);
                extension.applyOperationId(operation, method);
                extension.applySummary(operation, method);
                extension.applyDescription(operation, method);
                extension.applySchemes(context, operation, method);
                extension.applySecurityRequirements(context, operation, method);
                extension.applyTags(context, operation, method);
                extension.applyResponses(swagger, context, operation, method);
                extension.applyImplicitParameters(swagger, context, operation, method);
                extension.applyExtensions(context, operation, method);
                for (int i = 0; i < genericParameterTypes.length; i++) {
                    extension.applyParameters(httpMethod, context, operation, paramAnnotations[i]);
                }

                if ("post".equalsIgnoreCase(httpMethod) && operation.getConsumes() == null) {
                    operation.addConsumes("application/x-www-form-urlencoded");
                }
            }

            if (operation.getResponses() == null) {
                operation.defaultResponse(new Response().description("successful operation"));
            }

            final Map<String, String> regexMap = new HashMap<String, String>();
            final String parsedPath = PathUtils.parsePath(operationPath, regexMap);

            Path path = swagger.getPath(parsedPath);
            if (path == null) {
                path = new SwaggerPath();
                swagger.path(parsedPath, path);
            }
            path.set(httpMethod.toLowerCase(), operation);
        }
    }
 
Example 14
Source File: ProtoApiFromOpenApi.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@link TypeInfo} corresponding to the response object.
 *
 * <p>In case we cannot resolve the schema we fallback to the following well known types:
 * <li>'Value' type : If there is more than one success response codes or the success code schema
 *     represents a non message type.
 * <li>'List' type : If the response schema represents an array.
 * <li>'Empty' type : If there are no responses or there are no response for success code.
 */
private TypeInfo getResponseTypeInfo(Service.Builder serviceBuilder, Operation operation) {
  if (operation.getResponses() == null) {
    return WellKnownType.EMPTY.toTypeInfo();
  }

  int successCodeCount = 0;
  Response successResponse = null;
  for (String responseCode : operation.getResponses().keySet()) {
    Response response = operation.getResponses().get(responseCode);
    if (isSuccessCode(responseCode)) {
      successCodeCount++;
      if (response.getSchema() != null) {
        successResponse = response;
      }
    } else {
      // TODO (guptasu): Handle other cases like 4xx errors and non-RefProperty Schemas.
    }
  }

  if (successCodeCount == 1 && successResponse != null && successResponse.getSchema() != null) {
    TypeInfo responseTypeInfo =
        typeBuilder.ensureNamed(
            serviceBuilder,
            typeBuilder.getTypeInfo(serviceBuilder, successResponse.getSchema()),
            NameConverter.operationIdToResponseMessageName(operation.getOperationId()));
    if (responseTypeInfo.cardinality() == Cardinality.CARDINALITY_REPEATED) {
      // TODO (guptasu): Seems like we cannot create custom ListValue, something like
      // ListString. Therefore falling back to ListValue. Can we do better than this ?
      return WellKnownType.LIST.toTypeInfo();
    } else if (responseTypeInfo.kind() != Kind.TYPE_MESSAGE) {
      return WellKnownType.VALUE.toTypeInfo();
    } else {
      return responseTypeInfo;
    }
  } else if (successCodeCount == 0) {
    return WellKnownType.EMPTY.toTypeInfo();
  } else {
    // TODO (guptasu): Due to multiple schemas for successful response code  return type for the
    // operation is generalized as Value type.
    return WellKnownType.VALUE.toTypeInfo();
  }
}