Java Code Examples for org.eclipse.microprofile.openapi.OASFilter

The following examples show how to use org.eclipse.microprofile.openapi.OASFilter. These examples are extracted from open source projects. 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 Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static List<Parameter> filterParameterList(OASFilter filter, List<Parameter> models) {
    if (models != null) {
        models = new ArrayList<>(models);
        ListIterator<Parameter> iterator = models.listIterator();
        while (iterator.hasNext()) {
            Parameter model = iterator.next();
            filterParameter(filter, model);

            if (filter.filterParameter(model) == null) {
                iterator.remove();
            }
        }
    }
    return models;
}
 
Example 2
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterSchema(OASFilter filter, Schema model) {
    if (model != null) {
        Schema ap = model.getAdditionalPropertiesSchema();
        if (ap != null) {
            filterSchema(filter, ap);
            if (filter.filterSchema(ap) == null) {
                model.setAdditionalPropertiesSchema((Schema) null);
            }
        }
        filterSchemaList(filter, model.getAllOf());
        filterSchemaList(filter, model.getAnyOf());
        filterSchema(filter, model.getItems());
        if (model.getItems() != null && filter.filterSchema(model.getItems()) == null) {
            model.setItems(null);
        }
        filterSchema(filter, model.getNot());
        if (model.getNot() != null && filter.filterSchema(model.getNot()) == null) {
            model.setNot(null);
        }
        filterSchemas(filter, model.getProperties());
    }
}
 
