All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.bootique.swagger.SwaggerOpenapiApiFactory Maven / Gradle / Ivy

There is a newer version: 3.0.M2
Show newest version
/*
 * Licensed to ObjectStyle LLC under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ObjectStyle LLC licenses
 * this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package io.bootique.swagger;

import io.bootique.annotation.BQConfig;
import io.bootique.annotation.BQConfigProperty;
import io.bootique.jersey.MappedResource;
import io.bootique.resource.ResourceFactory;

import javax.inject.Provider;
import javax.ws.rs.core.Application;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @since 2.0
 */
@BQConfig
public class SwaggerOpenapiApiFactory {

    private ResourceFactory overrideSpec;
    private Map specs;
    private boolean pretty = true;

    @BQConfigProperty("Zero or more API specifications provided by the application")
    public void setSpecs(Map specs) {
        this.specs = specs;
    }

    @BQConfigProperty("Location of the OpenAPI spec file, that overrides 'spec', 'resourcePackages', 'resourceClasses' models. " +
            "This setting is shared by all child specs, unless they define an explicit 'overrideSpec' of their own")
    public void setOverrideSpec(ResourceFactory overrideSpec) {
        this.overrideSpec = overrideSpec;
    }

    @BQConfigProperty("Whether to format YAML and JSON. 'True' by default. This setting is shared by all child specs.")
    public void setPretty(boolean pretty) {
        this.pretty = pretty;
    }

    public MappedResource createResource(Provider appProvider) {

        Map models = new HashMap<>();
        resolveSpecs().values()
                .stream()
                .map(f -> f.createModel(appProvider, overrideSpec, pretty))
                // skip unmapped models
                .filter(Optional::isPresent)
                .map(Optional::get)
                .forEach(m -> indexByPath(models, m));

        SwaggerOpenapiApi resource = new SwaggerOpenapiApi(models);
        return new MappedResource<>(resource, models.keySet());
    }

    private void indexByPath(Map models, OpenApiModel model) {
        if (model.getPathJson() != null) {
            models.put(model.getPathJson(), model);
        }

        if (model.getPathYaml() != null) {
            models.put(model.getPathYaml(), model);
        }
    }

    protected Map resolveSpecs() {
        return this.specs != null ? this.specs : Collections.singletonMap("default", defaultApiFactory());
    }

    protected OpenApiModelFactory defaultApiFactory() {
        return new OpenApiModelFactory();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy