Java Code Examples for org.eclipse.microprofile.openapi.models.PathItem#HttpMethod

The following examples show how to use org.eclipse.microprofile.openapi.models.PathItem#HttpMethod . 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: AnnotationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Set the created operation to the pathItem
 * 
 * @param methodType the HTTP method type
 * @param pathItem the pathItem to set
 * @param operation the operation
 */
default void setOperationOnPathItem(PathItem.HttpMethod methodType, PathItem pathItem, Operation operation) {
    switch (methodType) {
        case DELETE:
            pathItem.setDELETE(operation);
            break;
        case GET:
            pathItem.setGET(operation);
            break;
        case HEAD:
            pathItem.setHEAD(operation);
            break;
        case OPTIONS:
            pathItem.setOPTIONS(operation);
            break;
        case PATCH:
            pathItem.setPATCH(operation);
            break;
        case POST:
            pathItem.setPOST(operation);
            break;
        case PUT:
            pathItem.setPUT(operation);
            break;
        case TRACE:
            pathItem.setTRACE(operation);
            break;
        default:
            break;
    }
}
 
Example 2
Source File: OpenApiSpecStyleValidator.java    From openapi-style-validator with Apache License 2.0 5 votes vote down vote up
private void validateOperations() {
    for (String key : openAPI.getPaths().getPathItems().keySet()) {
        PathItem path = openAPI.getPaths().getPathItems().get(key);
        for (PathItem.HttpMethod method : path.getOperations().keySet()) {
            Operation op = path.getOperations().get(method);
            if (parameters.isValidateOperationOperationId()) {
                if (op.getOperationId() == null || op.getOperationId().isEmpty()) {
                    errorAggregator.logMissingOrEmptyOperationAttribute(key, method, "operationId");
                }
            }

            if (parameters.isValidateOperationDescription()) {
                if (op.getDescription() == null || op.getDescription().isEmpty()) {
                    errorAggregator.logMissingOrEmptyOperationAttribute(key, method, "description");
                }
            }

            if (parameters.isValidateOperationSummary()) {
                if (op.getSummary() == null || op.getSummary().isEmpty()) {
                    errorAggregator.logMissingOrEmptyOperationAttribute(key, method, "summary");
                }
            }

            if (parameters.isValidateOperationTag()) {
                if (op.getTags() == null || op.getTags().isEmpty()) {
                    errorAggregator.logMissingOrEmptyOperationCollection(key, method, "tags");
                }
            }
        }
    }
}
 
Example 3
Source File: OperationStyleError.java    From openapi-style-validator with Apache License 2.0 5 votes vote down vote up
public OperationStyleError(String fieldNames,
                           String description,
                           String path,
                           PathItem.HttpMethod method) {
    super(StyleCheckSection.Operations, fieldNames, description);
    this.path = path;
    this.method = method;

}
 
Example 4
Source File: JaxRsAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
/**
 * Process a single JAX-RS method to produce an OpenAPI Operation.
 * 
 * @param openApi
 * @param resourceClass
 * @param method
 * @param methodType
 * @param resourceTags
 * @param locatorPathParameters
 */
private void processResourceMethod(final AnnotationScannerContext context,
        final ClassInfo resourceClass,
        final MethodInfo method,
        final PathItem.HttpMethod methodType,
        OpenAPI openApi,
        Set<String> resourceTags,
        List<Parameter> locatorPathParameters,
        Map<DotName, AnnotationInstance> exceptionAnnotationMap) {

    JaxRsLogging.log.processingMethod(method.toString());

    // Figure out the current @Produces and @Consumes (if any)
    CurrentScannerInfo.setCurrentConsumes(getMediaTypes(method, JaxRsConstants.CONSUMES).orElse(null));
    CurrentScannerInfo.setCurrentProduces(getMediaTypes(method, JaxRsConstants.PRODUCES).orElse(null));

    // Process any @Operation annotation
    Optional<Operation> maybeOperation = processOperation(context, method);
    if (!maybeOperation.isPresent()) {
        return; // If the operation is marked as hidden, just bail here because we don't want it as part of the model.
    }
    final Operation operation = maybeOperation.get();

    // Process tags - @Tag and @Tags annotations combines with the resource tags we've already found (passed in)
    processOperationTags(method, openApi, resourceTags, operation);

    // Process @Parameter annotations.
    Function<AnnotationInstance, Parameter> reader = t -> ParameterReader.readParameter(context, t);

    ResourceParameters params = ParameterProcessor.process(context, resourceClass, method,
            reader, context.getExtensions());
    operation.setParameters(params.getOperationParameters());

    PathItem pathItem = new PathItemImpl();
    pathItem.setParameters(ListUtil.mergeNullableLists(locatorPathParameters, params.getPathItemParameters()));

    // Process any @RequestBody annotation (note: the @RequestBody annotation can be found on a method argument *or* on the method)
    RequestBody requestBody = processRequestBody(context, method, params);
    if (requestBody != null) {
        operation.setRequestBody(requestBody);
    }

    // Process @APIResponse annotations
    processResponse(context, method, operation, exceptionAnnotationMap);

    // Process @SecurityRequirement annotations
    processSecurityRequirementAnnotation(resourceClass, method, operation);

    // Process @Callback annotations
    processCallback(context, method, operation);

    // Process @Server annotations
    processServerAnnotation(method, operation);

    // Process @Extension annotations
    processExtensions(context, method, operation);

    // Process Security Roles
    JavaSecurityProcessor.processSecurityRoles(method, operation);

    // Now set the operation on the PathItem as appropriate based on the Http method type
    setOperationOnPathItem(methodType, pathItem, operation);

    // Figure out the path for the operation.  This is a combination of the App, Resource, and Method @Path annotations
    final String path;

    if (this.subResourceStack.isEmpty()) {
        path = super.makePath(params.getFullOperationPath());
    } else {
        // When processing a sub-resource tree, ignore any @Path information from the current class
        path = super.makePath(params.getOperationPath());
    }

    // Get or create a PathItem to hold the operation
    PathItem existingPath = ModelUtil.paths(openApi).getPathItem(path);

    if (existingPath == null) {
        ModelUtil.paths(openApi).addPathItem(path, pathItem);
    } else {
        // Changes applied to 'existingPath', no need to re-assign or add to OAI.
        MergeUtil.mergeObjects(existingPath, pathItem);
    }
}
 
