Java Code Examples for org.eclipse.microprofile.openapi.models.OpenAPI#setOpenapi()

The following examples show how to use org.eclipse.microprofile.openapi.models.OpenAPI#setOpenapi() . 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: OpenApiAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
private OpenAPI scanMicroProfileOpenApiAnnotations() {

        // Initialize a new OAI document.  Even if nothing is found, this will be returned.
        OpenAPI openApi = new OpenAPIImpl();
        openApi.setOpenapi(OpenApiConstants.OPEN_API_VERSION);

        // Creating a new instance of a registry which will be set on the thread context.
        SchemaRegistry schemaRegistry = SchemaRegistry.newInstance(annotationScannerContext.getConfig(), openApi,
                annotationScannerContext.getIndex());

        // Register custom schemas if available
        getCustomSchemaRegistry(annotationScannerContext.getConfig()).registerCustomSchemas(schemaRegistry);

        // Find all OpenAPIDefinition annotations at the package level
        ScannerLogging.log.scanning("OpenAPI");
        processPackageOpenAPIDefinitions(annotationScannerContext, openApi);

        return openApi;
    }
 
Example 2
Source File: DefinitionReader.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a OpenAPIDefinition Json node.
 * 
 * @param openApi the OpenAPI model
 * @param node the Json node
 */
public static void processDefinition(final OpenAPI openApi,
        final JsonNode node) {
    IoLogging.log.jsonNode("OpenAPIDefinition");

    openApi.setOpenapi(JsonUtil.stringProperty(node, DefinitionConstant.PROP_OPENAPI));
    openApi.setInfo(InfoReader.readInfo(node.get(DefinitionConstant.PROP_INFO)));
    openApi.setTags(TagReader.readTags(node.get(DefinitionConstant.PROP_TAGS)).orElse(null));
    openApi.setServers(ServerReader.readServers(node.get(DefinitionConstant.PROP_SERVERS)).orElse(null));
    openApi.setSecurity(SecurityRequirementReader
            .readSecurityRequirements(node.get(DefinitionConstant.PROP_SECURITY)).orElse(null));
    openApi.setExternalDocs(
            ExternalDocsReader.readExternalDocs(node.get(ExternalDocsConstant.PROP_EXTERNAL_DOCS)));
    openApi.setComponents(
            ComponentsReader.readComponents(node.get(DefinitionConstant.PROP_COMPONENTS)));
    openApi.setPaths(PathsReader.readPaths(node.get(DefinitionConstant.PROP_PATHS)));
    ExtensionReader.readExtensions(node, openApi);
}
 
Example 3
Source File: JaxRsAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Processes a JAX-RS {@link Application} and creates an {@link OpenAPI} model. Performs
 * annotation scanning and other processing. Returns a model unique to that single JAX-RS
 * app.
 * 
 * @param applicationClass
 */
private OpenAPI processApplicationClass(final AnnotationScannerContext context, ClassInfo applicationClass) {
    OpenAPI openApi = new OpenAPIImpl();
    openApi.setOpenapi(OpenApiConstants.OPEN_API_VERSION);

    // Get the @ApplicationPath info and save it for later (also support @Path which seems nonstandard but common).
    AnnotationInstance applicationPathAnnotation = JandexUtil.getClassAnnotation(applicationClass,
            JaxRsConstants.APPLICATION_PATH);
    if (applicationPathAnnotation == null || context.getConfig().applicationPathDisable()) {
        applicationPathAnnotation = JandexUtil.getClassAnnotation(applicationClass, JaxRsConstants.PATH);
    }
    // TODO: Add support for Application selection when there are more than one
    if (applicationPathAnnotation != null) {
        this.currentAppPath = applicationPathAnnotation.value().asString();
    } else {
        this.currentAppPath = "/";
    }

    // Process @OpenAPIDefinition annotation
    processDefinitionAnnotation(context, applicationClass, openApi);

    // Process @SecurityScheme annotations
    processSecuritySchemeAnnotation(applicationClass, openApi);

    // Process @Server annotations
    processServerAnnotation(applicationClass, openApi);

    return openApi;
}
 
Example 4
Source File: SpringAnnotationScanner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * Processes a Spring Controller and creates an {@link OpenAPI} model. Performs
 * annotation scanning and other processing. Returns a model unique to that single Spring
 * controller.
 *
 * @param context the scanning context
 * @param controllerClass the Spring REST controller
 */
private OpenAPI processControllerClass(final AnnotationScannerContext context, ClassInfo controllerClass) {

    SpringLogging.log.processingController(controllerClass.simpleName());

    OpenAPI openApi = new OpenAPIImpl();
    openApi.setOpenapi(OpenApiConstants.OPEN_API_VERSION);

    // Get the @RequestMapping info and save it for later
    AnnotationInstance requestMappingAnnotation = JandexUtil.getClassAnnotation(controllerClass,
            SpringConstants.REQUEST_MAPPING);

    if (requestMappingAnnotation != null) {
        this.currentAppPath = ParameterProcessor.requestMappingValuesToPath(requestMappingAnnotation);
    } else {
        this.currentAppPath = "/";
    }

    // Process @OpenAPIDefinition annotation
    processDefinitionAnnotation(context, controllerClass, openApi);

    // Process @SecurityScheme annotations
    processSecuritySchemeAnnotation(controllerClass, openApi);

    // Process @Server annotations
    processServerAnnotation(controllerClass, openApi);

    // Process Java security
    processJavaSecurity(controllerClass, openApi);

    // Now find and process the operation methods
    processControllerMethods(context, controllerClass, openApi, null);

    return openApi;
}
 
Example 5
Source File: OpenApiDocument.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
public void initialize() {
    synchronized (INSTANCE) {
        if (model != null) {
            modelAlreadyInitialized();
        }
        // Check all the required parts are set
        if (config == null) {
            throw ApiMessages.msg.configMustBeSet();
        }

        // Phase 1: Use OASModelReader
        OpenAPI merged = readerModel;

        // Phase 2: Merge any static OpenAPI file packaged in the app
        merged = MergeUtil.mergeObjects(merged, staticFileModel);

        // Phase 3: Merge annotations
        merged = MergeUtil.mergeObjects(merged, annotationsModel);

        // Phase 4: Filter model via OASFilter
        merged = filterModel(merged);

        // Phase 5: Default empty document if model == null
        if (merged == null) {
            merged = new OpenAPIImpl();
            merged.setOpenapi(OpenApiConstants.OPEN_API_VERSION);
        }

        // Phase 6: Provide missing required elements
        if (merged.getPaths() == null) {
            merged.setPaths(new PathsImpl());
        }
        if (merged.getInfo() == null) {
            merged.setInfo(new InfoImpl());
        }
        if (merged.getInfo().getTitle() == null) {
            merged.getInfo().setTitle((archiveName == null ? "Generated" : archiveName) + " API");
        }
        if (merged.getInfo().getVersion() == null) {
            merged.getInfo().setVersion("1.0");
        }

        // Phase 7: Use Config values to add Servers (global, pathItem, operation)
        ServersUtil.configureServers(config, merged);

        model = merged;
        clear();
    }
}