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

The following examples show how to use io.swagger.models.Operation#setOperationId() . 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: OperationsTransformer.java    From spring-openapi with MIT License 6 votes vote down vote up
private Operation mapOperation(String operationName, HttpMethod httpMethod, Method method, String[] produces, String[] consumes, String controllerClassName) {
	Operation operation = new Operation();
	operation.setOperationId(getOperationId(operationName, method, httpMethod));
	operation.setSummary(StringUtils.isBlank(operationName) ? operationName : method.getName());
	operation.setTags(singletonList(classNameToTag(controllerClassName)));

	operation.setResponses(createApiResponses(method));
	operation.setParameters(transformParameters(method));
	if (isNotEmpty(consumes)) {
		operation.setConsumes(asList(consumes));
	}
	if (isNotEmpty(produces)) {
		operation.setProduces(asList(produces));
	}

	if (isHttpMethodWithRequestBody(httpMethod)) {
		io.swagger.models.parameters.Parameter requestBody = createRequestBody(method, getFirstFromArray(consumes));
		if (requestBody != null) {
			operation.getParameters().add(requestBody);
		}
	}

	applyAnnotationsForOperation(operation, method.getAnnotations());
	operationInterceptors.forEach(interceptor -> interceptor.intercept(method, operation));
	return operation;
}
 
Example 2
Source File: RestControllerProcessor.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Sets an operation Id on the operation based on the specified method. The operation Id takes on the format of
 * "~tagNameWithoutSpaces~.~methodName~[~counter~]".
 *
 * @param tagName the tag name for the class.
 * @param method the method for the operation.
 * @param operation the operation to set the Id on.
 */
private void setOperationId(String tagName, Method method, Operation operation)
{
    // Initialize the counter and the "base" operation Id (i.e. the one without the counter) and default the operation Id we're going to use to the base
    // one.
    int count = 0;
    String baseOperationId = tagName.replaceAll(" ", "") + "." + method.getName();
    String operationId = baseOperationId;

    // As long as the operation Id is a duplicate with one used before, add a counter to the end of it until we find one that hasn't been used before.
    while (operationIds.contains(operationId))
    {
        count++;
        operationId = baseOperationId + count;
    }

    // Add our new operation Id to the set so we don't use it again and set the operation Id on the operation itself.
    operationIds.add(operationId);
    operation.setOperationId(operationId);
}
 
Example 3
Source File: OperationsTransformer.java    From spring-openapi with MIT License 5 votes vote down vote up
private void handleOperation(Operation operation, Map<String, Integer> operationIdCount) {
	if (operation == null) {
		return;
	}
	String operationId = operation.getOperationId();
	if (operationIdCount.containsKey(operationId)) {
		Integer newValue = operationIdCount.get(operationId) + 1;
		operation.setOperationId(operationId + "_" + newValue);
		operationIdCount.put(operationId, newValue);
		return;
	}
	operationIdCount.put(operationId, 0);
}
 
Example 4
Source File: FlaskConnexionCodegen.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
@Override
public void preprocessSwagger(Swagger swagger) {
    // need vendor extensions for x-swagger-router-controller
    Map<String, Path> paths = swagger.getPaths();
    if(paths != null) {
        for(String pathname : paths.keySet()) {
            Path path = paths.get(pathname);
            Map<HttpMethod, Operation> operationMap = path.getOperationMap();
            if(operationMap != null) {
                for(HttpMethod method : operationMap.keySet()) {
                    Operation operation = operationMap.get(method);
                    String tag = "default";
                    if(operation.getTags() != null && operation.getTags().size() > 0) {
                        tag = operation.getTags().get(0);
                    }
                    String operationId = operation.getOperationId();
                    if(operationId == null) {
                        operationId = getOrGenerateOperationId(operation, pathname, method.toString());
                    }
                    operation.setOperationId(toOperationId(operationId));
                    if(operation.getVendorExtensions().get("x-swagger-router-controller") == null) {
                        operation.getVendorExtensions().put(
                                "x-swagger-router-controller",
                                controllerPackage + "." + toApiFilename(tag)
                        );
                    }
                    for (Parameter param: operation.getParameters()) {
                        // sanitize the param name but don't underscore it since it's used for request mapping
                        String name = param.getName();
                        String paramName = sanitizeName(name);
                        if (!paramName.equals(name)) {
                            LOGGER.warn(name + " cannot be used as parameter name with flask-connexion and was sanitized as " + paramName);
                        }
                        param.setName(paramName);
                    }
                }
            }
        }
    }
}
 
Example 5
Source File: ApiOperationProcessor.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void process(SwaggerGenerator swaggerGenerator,
    OperationGenerator operationGenerator, ApiOperation apiOperationAnnotation) {
  Operation operation = operationGenerator.getOperation();

  operationGenerator.setHttpMethod(apiOperationAnnotation.httpMethod());

  if (!StringUtils.isEmpty(apiOperationAnnotation.value())) {
    operation.setSummary(apiOperationAnnotation.value());
  }

  if (!StringUtils.isEmpty(apiOperationAnnotation.notes())) {
    operation.setDescription(apiOperationAnnotation.notes());
  }

  operation.setOperationId(apiOperationAnnotation.nickname());
  operation.getVendorExtensions().putAll(BaseReaderUtils.parseExtensions(apiOperationAnnotation.extensions()));

  convertTags(apiOperationAnnotation.tags(), operation);
  SwaggerUtils.setCommaConsumes(operation, apiOperationAnnotation.consumes());
  SwaggerUtils.setCommaProduces(operation, apiOperationAnnotation.produces());
  convertProtocols(apiOperationAnnotation.protocols(), operation);
  AnnotationUtils.addResponse(swaggerGenerator.getSwagger(),
      operation,
      apiOperationAnnotation);

  // responseReference未解析
  // hidden未解析
  // authorizations未解析
}
 
Example 6
Source File: ProtoApiFromOpenApi.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
private Operation constructReservedOperation(String suffix) {
  Operation getOperation = new Operation();
  getOperation.setOperationId(
      String.format("Google_Autogenerated_Unrecognized_%s_Method_Call", suffix));

  // Clear all the control plane settings that do not apply to the wild card operations.
  getOperation.setSecurity(new ArrayList<Map<String, List<String>>>());

  return getOperation;
}