org.eclipse.microprofile.openapi.OASModelReader Java Examples

The following examples show how to use org.eclipse.microprofile.openapi.OASModelReader. 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: OpenApiProcessor.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Instantiate the configured {@link OASModelReader} and invoke it. If no reader is configured,
 * then return null. If a class is configured but there is an error either instantiating or invoking
 * it, a {@link RuntimeException} is thrown.
 * 
 * @param config OpenApiConfig
 * @param loader ClassLoader
 * @return OpenApiImpl created from OASModelReader
 */
public static OpenAPI modelFromReader(OpenApiConfig config, ClassLoader loader) {
    String readerClassName = config.modelReader();
    if (readerClassName == null) {
        return null;
    }
    try {
        Class<?> c = loader.loadClass(readerClassName);
        OASModelReader reader = (OASModelReader) c.newInstance();
        return reader.buildModel();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: TomEEOpenAPIExtension.java    From tomee with 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);
}