Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
OpenApiDocumentBuilder.java
Go to the documentation of this file.
1package dev.rafex.ether.http.openapi.builder;
2
3/*-
4 * #%L
5 * ether-http-openapi
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.util.ArrayList;
30import java.util.LinkedHashMap;
31import java.util.List;
32import java.util.Map;
33import java.util.Objects;
34
35import dev.rafex.ether.http.openapi.model.OpenApiComponents;
36import dev.rafex.ether.http.openapi.model.OpenApiDocument;
37import dev.rafex.ether.http.openapi.model.OpenApiInfo;
38import dev.rafex.ether.http.openapi.model.OpenApiOperation;
39import dev.rafex.ether.http.openapi.model.OpenApiPathItem;
40import dev.rafex.ether.http.openapi.model.OpenApiResponse;
41import dev.rafex.ether.http.openapi.model.OpenApiSchema;
42import dev.rafex.ether.http.openapi.model.OpenApiServer;
43
44public final class OpenApiDocumentBuilder {
45
46 private String openapi = "3.1.0";
47 private OpenApiInfo info = new OpenApiInfo("ether-api", "0.0.0", null, null, null);
48 private final List<OpenApiServer> servers = new ArrayList<>();
49 private final Map<String, OpenApiPathItem> paths = new LinkedHashMap<>();
50 private final Map<String, OpenApiSchema> schemas = new LinkedHashMap<>();
51 private final Map<String, OpenApiResponse> responses = new LinkedHashMap<>();
52
54 return new OpenApiDocumentBuilder();
55 }
56
57 public OpenApiDocumentBuilder openapi(final String openapi) {
58 this.openapi = Objects.requireNonNull(openapi, "openapi");
59 return this;
60 }
61
62 public OpenApiDocumentBuilder info(final OpenApiInfo info) {
63 this.info = Objects.requireNonNull(info, "info");
64 return this;
65 }
66
67 public OpenApiDocumentBuilder addServer(final OpenApiServer server) {
68 servers.add(Objects.requireNonNull(server, "server"));
69 return this;
70 }
71
72 public OpenApiDocumentBuilder addSchema(final String name, final OpenApiSchema schema) {
73 schemas.put(Objects.requireNonNull(name, "name"), Objects.requireNonNull(schema, "schema"));
74 return this;
75 }
76
77 public OpenApiDocumentBuilder addResponse(final String name, final OpenApiResponse response) {
78 responses.put(Objects.requireNonNull(name, "name"), Objects.requireNonNull(response, "response"));
79 return this;
80 }
81
82 public OpenApiDocumentBuilder addOperation(final String path, final String method,
83 final OpenApiOperation operation) {
84 Objects.requireNonNull(path, "path");
85 Objects.requireNonNull(method, "method");
86 Objects.requireNonNull(operation, "operation");
87 paths.compute(path,
88 (k, current) -> (current == null ? OpenApiPathItem.empty() : current).withOperation(method, operation));
89 return this;
90 }
91
92 public OpenApiDocument build() {
93 return new OpenApiDocument(openapi, info, servers, paths, new OpenApiComponents(schemas, responses));
94 }
95}
OpenApiDocumentBuilder addSchema(final String name, final OpenApiSchema schema)
OpenApiDocumentBuilder addOperation(final String path, final String method, final OpenApiOperation operation)
OpenApiDocumentBuilder addResponse(final String name, final OpenApiResponse response)
OpenApiDocumentBuilder addServer(final OpenApiServer server)