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

The following examples show how to use io.swagger.v3.oas.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: 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 2
Source File: OpenapiImporterV30.java    From milkman with MIT License 5 votes vote down vote up
private RequestContainer toRequests(String host, HttpMethod method, String path, Operation operation) {

        String qryStr = "";
        if (operation.getParameters() != null){
            qryStr = operation.getParameters().stream()
                    .filter(p -> p.getIn().equals("query") && p.getRequired())
                    .map(p -> p.getName() + "=")
                    .collect(Collectors.joining("&", "?", ""));
            //if it only contains prefix
            if (qryStr.length() == 1){
                qryStr = "";
            }
        }


        RestRequestContainer container = new RestRequestContainer(operation.getOperationId(), host + path + qryStr, method.name());



        RestHeaderAspect headers = new RestHeaderAspect();
        container.addAspect(headers);

        if (operation.getParameters() != null) {
            operation.getParameters().stream()
                    .filter(p -> p.getIn().equals("header") && p.getRequired())
                    .map(p -> new HeaderEntry(UUID.randomUUID().toString(), p.getName(), "", true))
                    .forEach(headers.getEntries()::add);
        }

        RestBodyAspect body = new RestBodyAspect();
        container.addAspect(body);
        container.setInStorage(true);
        return container;
    }
 
Example 3
Source File: ReflectionUtils.java    From swagger-inflector with Apache License 2.0 5 votes vote down vote up
public String getMethodName(String path, String httpMethod, Operation operation) {
    String output = operation.getOperationId();
    if (output != null) {
        return sanitizeToJava(output);
    }
    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 (int i = 0; i < parts.length; i++) {
        String part = parts[i];
        if (part.length() > 0) {
            if (builder.toString().length() == 0) {
                part = Character.toLowerCase(part.charAt(0)) + part.substring(1);
            } else {
                part = StringUtils.capitalize(part);
            }
            builder.append(part);
        }
    }
    output = builder.toString();
    LOGGER.warn("generated operationId " + output);
    return output;
}
 
Example 4
Source File: NodeJSExpressServerCodegen.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
    public void preprocessOpenAPI(OpenAPI openAPI) {
        URL url = URLPathUtils.getServerURL(openAPI, serverVariableOverrides());
        String host = URLPathUtils.getProtocolAndHost(url);
        String port = URLPathUtils.getPort(url, defaultServerPort);
        String basePath = url.getPath();

        if (additionalProperties.containsKey(SERVER_PORT)) {
            port = additionalProperties.get(SERVER_PORT).toString();
        }
        this.additionalProperties.put(SERVER_PORT, port);

        if (openAPI.getInfo() != null) {
            Info info = openAPI.getInfo();
            if (info.getTitle() != null) {
                // when info.title is defined, use it for projectName
                // used in package.json
                projectName = info.getTitle()
                        .replaceAll("[^a-zA-Z0-9]", "-")
                        .replaceAll("^[-]*", "")
                        .replaceAll("[-]*$", "")
                        .replaceAll("[-]{2,}", "-")
                        .toLowerCase(Locale.ROOT);
                this.additionalProperties.put("projectName", projectName);
            }
        }

        // need vendor extensions
        Paths paths = openAPI.getPaths();
        if (paths != null) {
            for (String pathname : paths.keySet()) {
                PathItem path = paths.get(pathname);
                Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
                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 = toApiName(operation.getTags().get(0));
                        }
                        if (operation.getOperationId() == null) {
                            operation.setOperationId(getOrGenerateOperationId(operation, pathname, method.toString()));
                        }
                        // add x-openapi-router-controller
//                        if (operation.getExtensions() == null ||
//                                operation.getExtensions().get("x-openapi-router-controller") == null) {
//                            operation.addExtension("x-openapi-router-controller", sanitizeTag(tag) + "Controller");
//                        }
//                        // add x-openapi-router-service
//                        if (operation.getExtensions() == null ||
//                                operation.getExtensions().get("x-openapi-router-service") == null) {
//                            operation.addExtension("x-openapi-router-service", sanitizeTag(tag) + "Service");
//                        }
                        if (operation.getExtensions() == null ||
                                operation.getExtensions().get("x-eov-operation-handler") == null) {
                            operation.addExtension("x-eov-operation-handler", "controllers/" + sanitizeTag(tag) + "Controller");
                        }
                    }
                }
            }
        }

    }
 