Example 5
Source File: SpringAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
/**
 * Process a single Spring method to produce an OpenAPI Operation.
 *
 * @param openApi
 * @param resourceClass
 * @param method
 * @param methodType
 * @param resourceTags
 * @param locatorPathParameters
 */
private void processControllerMethod(final AnnotationScannerContext context,
        final ClassInfo resourceClass,
        final MethodInfo method,
        final PathItem.HttpMethod methodType,
        OpenAPI openApi,
        Set<String> resourceTags,
        List<Parameter> locatorPathParameters) {

    SpringLogging.log.processingMethod(method.toString());

    // Figure out the current @Produces and @Consumes (if any)
    CurrentScannerInfo.setCurrentConsumes(getMediaTypes(method, MediaTypeProperty.consumes).orElse(null));
    CurrentScannerInfo.setCurrentProduces(getMediaTypes(method, MediaTypeProperty.produces).orElse(null));

    // Process any @Operation annotation
    Optional<Operation> maybeOperation = processOperation(context, method);
    if (!maybeOperation.isPresent()) {
        return; // If the operation is marked as hidden, just bail here because we don't want it as part of the model.
    }
    final Operation operation = maybeOperation.get();

    // Process tags - @Tag and @Tags annotations combines with the resource tags we've already found (passed in)
    processOperationTags(method, openApi, resourceTags, operation);

    // Process @Parameter annotations.
    PathItem pathItem = new PathItemImpl();
    Function<AnnotationInstance, Parameter> reader = t -> ParameterReader.readParameter(context, t);
    ResourceParameters params = ParameterProcessor.process(context.getIndex(), resourceClass, method, reader,
            context.getExtensions());
    operation.setParameters(params.getOperationParameters());

    pathItem.setParameters(ListUtil.mergeNullableLists(locatorPathParameters, params.getPathItemParameters()));

    // Process any @RequestBody annotation (note: the @RequestBody annotation can be found on a method argument *or* on the method)
    RequestBody requestBody = processRequestBody(context, method, params);
    if (requestBody != null) {
        operation.setRequestBody(requestBody);
    }

    // Process @APIResponse annotations
    processResponse(context, method, operation, null);

    // Process @SecurityRequirement annotations
    processSecurityRequirementAnnotation(resourceClass, method, operation);

    // Process @Callback annotations
    processCallback(context, method, operation);

    // Process @Server annotations
    processServerAnnotation(method, operation);

    // Process @Extension annotations
    processExtensions(context, method, operation);

    // Process Security Roles
    JavaSecurityProcessor.processSecurityRoles(method, operation);

    // Now set the operation on the PathItem as appropriate based on the Http method type
    setOperationOnPathItem(methodType, pathItem, operation);

    // Figure out the path for the operation.  This is a combination of the App, Resource, and Method @Path annotations
    String path = super.makePath(params.getOperationPath());

    // Get or create a PathItem to hold the operation
    PathItem existingPath = ModelUtil.paths(openApi).getPathItem(path);

    if (existingPath == null) {
        ModelUtil.paths(openApi).addPathItem(path, pathItem);
    } else {
        // Changes applied to 'existingPath', no need to re-assign or add to OAI.
        MergeUtil.mergeObjects(existingPath, pathItem);
    }
}
 
Example 6
Source File: ErrorAggregator.java    From openapi-style-validator with Apache License 2.0 4 votes vote down vote up
void logMissingOrEmptyOperationAttribute(String path, PathItem.HttpMethod method, String field) {
    errorList.add(new OperationStyleError(field,
            "This field should be present and not empty",
            path, method));
}
 
Example 7
Source File: ErrorAggregator.java    From openapi-style-validator with Apache License 2.0 4 votes vote down vote up
void logMissingOrEmptyOperationCollection(String path, PathItem.HttpMethod method, String field) {
    errorList.add(new OperationStyleError(field,
            "The collection should be present and there should be at least one item in it",
            path, method));
}
 
Example 8
Source File: ErrorAggregator.java    From openapi-style-validator with Apache License 2.0 4 votes vote down vote up
void logOperationBadNaming(String variableName, String variableType, String neededNamingStrategy, String path, PathItem.HttpMethod httpMethod) {
    errorList.add(new OperationNamingStyleError(StyleError.StyleCheckSection.Naming, variableName,
            String.format("%s should be in %s", variableType, neededNamingStrategy), path, httpMethod));
}
 
Example 9
Source File: OperationNamingStyleError.java    From openapi-style-validator with Apache License 2.0 4 votes vote down vote up
public OperationNamingStyleError(StyleCheckSection styleCheckSection, String fieldNames, String description, String path, PathItem.HttpMethod method) {
    super(styleCheckSection, fieldNames, description);
    this.path = path;
    this.method = method;
}