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

The following examples show how to use io.swagger.models.Operation#getOperationId() . 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: SwaggerOperations.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public SwaggerOperations(Swagger swagger) {
  this.swagger = swagger;
  Map<String, Path> paths = swagger.getPaths();
  if (paths == null) {
    return;
  }

  for (Entry<String, Path> pathEntry : paths.entrySet()) {
    for (Entry<HttpMethod, Operation> operationEntry : pathEntry.getValue().getOperationMap().entrySet()) {
      Operation operation = operationEntry.getValue();
      if (StringUtils.isEmpty(operation.getOperationId())) {
        throw new IllegalStateException(String
            .format("OperationId can not be empty, path=%s, httpMethod=%s.",
                pathEntry.getKey(), operationEntry.getKey()));
      }

      SwaggerOperation swaggerOperation = new SwaggerOperation(swagger, pathEntry.getKey(), operationEntry.getKey(),
          operation);
      if (operations.putIfAbsent(operation.getOperationId(), swaggerOperation) != null) {
        throw new IllegalStateException(
            "please make sure operationId is unique, duplicated operationId is " + operation.getOperationId());
      }
    }
  }
}
 
Example 2
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 3
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 4
Source File: DefaultCodegen.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
/**
 * Get operationId from the operation object, and if it's blank, generate a new one from the given parameters.
 *
 * @param operation the operation object
 * @param path the path of the operation
 * @param httpMethod the HTTP method of the operation
 * @return the (generated) operationId
 */
protected String getOrGenerateOperationId(Operation operation, String path, String httpMethod) {
    String operationId = operation.getOperationId();
    if (StringUtils.isBlank(operationId)) {
        String tmpPath = path;
        tmpPath = tmpPath.replaceAll("\\{", "");
        tmpPath = tmpPath.replaceAll("\\}", "");
        String[] parts = (tmpPath + "/" + httpMethod).split("/");
        StringBuilder builder = new StringBuilder();
        if ("/".equals(tmpPath)) {
            // must be root tmpPath
            builder.append("root");
        }
        for (String part : parts) {
            if (part.length() > 0) {
                if (builder.toString().length() == 0) {
                    part = Character.toLowerCase(part.charAt(0)) + part.substring(1);
                } else {
                    part = initialCaps(part);
                }
                builder.append(part);
            }
        }
        operationId = sanitizeName(builder.toString());
        LOGGER.warn("Empty operationId found for path: " + httpMethod + " " + path + ". Renamed to auto-generated operationId: " + operationId);
    }
    return operationId;
}
 
Example 5
Source File: OperationIdServiceIdResolver.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
@Override
public String resolve(HttpMethod httpMethod, String pathname, Operation operation) {
    if (StringUtils.isBlank(operation.getOperationId())) {
        return super.resolve(httpMethod, pathname, operation);
    }

    return operation.getOperationId();
}
 
Example 6
Source File: ProtoApiFromOpenApi.java    From api-compiler with Apache License 2.0 5 votes vote down vote up
/** Validate if the operation id is correct and is unique. */
private boolean validateOperationId(
    Operation operation,
    String urlPath,
    String operationType,
    DiagCollector diagCollector,
    Map<String, String> duplicateOperationIdLookup) {
  if (Strings.isNullOrEmpty(operation.getOperationId())) {
    diagCollector.addDiag(
        Diag.error(
            OpenApiLocations.createOperationLocation(operationType, urlPath),
            "Operation does not have the required 'operationId' field. Please specify unique"
                + " value for 'operationId' field for all operations."));
    return false;
  }
  String operationId = operation.getOperationId();
  String sanitizedOperationId = NameConverter.operationIdToMethodName(operationId);
  if (duplicateOperationIdLookup.containsKey(sanitizedOperationId)) {
    String dupeOperationId = duplicateOperationIdLookup.get(sanitizedOperationId);
    Location errorLocation = OpenApiLocations.createOperationLocation(operationType, urlPath);
    String errorMessage = String.format("operationId '%s' has duplicate entry", operationId);
    if (!operationId.equals(dupeOperationId)) {
      errorLocation = SimpleLocation.TOPLEVEL;
      errorMessage +=
          String.format(
              ". Duplicate operationId found is '%s'. The two operationIds result into same "
                  + "underlying method name '%s'. Please use unique values for operationId",
              dupeOperationId, sanitizedOperationId);
    }
    diagCollector.addDiag(Diag.error(errorLocation, errorMessage));
    return false;
  }

  duplicateOperationIdLookup.put(sanitizedOperationId, operationId);
  return true;
}
 
Example 7
Source File: ImportController.java    From restfiddle with Apache License 2.0 4 votes vote down vote up
private void swaggerToRFConverter(String projectId, String name, MultipartFile file) throws IOException {
// MultipartFile file
File tempFile = File.createTempFile("RF_SWAGGER_IMPORT", "JSON");
file.transferTo(tempFile);
Swagger swagger = new SwaggerParser().read(tempFile.getAbsolutePath());

String host = swagger.getHost();
String basePath = swagger.getBasePath();

Info info = swagger.getInfo();
String title = info.getTitle();
String description = info.getDescription();

NodeDTO folderNode = createFolder(projectId, title);
folderNode.setDescription(description);

ConversationDTO conversationDTO;

Map<String, Path> paths = swagger.getPaths();
Set<String> keySet = paths.keySet();
for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
    String pathKey = iterator.next();
    Path path = paths.get(pathKey);

    Map<HttpMethod, Operation> operationMap = path.getOperationMap();
    Set<HttpMethod> operationsKeySet = operationMap.keySet();
    for (Iterator<HttpMethod> operIterator = operationsKeySet.iterator(); operIterator.hasNext();) {
	HttpMethod httpMethod = operIterator.next();
	Operation operation = operationMap.get(httpMethod);

	conversationDTO = new ConversationDTO();
	RfRequestDTO rfRequestDTO = new RfRequestDTO();
	rfRequestDTO.setApiUrl("http://" + host + basePath + pathKey);
	rfRequestDTO.setMethodType(httpMethod.name());
	operation.getParameters();
	conversationDTO.setRfRequestDTO(rfRequestDTO);
	ConversationDTO createdConversation = conversationController.create(conversationDTO);
	conversationDTO.setId(createdConversation.getId());

	String operationId = operation.getOperationId();
	String summary = operation.getSummary();
	// Request Node
	NodeDTO childNode = new NodeDTO();
	childNode.setName(operationId);
	childNode.setDescription(summary);
	childNode.setProjectId(projectId);
	childNode.setConversationDTO(conversationDTO);
	NodeDTO createdChildNode = nodeController.create(folderNode.getId(), childNode);
	System.out.println("created node : " + createdChildNode.getName());
    }
}
   }