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

software.amazon.smithy.openapi.model.OpenApi Maven / Gradle / Ivy

There is a newer version: 1.51.0
Show newest version
/*
 * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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 software.amazon.smithy.openapi.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SmithyBuilder;
import software.amazon.smithy.utils.ToSmithyBuilder;

public final class OpenApi extends Component implements ToSmithyBuilder {
    private final String openapi;
    private final InfoObject info;
    private final List servers;
    private final Map paths = new TreeMap<>();
    private final ComponentsObject components;
    private final List>> security;
    private final List tags;
    private final ExternalDocumentation externalDocs;

    private OpenApi(Builder builder) {
        super(builder);
        openapi = SmithyBuilder.requiredState("openapi", builder.openapi);
        info = SmithyBuilder.requiredState("info", builder.info);
        servers = ListUtils.copyOf(builder.servers);
        paths.putAll(builder.paths);
        components = builder.components == null ? ComponentsObject.builder().build() : builder.components;
        security = ListUtils.copyOf(builder.security);
        tags = ListUtils.copyOf(builder.tags);
        externalDocs = builder.externalDocs;
    }

    public static Builder builder() {
        return new Builder();
    }

    public String getOpenapi() {
        return openapi;
    }

    public InfoObject getInfo() {
        return info;
    }

    public List getServers() {
        return servers;
    }

    public Map getPaths() {
        return paths;
    }

    public ComponentsObject getComponents() {
        return components;
    }

    public List>> getSecurity() {
        return security;
    }

    public List getTags() {
        return tags;
    }

    public Optional getExternalDocs() {
        return Optional.ofNullable(externalDocs);
    }

    @Override
    public Builder toBuilder() {
        Builder builder = builder()
                .openapi(openapi)
                .info(info)
                .paths(paths)
                .components(components)
                .externalDocs(externalDocs)
                .extensions(getExtensions());
        security.forEach(builder::addSecurity);
        servers.forEach(builder::addServer);
        tags.forEach(builder::addTag);
        return builder.extensions(getExtensions());
    }

    @Override
    protected ObjectNode.Builder createNodeBuilder() {
        ObjectNode.Builder builder = Node.objectNodeBuilder()
                .withMember("openapi", openapi)
                .withMember("info", info)
                .withOptionalMember("externalDocumentation", getExternalDocs());

        if (!servers.isEmpty()) {
            builder.withMember("servers", servers.stream().collect(ArrayNode.collect()));
        }

        if (!paths.isEmpty()) {
            builder.withMember("paths", paths.entrySet().stream()
                    .collect(ObjectNode.collectStringKeys(Map.Entry::getKey, Map.Entry::getValue)));
        }

        builder.withMember("components", components);

        if (!security.isEmpty()) {
            builder.withMember("security", security.stream()
                    .map(mapping -> mapping.entrySet().stream()
                            .sorted(Comparator.comparing(Map.Entry::getKey))
                            .collect(ObjectNode.collectStringKeys(
                                    Map.Entry::getKey,
                                    entry -> entry.getValue().stream().map(Node::from).collect(ArrayNode.collect()))))
                    .collect(ArrayNode.collect()));
        }

        if (!tags.isEmpty()) {
            builder.withMember("tags", tags.stream().sorted().collect(ArrayNode.collect()));
        }

        return builder;
    }

    public static final class Builder extends Component.Builder {
        private String openapi;
        private InfoObject info;
        private final List servers = new ArrayList<>();
        private Map paths = new TreeMap<>();
        private ComponentsObject components;
        private final List>> security = new ArrayList<>();
        private final List tags = new ArrayList<>();
        private ExternalDocumentation externalDocs;

        private Builder() {}

        @Override
        public OpenApi build() {
            return new OpenApi(this);
        }

        public Builder openapi(String openapi) {
            this.openapi = openapi;
            return this;
        }

        public Builder info(InfoObject info) {
            this.info = info;
            return this;
        }

        public Builder paths(Map paths) {
            this.paths = paths;
            return this;
        }

        public Builder putPath(String path, PathItem item) {
            paths.put(path, item);
            return this;
        }

        public Builder removePath(String path) {
            paths.remove(path);
            return this;
        }

        public Builder components(ComponentsObject components) {
            this.components = components;
            return this;
        }

        public Builder externalDocs(ExternalDocumentation externalDocs) {
            this.externalDocs = externalDocs;
            return this;
        }

        public Builder addServer(ServerObject server) {
            this.servers.add(server);
            return this;
        }

        public Builder clearServer() {
            servers.clear();
            return this;
        }

        public Builder addSecurity(Map> requirement) {
            this.security.add(requirement);
            return this;
        }

        public Builder security(Collection>> security) {
            this.security.clear();
            this.security.addAll(security);
            return this;
        }

        public Builder clearSecurity() {
            security.clear();
            return this;
        }

        public Builder addTag(TagObject tag) {
            this.tags.add(tag);
            return this;
        }

        public Builder clearTags() {
            tags.clear();
            return this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy