1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.juneau.bean.openapi3;
18
19 import static org.apache.juneau.commons.utils.AssertionUtils.*;
20 import static org.apache.juneau.commons.utils.CollectionUtils.*;
21 import static org.apache.juneau.commons.utils.StringUtils.*;
22 import static org.apache.juneau.commons.utils.Utils.*;
23 import static org.apache.juneau.internal.ConverterUtils.*;
24
25 import java.net.*;
26 import java.util.*;
27
28 import org.apache.juneau.*;
29 import org.apache.juneau.commons.collections.*;
30
31 /**
32 * Allows referencing an external resource for extended documentation.
33 *
34 * <p>
35 * The External Documentation Object allows referencing an external resource for extended documentation. This can be
36 * used to provide additional documentation that is not part of the main OpenAPI specification, such as detailed
37 * guides, tutorials, or API documentation hosted elsewhere.
38 *
39 * <h5 class='section'>OpenAPI Specification:</h5>
40 * <p>
41 * The External Documentation Object is composed of the following fields:
42 * <ul class='spaced-list'>
43 * <li><c>description</c> (string) - A short description of the target documentation (CommonMark syntax may be used)
44 * <li><c>url</c> (string, REQUIRED) - The URL for the target documentation
45 * </ul>
46 *
47 * <h5 class='section'>Example:</h5>
48 * <p class='bjava'>
49 * <jc>// Create external documentation reference</jc>
50 * ExternalDocumentation <jv>docs</jv> = <jk>new</jk> ExternalDocumentation()
51 * .setDescription(<js>"Find more info here"</js>)
52 * .setUrl(<js>"https://example.com"</js>);
53 * </p>
54 *
55 * <h5 class='section'>See Also:</h5><ul>
56 * <li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#external-documentation-object">OpenAPI Specification > External Documentation Object</a>
57 * <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/external-documentation/">OpenAPI External Documentation</a>
58 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
59 * </ul>
60 */
61 public class ExternalDocumentation extends OpenApiElement {
62
63 private String description;
64 private URI url;
65
66 /**
67 * Default constructor.
68 */
69 public ExternalDocumentation() {}
70
71 /**
72 * Copy constructor.
73 *
74 * @param copyFrom The object to copy.
75 */
76 public ExternalDocumentation(ExternalDocumentation copyFrom) {
77 super(copyFrom);
78
79 this.description = copyFrom.description;
80 this.url = copyFrom.url;
81 }
82
83 /**
84 * Make a deep copy of this object.
85 *
86 * @return A deep copy of this object.
87 */
88 public ExternalDocumentation copy() {
89 return new ExternalDocumentation(this);
90 }
91
92 @Override /* Overridden from OpenApiElement */
93 public <T> T get(String property, Class<T> type) {
94 assertArgNotNull("property", property);
95 return switch (property) {
96 case "description" -> toType(getDescription(), type);
97 case "url" -> toType(getUrl(), type);
98 default -> super.get(property, type);
99 };
100 }
101
102 /**
103 * Bean property getter: <property>description</property>.
104 *
105 * <p>
106 * A short description of the target documentation.
107 *
108 * @return The property value, or <jk>null</jk> if it is not set.
109 */
110 public String getDescription() { return description; }
111
112 /**
113 * Bean property getter: <property>url</property>.
114 *
115 * <p>
116 * The URL for the target documentation.
117 *
118 * @return The property value, or <jk>null</jk> if it is not set.
119 */
120 public URI getUrl() { return url; }
121
122 @Override /* Overridden from OpenApiElement */
123 public Set<String> keySet() {
124 // @formatter:off
125 var s = setb(String.class)
126 .addIf(nn(description), "description")
127 .addIf(nn(url), "url")
128 .build();
129 // @formatter:on
130 return new MultiSet<>(s, super.keySet());
131 }
132
133 @Override /* Overridden from OpenApiElement */
134 public ExternalDocumentation set(String property, Object value) {
135 assertArgNotNull("property", property);
136 return switch (property) {
137 case "description" -> setDescription(s(value));
138 case "url" -> setUrl(toUri(value));
139 default -> {
140 super.set(property, value);
141 yield this;
142 }
143 };
144 }
145
146 /**
147 * Bean property setter: <property>description</property>.
148 *
149 * <p>
150 * A short description of the target documentation.
151 *
152 * @param value
153 * The new value for this property.
154 * <br>Can be <jk>null</jk> to unset the property.
155 * @return This object
156 */
157 public ExternalDocumentation setDescription(String value) {
158 description = value;
159 return this;
160 }
161
162 /**
163 * Bean property setter: <property>url</property>.
164 *
165 * <p>
166 * The URL for the target documentation.
167 *
168 * @param value
169 * The new value for this property.
170 * <br>Property value is required.
171 * <br>URIs defined by {@link UriResolver} can be used for values.
172 * <br>Can be <jk>null</jk> to unset the property.
173 * @return This object
174 */
175 public ExternalDocumentation setUrl(URI value) {
176 url = value;
177 return this;
178 }
179
180 @Override /* Overridden from OpenApiElement */
181 public ExternalDocumentation strict() {
182 super.strict();
183 return this;
184 }
185
186 @Override /* Overridden from OpenApiElement */
187 public ExternalDocumentation strict(Object value) {
188 super.strict(value);
189 return this;
190 }
191 }