Example 5
Source File: BallerinaOperation.java    From product-microgateway with Apache License 2.0 4 votes vote down vote up
@Override
public BallerinaOperation buildContext(Operation operation, ExtendedAPI api) throws BallerinaServiceGenException,
        CLICompileTimeException {
    if (operation == null) {
        return getDefaultValue();
    }

    // OperationId with spaces with special characters will cause errors in ballerina code.
    // Replacing it with uuid so that we can identify there was a ' ' when doing bal -> swagger
    operation.setOperationId(UUID.randomUUID().toString().replaceAll("-", ""));
    this.operationId = operation.getOperationId();
    this.tags = operation.getTags();
    this.summary = operation.getSummary();
    this.description = operation.getDescription();
    this.externalDocs = operation.getExternalDocs();
    this.parameters = new ArrayList<>();
    //to provide resource level security in dev-first approach
    ApplicationSecurity appSecurity = OpenAPICodegenUtils.populateApplicationSecurity(api.getName(),
            api.getVersion(), operation.getExtensions(), api.getMutualSSL());
    this.authProviders = OpenAPICodegenUtils.getMgwResourceSecurity(operation, appSecurity);
    this.apiKeys = OpenAPICodegenUtils.generateAPIKeysFromSecurity(operation.getSecurity(),
            this.authProviders.contains(OpenAPIConstants.APISecurity.apikey.name()));
    ApplicationSecurity apiAppSecurity = api.getApplicationSecurity();
    if (appSecurity != null && appSecurity.isOptional() != null) {
        // if app security is made optional at resource
        this.applicationSecurityOptional = appSecurity.isOptional();
    } else if (apiAppSecurity != null && apiAppSecurity.isOptional() != null) {
        // if app security made optional at API level
        this.applicationSecurityOptional = apiAppSecurity.isOptional();
    }
    //to set resource level scopes in dev-first approach
    this.scope = OpenAPICodegenUtils.getMgwResourceScope(operation);
    //set resource level endpoint configuration
    setEpConfigDTO(operation);
    Map<String, Object> exts = operation.getExtensions();

    if (exts != null) {
        // set interceptor details
        Object reqExt = exts.get(OpenAPIConstants.REQUEST_INTERCEPTOR);
        Object resExt = exts.get(OpenAPIConstants.RESPONSE_INTERCEPTOR);
        if (reqExt != null) {
            reqInterceptorContext = new BallerinaInterceptor(reqExt.toString());
            requestInterceptor = reqInterceptorContext.getInvokeStatement();
            isJavaRequestInterceptor = BallerinaInterceptor.Type.JAVA == reqInterceptorContext.getType();
        }
        if (resExt != null) {
            resInterceptorContext = new BallerinaInterceptor(resExt.toString());
            responseInterceptor = resInterceptorContext.getInvokeStatement();
            isJavaResponseInterceptor = BallerinaInterceptor.Type.JAVA == resInterceptorContext.getType();
        }


        Optional<Object> scopes = Optional.ofNullable(exts.get(X_SCOPE));
        scopes.ifPresent(value -> this.scope = "\"" + value.toString() + "\"");
        Optional<Object> authType = Optional.ofNullable(exts.get(X_AUTH_TYPE));
        authType.ifPresent(value -> {
            if (AUTH_TYPE_NONE.equals(value)) {
                this.isSecured = false;
            }
        });
        Optional<Object> resourceTier = Optional.ofNullable(exts.get(X_THROTTLING_TIER));
        resourceTier.ifPresent(value -> this.resourceTier = value.toString());
        //set dev-first resource level throttle policy
        if (this.resourceTier == null) {
            Optional<Object> extResourceTier = Optional.ofNullable(exts.get(OpenAPIConstants.THROTTLING_TIER));
            extResourceTier.ifPresent(value -> this.resourceTier = value.toString());
        }
        if (api.getApiLevelPolicy() != null && this.resourceTier != null) {
            //if api level policy exists then we are neglecting the resource level policies
            String message = "[WARN] : Resource level policy: " + this.resourceTier
                    + " will be neglected due to the presence of API level policy: " + api.getApiLevelPolicy()
                    + " for the API : " + api.getName() + "\n";
            CmdUtils.appendMessagesToConsole(message);
            this.resourceTier = null;
        }
        Optional<Object> extDisableSecurity = Optional.ofNullable(exts.get(OpenAPIConstants.DISABLE_SECURITY));
        extDisableSecurity.ifPresent(value -> {
            try {
                this.isSecured = !(Boolean) value;
                this.isSecuredAssignedFromOperation = true;
            } catch (ClassCastException e) {
                throw new CLIRuntimeException("The property '" + OpenAPIConstants.DISABLE_SECURITY +
                        "' should be a boolean value. But provided '" + value.toString() + "'.");
            }
        });
    }

    if (operation.getParameters() != null) {
        for (Parameter parameter : operation.getParameters()) {
            this.parameters.add(new BallerinaParameter().buildContext(parameter, api));
        }
    }

    return this;
}
 
