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

software.amazon.smithy.openapi.model.OperationObject 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.Collections;
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.Pair;
import software.amazon.smithy.utils.ToSmithyBuilder;

public final class OperationObject extends Component implements ToSmithyBuilder {
    private final String summary;
    private final String description;
    private final ExternalDocumentation externalDocs;
    private final String operationId;
    private final RequestBodyObject requestBody;
    private final boolean deprecated;
    private final List tags;
    private final List parameters;
    private final Map responses;
    private final Map callbacks;
    private final List>> security;
    private final List servers;

    private OperationObject(Builder builder) {
        super(builder);
        tags = ListUtils.copyOf(builder.tags);
        summary = builder.summary;
        description = builder.description;
        externalDocs = builder.externalDocs;
        operationId = builder.operationId;
        parameters = ListUtils.copyOf(builder.parameters);
        requestBody = builder.requestBody;
        responses = Collections.unmodifiableMap(new TreeMap<>(builder.responses));
        deprecated = builder.deprecated;
        callbacks = Collections.unmodifiableMap(new TreeMap<>(builder.callbacks));
        if (builder.security != null) {
            security = ListUtils.copyOf(builder.security);
        } else {
            security = null;
        }
        servers = ListUtils.copyOf(builder.servers);
    }

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

    public List getTags() {
        return tags;
    }

    public Optional getSummary() {
        return Optional.ofNullable(summary);
    }

    public Optional getDescription() {
        return Optional.ofNullable(description);
    }

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

    public Optional getOperationId() {
        return Optional.ofNullable(operationId);
    }

    public List getParameters() {
        return parameters;
    }

    public Optional getRequestBody() {
        return Optional.ofNullable(requestBody);
    }

    public Map getResponses() {
        return responses;
    }

    public Map getCallbacks() {
        return callbacks;
    }

    public boolean isDeprecated() {
        return deprecated;
    }

    public Optional>>> getSecurity() {
        return Optional.ofNullable(security);
    }

    public List getServers() {
        return servers;
    }

    @Override
    protected ObjectNode.Builder createNodeBuilder() {
        ObjectNode.Builder builder = Node.objectNodeBuilder()
                .withOptionalMember("description", getDescription().map(Node::from))
                .withOptionalMember("summary", getSummary().map(Node::from))
                .withOptionalMember("externalDocs", getExternalDocs())
                .withOptionalMember("operationId", getOperationId().map(Node::from))
                .withOptionalMember("requestBody", getRequestBody());

        if (isDeprecated()) {
            builder.withMember("deprecated", Node.from(true));
        }

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

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

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

        if (getSecurity().isPresent()) {
            builder.withMember("security", getSecurity().get().stream()
                    .map(map -> map.entrySet().stream()
                            .sorted(Comparator.comparing(Map.Entry::getKey))
                            .map(entry -> Pair.of(entry.getKey(), entry.getValue().stream().map(Node::from)
                                    .collect(ArrayNode.collect())))
                            .collect(ObjectNode.collectStringKeys(Pair::getLeft, Pair::getRight)))
                    .collect(ArrayNode.collect()));
        }

        if (!servers.isEmpty()) {
            builder.withMember("servers", getServers().stream()
                    .map(ServerObject::toNode).collect(ArrayNode.collect()));
        }

        if (!tags.isEmpty()) {
            builder.withMember("tags", getTags().stream().sorted().map(Node::from).collect(ArrayNode.collect()));
        }

        return builder;
    }

    @Override
    public Builder toBuilder() {
        Builder builder = builder()
                .extensions(getExtensions())
                .callbacks(callbacks)
                .responses(responses)
                .parameters(parameters)
                .servers(servers)
                .summary(summary)
                .tags(tags)
                .deprecated(deprecated)
                .description(description)
                .externalDocs(externalDocs)
                .operationId(operationId)
                .requestBody(requestBody);
        getSecurity().ifPresent(builder::security);
        return builder;
    }

    public static final class Builder extends Component.Builder {
        private final List tags = new ArrayList<>();
        private final List parameters = new ArrayList<>();
        private final Map responses = new TreeMap<>();
        private final Map callbacks = new TreeMap<>();
        private List>> security;
        private final List servers = new ArrayList<>();
        private String summary;
        private String description;
        private ExternalDocumentation externalDocs;
        private String operationId;
        private RequestBodyObject requestBody;
        private boolean deprecated;

        private Builder() {}

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

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

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

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

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

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

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

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

        public Builder addParameter(ParameterObject parameter) {
            parameters.add(parameter);
            return this;
        }

        public Builder requestBody(RequestBodyObject requestBody) {
            this.requestBody = requestBody;
            return this;
        }

        public Builder responses(Map responses) {
            this.responses.clear();
            this.responses.putAll(responses);
            return this;
        }

        public Builder putResponse(String statusCode, ResponseObject response) {
            responses.put(statusCode, response);
            return this;
        }

        public Builder callbacks(Map callbacks) {
            this.callbacks.clear();
            this.callbacks.putAll(callbacks);
            return this;
        }

        public Builder putCallback(String expression, CallbackObject callback) {
            callbacks.put(expression, callback);
            return this;
        }

        public Builder deprecated(boolean deprecated) {
            this.deprecated = deprecated;
            return this;
        }

        public Builder security(Collection>> security) {
            this.security = new ArrayList<>();
            this.security.addAll(security);
            return this;
        }

        public Builder addSecurity(Map> security) {
            if (this.security == null) {
                this.security = new ArrayList<>();
            }
            this.security.add(security);
            return this;
        }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy