Search in sources :

Example 1 with PathsImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiDocument method initialize.

public void initialize() {
    synchronized (INSTANCE) {
        if (model != null) {
            modelAlreadyInitialized();
        }
        // Check all the required parts are set
        if (config == null) {
            throw new IllegalStateException("OpenApiConfig must be set before init");
        }
        // 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;
        LOGGER.info("OpenAPI document initialized: " + model);
        clear();
    }
}
Also used : OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) PathsImpl(org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl) OpenAPI(org.eclipse.microprofile.openapi.models.OpenAPI) InfoImpl(org.wildfly.swarm.microprofile.openapi.api.models.info.InfoImpl)

Example 2 with PathsImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiAnnotationScanner method scan.

/**
 * Scan the deployment for relevant annotations.  Returns an OpenAPI data model that was
 * built from those found annotations.
 */
public OpenAPIImpl scan() {
    LOG.debug("Scanning deployment for OpenAPI and JAX-RS Annotations.");
    // Initialize a new OAI document.  Even if nothing is found, this will be returned.
    oai = new OpenAPIImpl();
    oai.setOpenapi(OpenApiConstants.OPEN_API_VERSION);
    // Get all jax-rs applications and convert them to OAI models (and merge them into a single one)
    Collection<ClassInfo> applications = this.index.getAllKnownSubclasses(DotName.createSimple(Application.class.getName()));
    for (ClassInfo classInfo : applications) {
        oai = MergeUtil.merge(oai, jaxRsApplicationToOpenApi(classInfo));
    }
    // TODO find all OpenAPIDefinition annotations at the package level
    // Now find all jax-rs endpoints
    Collection<ClassInfo> resourceClasses = JandexUtil.getJaxRsResourceClasses(this.index);
    for (ClassInfo resourceClass : resourceClasses) {
        processJaxRsResourceClass(oai, resourceClass);
    }
    // Now that all paths have been created, sort them (we don't have a better way to organize them).
    if (oai != null) {
        Paths paths = oai.getPaths();
        if (paths != null) {
            Paths sortedPaths = new PathsImpl();
            TreeSet<String> sortedKeys = new TreeSet<>(paths.keySet());
            for (String pathKey : sortedKeys) {
                PathItem pathItem = paths.get(pathKey);
                sortedPaths.addPathItem(pathKey, pathItem);
            }
            sortedPaths.setExtensions(paths.getExtensions());
            oai.setPaths(sortedPaths);
        }
    }
    return oai;
}
Also used : PathItem(org.eclipse.microprofile.openapi.models.PathItem) OpenAPIImpl(org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl) TreeSet(java.util.TreeSet) PathsImpl(org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl) Paths(org.eclipse.microprofile.openapi.models.Paths) ClassInfo(org.jboss.jandex.ClassInfo)

Example 3 with PathsImpl

use of org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl in project wildfly-swarm by wildfly-swarm.

the class OpenApiParser method readPaths.

/**
 * Reads the {@link Paths} OpenAPI nodes.
 * @param node
 */
private Paths readPaths(JsonNode node) {
    if (node == null || !node.isObject()) {
        return null;
    }
    PathsImpl model = new PathsImpl();
    for (Iterator<String> fieldNames = node.fieldNames(); fieldNames.hasNext(); ) {
        String fieldName = fieldNames.next();
        if (fieldName.startsWith(OpenApiConstants.EXTENSION_PROPERTY_PREFIX)) {
            continue;
        }
        model.addPathItem(fieldName, readPathItem(node.get(fieldName)));
    }
    readExtensions(node, model);
    return model;
}
Also used : PathsImpl(org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl)

Aggregations

PathsImpl (org.wildfly.swarm.microprofile.openapi.api.models.PathsImpl)3 OpenAPIImpl (org.wildfly.swarm.microprofile.openapi.api.models.OpenAPIImpl)2 TreeSet (java.util.TreeSet)1 OpenAPI (org.eclipse.microprofile.openapi.models.OpenAPI)1 PathItem (org.eclipse.microprofile.openapi.models.PathItem)1 Paths (org.eclipse.microprofile.openapi.models.Paths)1 ClassInfo (org.jboss.jandex.ClassInfo)1 InfoImpl (org.wildfly.swarm.microprofile.openapi.api.models.info.InfoImpl)1