Example 3
Source Project: smallrye-open-api   Source File: FilterUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Test method for
 * {@link FilterUtil#applyFilter(org.eclipse.microprofile.openapi.OASFilter, org.eclipse.microprofile.openapi.models.OpenAPI)}.
 * 
 * @throws Exception
 */
@Test
public void testApplyFilter() throws Exception {
    URL beforeUrl = FilterUtilTest.class.getResource("filter-before.json");
    URL afterUrl = FilterUtilTest.class.getResource("filter-after.json");

    OpenAPI model = OpenApiParser.parse(beforeUrl);
    OASFilter filter = filter();

    model = FilterUtil.applyFilter(filter, model);

    String actual = OpenApiSerializer.serialize(model, Format.JSON);
    String expected = loadResource(afterUrl);

    assertJsonEquals(expected, actual);
}
 
Example 4
Source Project: smallrye-open-api   Source File: FilterUtilTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates and returns the filter to use for the test.
 */
private OASFilter filter() {
    return new OASFilter() {
        /**
         * @see org.eclipse.microprofile.openapi.OASFilter#filterOpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI)
         */
        @Override
        public void filterOpenAPI(OpenAPI openAPI) {
            openAPI.getInfo().setLicense(null);
            openAPI.getInfo().setTitle("Updated API Title");
        }

        /**
         * @see org.eclipse.microprofile.openapi.OASFilter#filterPathItem(org.eclipse.microprofile.openapi.models.PathItem)
         */
        @Override
        public PathItem filterPathItem(PathItem pathItem) {
            if (pathItem.getRef() != null) {
                return null;
            } else {
                return pathItem;
            }
        }
    };
}
 
Example 5
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Apply the given filter to the given model.
 * 
 * @param filter OASFilter
 * @param model OpenAPI model
 * @return Filtered OpenAPI model
 */
public static final OpenAPI applyFilter(OASFilter filter, OpenAPI model) {
    filterComponents(filter, model.getComponents());
    filterPaths(filter, model.getPaths());
    filterServers(filter, model.getServers());
    filterTags(filter, model.getTags());
    filter.filterOpenAPI(model);
    return model;
}
 
Example 6
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterComponents(OASFilter filter, Components model) {
    if (model != null) {
        filterCallbacks(filter, model.getCallbacks());
        filterHeaders(filter, model.getHeaders());
        filterLinks(filter, model.getLinks());
        filterParameters(filter, model.getParameters());
        filterRequestBodies(filter, model.getRequestBodies());
        filterAPIResponses(filter, model.getResponses());
        filterSchemas(filter, model.getSchemas());
        filterSecuritySchemes(filter, model.getSecuritySchemes());
    }
}
 
Example 7
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterCallbacks(OASFilter filter, Map<String, Callback> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            Callback model = models.get(key);
            filterCallback(filter, model);
            if (filter.filterCallback(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 8
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterCallback(OASFilter filter, Callback model) {
    if (model != null) {
        Collection<String> keys = new ArrayList<>(model.getPathItems().keySet());
        for (String key : keys) {
            PathItem childModel = model.getPathItem(key);
            filterPathItem(filter, childModel);

            if (filter.filterPathItem(childModel) == null) {
                model.removePathItem(key);
            }
        }
    }
}
 
Example 9
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterPathItem(OASFilter filter, PathItem model) {
    if (model != null) {
        model.setParameters(filterParameterList(filter, model.getParameters()));
        filterOperation(filter, model.getDELETE());
        if (model.getDELETE() != null) {
            model.setDELETE(filter.filterOperation(model.getDELETE()));
        }
        filterOperation(filter, model.getGET());
        if (model.getGET() != null) {
            model.setGET(filter.filterOperation(model.getGET()));
        }
        filterOperation(filter, model.getHEAD());
        if (model.getHEAD() != null) {
            model.setHEAD(filter.filterOperation(model.getHEAD()));
        }
        filterOperation(filter, model.getOPTIONS());
        if (model.getOPTIONS() != null) {
            model.setOPTIONS(filter.filterOperation(model.getOPTIONS()));
        }
        filterOperation(filter, model.getPATCH());
        if (model.getPATCH() != null) {
            model.setPATCH(filter.filterOperation(model.getPATCH()));
        }
        filterOperation(filter, model.getPOST());
        if (model.getPOST() != null) {
            model.setPOST(filter.filterOperation(model.getPOST()));
        }
        filterOperation(filter, model.getPUT());
        if (model.getPUT() != null) {
            model.setPUT(filter.filterOperation(model.getPUT()));
        }
        filterOperation(filter, model.getTRACE());
        if (model.getTRACE() != null) {
            model.setTRACE(filter.filterOperation(model.getTRACE()));
        }
        filterServers(filter, model.getServers());
    }
}
 
Example 10
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterOperation(OASFilter filter, Operation model) {
    if (model != null) {
        filterCallbacks(filter, model.getCallbacks());
        model.setParameters(filterParameterList(filter, model.getParameters()));
        filterRequestBody(filter, model.getRequestBody());
        if (model.getRequestBody() != null && filter.filterRequestBody(model.getRequestBody()) == null) {
            model.setRequestBody(null);
        }
        if (model.getResponses() != null) {
            filterAPIResponses(filter, model.getResponses().getAPIResponses());
        }
        filterServers(filter, model.getServers());
    }
}
 
Example 11
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterHeaders(OASFilter filter, Map<String, Header> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            Header model = models.get(key);
            filterHeader(filter, model);

            if (filter.filterHeader(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 12
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterHeader(OASFilter filter, Header model) {
    if (model != null) {
        filterContent(filter, model.getContent());
        filterSchema(filter, model.getSchema());
        if (model.getSchema() != null && filter.filterSchema(model.getSchema()) == null) {
            model.setSchema(null);
        }
    }
}
 
Example 13
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterContent(OASFilter filter, Content model) {
    if (model != null && model.getMediaTypes() != null) {
        Collection<String> keys = new ArrayList<>(model.getMediaTypes().keySet());
        for (String key : keys) {
            MediaType childModel = model.getMediaType(key);
            filterMediaType(filter, childModel);
        }
    }
}
 
Example 14
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterMediaType(OASFilter filter, MediaType model) {
    if (model != null) {
        filterEncoding(filter, model.getEncoding());
        filterSchema(filter, model.getSchema());
        if (model.getSchema() != null && filter.filterSchema(model.getSchema()) == null) {
            model.setSchema(null);
        }
    }
}
 
Example 15
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterEncoding(OASFilter filter, Map<String, Encoding> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            Encoding model = models.get(key);
            filterEncoding(filter, model);
        }
    }
}
 
Example 16
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterLinks(OASFilter filter, Map<String, Link> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            Link model = models.get(key);
            filterLink(filter, model);

            if (filter.filterLink(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 17
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterParameters(OASFilter filter, Map<String, Parameter> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            Parameter model = models.get(key);
            filterParameter(filter, model);

            if (filter.filterParameter(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 18
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterParameter(OASFilter filter, Parameter model) {
    if (model != null) {
        filterContent(filter, model.getContent());
        filterSchema(filter, model.getSchema());
        if (model.getSchema() != null && filter.filterSchema(model.getSchema()) == null) {
            model.setSchema(null);
        }
    }
}
 
Example 19
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterRequestBodies(OASFilter filter, Map<String, RequestBody> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            RequestBody model = models.get(key);
            filterRequestBody(filter, model);

            if (filter.filterRequestBody(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 20
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterAPIResponses(OASFilter filter, Map<String, APIResponse> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            APIResponse model = models.get(key);
            filterAPIResponse(filter, model);

            if (filter.filterAPIResponse(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 21
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterAPIResponse(OASFilter filter, APIResponse model) {
    if (model != null) {
        filterContent(filter, model.getContent());
        filterHeaders(filter, model.getHeaders());
        filterLinks(filter, model.getLinks());
    }
}
 
Example 22
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterSchemas(OASFilter filter, Map<String, Schema> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            Schema model = models.get(key);
            filterSchema(filter, model);

            if (filter.filterSchema(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 23
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterSchemaList(OASFilter filter, List<Schema> models) {
    if (models != null) {
        ListIterator<Schema> iterator = models.listIterator();
        while (iterator.hasNext()) {
            Schema model = iterator.next();
            filterSchema(filter, model);

            if (filter.filterSchema(model) == null) {
                iterator.remove();
            }
        }
    }
}
 
Example 24
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given models.
 * 
 * @param filter
 * @param models
 */
private static void filterSecuritySchemes(OASFilter filter, Map<String, SecurityScheme> models) {
    if (models != null) {
        Collection<String> keys = new ArrayList<>(models.keySet());
        for (String key : keys) {
            SecurityScheme model = models.get(key);
            if (filter.filterSecurityScheme(model) == null) {
                models.remove(key);
            }
        }
    }
}
 
Example 25
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param model
 */
private static void filterPaths(OASFilter filter, Paths model) {
    if (model != null) {
        Collection<String> keys = new ArrayList<>(model.getPathItems().keySet());
        for (String key : keys) {
            PathItem childModel = model.getPathItem(key);
            filterPathItem(filter, childModel);

            if (filter.filterPathItem(childModel) == null) {
                model.removePathItem(key);
            }
        }
    }
}
 
Example 26
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param models
 */
private static void filterServers(OASFilter filter, List<Server> models) {
    if (models != null) {
        ListIterator<Server> iterator = models.listIterator();
        while (iterator.hasNext()) {
            Server model = iterator.next();
            if (filter.filterServer(model) == null) {
                iterator.remove();
            }
        }
    }
}
 
Example 27
Source Project: smallrye-open-api   Source File: FilterUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filters the given model.
 * 
 * @param filter
 * @param models
 */
private static void filterTags(OASFilter filter, List<Tag> models) {
    if (models != null) {
        ListIterator<Tag> iterator = models.listIterator();
        while (iterator.hasNext()) {
            Tag model = iterator.next();
            model = filter.filterTag(model);
            if (model == null) {
                iterator.remove();
            }
        }
    }
}
 
Example 28
Source Project: smallrye-open-api   Source File: OpenApiProcessor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate the {@link OASFilter} configured by the app.
 * 
 * @param config OpenApiConfig
 * @param loader ClassLoader
 * @return OASFilter instance retrieved from loader
 */
public static OASFilter getFilter(OpenApiConfig config, ClassLoader loader) {
    String filterClassName = config.filter();
    if (filterClassName == null) {
        return null;
    }
    try {
        Class<?> c = loader.loadClass(filterClassName);
        return (OASFilter) c.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example 29
Source Project: thorntail   Source File: OpenApiServletContextListener.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate the {@link OASFilter} configured by the app.
 */
private OASFilter getFilter() {
    ClassLoader cl = getContextClassLoader();
    if (cl == null) {
        cl = OpenApiServletContextListener.class.getClassLoader();
    }
    return OpenApiProcessor.getFilter(config, cl);
}
 
Example 30
Source Project: tomee   Source File: TomEEOpenAPIExtension.java    License: Apache License 2.0 5 votes vote down vote up
private OpenAPI createOpenApi(final Class<?> application, final Stream<Class<?>> beans) {
    final CDI<Object> current = CDI.current();
    final OpenAPI api = ofNullable(config.read(OASConfig.MODEL_READER, null))
            .map(value -> newInstance(current, value))
            .map(it -> OASModelReader.class.cast(it).buildModel())
            .orElseGet(() -> current.select(DefaultLoader.class).get().loadDefaultApi());

    final BeanManager beanManager = current.getBeanManager();
    processor.processApplication(api, new ElementImpl(beanManager.createAnnotatedType(application)));
    if (skipScan) {
        return api.paths(new PathsImpl());
    }

    // adds the context path to the base
    final Instance<ServletContext> servletContextInstance = current.select(ServletContext.class);
    final boolean appendContextPath = Boolean.valueOf(config.read("application.append-context-path", "true"));
    String contextPath = "";
    if (appendContextPath && !servletContextInstance.isAmbiguous() && !servletContextInstance.isUnsatisfied()) {
        contextPath = servletContextInstance.get().getContextPath();
    }

    final String base = contextPath + processor.getApplicationBinding(application);
    processor.beforeProcessing();
    beans.filter(c -> (excludeClasses == null || !excludeClasses.contains(c.getName())))
            .filter(c -> (excludePackages == null || excludePackages.stream().noneMatch(it -> c.getName().startsWith(it))))
            .map(beanManager::createAnnotatedType)
            .forEach(at -> processor.processClass(
                    base, api, new ElementImpl(at), at.getMethods().stream().map(MethodElementImpl::new)));

    return ofNullable(config.read(OASConfig.FILTER, null))
            .map(it -> newInstance(current, it))
            .map(i -> new FilterImpl(OASFilter.class.cast(i)).filter(api))
            .orElse(api);
}