Example 6
Source File: OASMergeUtil.java    From crnk-framework with Apache License 2.0 4 votes vote down vote up
public static Operation mergeOperations(Operation thisOperation, Operation thatOperation) {
  if (thatOperation == null) {
    return thisOperation;
  }

  if (thatOperation.getTags() != null) {
    thisOperation.setTags(
        mergeTags(thisOperation.getTags(), thatOperation.getTags())
    );
  }
  if (thatOperation.getExternalDocs() != null) {
    thisOperation.setExternalDocs(
      mergeExternalDocumentation(thisOperation.getExternalDocs(), thatOperation.getExternalDocs())
    );
  }
  if (thatOperation.getParameters() != null) {
    thisOperation.setParameters(
        mergeParameters(thisOperation.getParameters(), thatOperation.getParameters())
    );
  }
  if (thatOperation.getRequestBody() != null) {
    thisOperation.setRequestBody(thatOperation.getRequestBody());
  }
  if (thatOperation.getResponses() != null) {
    thisOperation.setResponses(thatOperation.getResponses());
  }
  if (thatOperation.getCallbacks() != null) {
    thisOperation.setCallbacks(thatOperation.getCallbacks());
  }
  if (thatOperation.getDeprecated() != null) {
    thisOperation.setDeprecated(thatOperation.getDeprecated());
  }
  if (thatOperation.getSecurity() != null) {
    thisOperation.setSecurity(thatOperation.getSecurity());
  }
  if (thatOperation.getServers() != null) {
    thisOperation.setServers(thatOperation.getServers());
  }
  if (thatOperation.getExtensions() != null) {
    thisOperation.setExtensions(thatOperation.getExtensions());
  }
  if (thatOperation.getOperationId() != null) {
    thisOperation.setOperationId(thatOperation.getOperationId());
  }
  if (thatOperation.getSummary() != null) {
    thisOperation.setSummary(thatOperation.getSummary());
  }
  if (thatOperation.getDescription() != null) {
    thisOperation.setDescription(thatOperation.getDescription());
  }
  if (thatOperation.getExtensions() != null) {
    thisOperation.setExtensions(thatOperation.getExtensions());
  }
  return thisOperation;
}