View Javadoc
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.Utils.*;
22  import static org.apache.juneau.internal.ConverterUtils.*;
23  
24  import java.util.*;
25  
26  import org.apache.juneau.commons.collections.*;
27  
28  /**
29   * information for Examples object.
30   *
31   * <p>
32   * The Example Object provides an example of a media type. The example object is mutually exclusive of the examples
33   * object. Furthermore, if referencing a schema which contains an example, the example value shall override the example
34   * provided by the schema.
35   *
36   * <h5 class='section'>OpenAPI Specification:</h5>
37   * <p>
38   * The Example Object is composed of the following fields:
39   * <ul class='spaced-list'>
40   * 	<li><c>summary</c> (string) - Short description for the example
41   * 	<li><c>description</c> (string) - Long description for the example. CommonMark syntax MAY be used for rich text representation
42   * 	<li><c>value</c> (any) - Embedded literal example. The value field and externalValue field are mutually exclusive
43   * 	<li><c>externalValue</c> (string) - A URI that points to the literal example. This provides the capability to reference
44   * 		examples that cannot easily be included in JSON or YAML documents. The value field and externalValue field are mutually exclusive
45   * </ul>
46   *
47   * <h5 class='section'>Example:</h5>
48   * <p class='bcode'>
49   * 	<jc>// Construct using SwaggerBuilder.</jc>
50   * 	Example <jv>x</jv> = <jsm>example</jsm>()
51   * 		.setSummary(<js>"User example"</js>)
52   * 		.setValue(<js>"John Doe"</js>);
53   *
54   * 	<jc>// Serialize using JsonSerializer.</jc>
55   * 	String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>);
56   *
57   * 	<jc>// Or just use toString() which does the same as above.</jc>
58   * 	<jv>json</jv> = <jv>x</jv>.toString();
59   * </p>
60   * <p class='bcode'>
61   * 	<jc>// Output</jc>
62   * 	{
63   * 		<js>"summary"</js>: <js>"User example"</js>,
64   * 		<js>"value"</js>: <js>"John Doe"</js>
65   * 	}
66   * </p>
67   *
68   * <h5 class='section'>See Also:</h5><ul>
69   * 	<li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#example-object">OpenAPI Specification &gt; Example Object</a>
70   * 	<li class='link'><a class="doclink" href="https://swagger.io/docs/specification/adding-examples/">OpenAPI Adding Examples</a>
71   * 	<li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
72   * </ul>
73   */
74  public class Example extends OpenApiElement {
75  
76  	private String summary;
77  	private String description;
78  	private String externalValue;
79  	private Object value;
80  
81  	/**
82  	 * Default constructor.
83  	 */
84  	public Example() {}
85  
86  	/**
87  	 * Copy constructor.
88  	 *
89  	 * @param copyFrom The object to copy.
90  	 */
91  	public Example(Example copyFrom) {
92  		super(copyFrom);
93  
94  		this.summary = copyFrom.summary;
95  		this.description = copyFrom.description;
96  		this.externalValue = copyFrom.externalValue;
97  		this.value = copyFrom.value;
98  	}
99  
100 	/**
101 	 * Make a deep copy of this object.
102 	 *
103 	 * @return A deep copy of this object.
104 	 */
105 	public Example copy() {
106 		return new Example(this);
107 	}
108 
109 	@Override /* Overridden from OpenApiElement */
110 	public <T> T get(String property, Class<T> type) {
111 		assertArgNotNull("property", property);
112 		return switch (property) {
113 			case "description" -> toType(getDescription(), type);
114 			case "externalValue" -> toType(getExternalValue(), type);
115 			case "summary" -> toType(getSummary(), type);
116 			case "value" -> toType(getValue(), type);
117 			default -> super.get(property, type);
118 		};
119 	}
120 
121 	/**
122 	 * Bean property getter:  <property>description</property>.
123 	 *
124 	 * <p>
125 	 * The URL pointing to the contact information.
126 	 *
127 	 * @return The property value, or <jk>null</jk> if it is not set.
128 	 */
129 	public String getDescription() { return description; }
130 
131 	/**
132 	 * Bean property getter:  <property>externalValue</property>.
133 	 *
134 	 * <p>
135 	 * The email address of the contact person/organization.
136 	 *
137 	 * @return The property value, or <jk>null</jk> if it is not set.
138 	 */
139 	public String getExternalValue() { return externalValue; }
140 
141 	/**
142 	 * Bean property getter:  <property>summary</property>.
143 	 *
144 	 * <p>
145 	 * The identifying name of the contact person/organization.
146 	 *
147 	 * @return The property value, or <jk>null</jk> if it is not set.
148 	 */
149 	public String getSummary() { return summary; }
150 
151 	/**
152 	 * Bean property getter:  <property>default</property>.
153 	 *
154 	 * <p>
155 	 * Declares the value of the parameter that the server will use if none is provided, for example a <js>"count"</js>
156 	 * to control the number of results per page might default to 100 if not supplied by the client in the request.
157 	 *
158 	 * (Note: <js>"value"</js> has no meaning for required parameters.)
159 	 * Unlike JSON Schema this value MUST conform to the defined <code>type</code> for this parameter.
160 	 *
161 	 * @return The property value, or <jk>null</jk> if it is not set.
162 	 */
163 	public Object getValue() { return value; }
164 
165 	@Override /* Overridden from OpenApiElement */
166 	public Set<String> keySet() {
167 		// @formatter:off
168 		var s = setb(String.class)
169 			.addIf(nn(description), "description")
170 			.addIf(nn(externalValue), "externalValue")
171 			.addIf(nn(summary), "summary")
172 			.addIf(nn(value), "value")
173 			.build();
174 		// @formatter:on
175 		return new MultiSet<>(s, super.keySet());
176 	}
177 
178 	@Override /* Overridden from OpenApiElement */
179 	public Example set(String property, Object value) {
180 		assertArgNotNull("property", property);
181 		return switch (property) {
182 			case "description" -> setDescription(s(value));
183 			case "externalValue" -> setExternalValue(s(value));
184 			case "summary" -> setSummary(s(value));
185 			case "value" -> setValue(value);
186 			default -> {
187 				super.set(property, value);
188 				yield this;
189 			}
190 		};
191 	}
192 
193 	/**
194 	 * Bean property setter:  <property>description</property>.
195 	 * @param value
196 	 * 	The new value for this property.
197 	 * 	<br>Can be <jk>null</jk> to unset the property.
198 	 * @return This object
199 	 */
200 	public Example setDescription(String value) {
201 		description = value;
202 		return this;
203 	}
204 
205 	/**
206 	 * Bean property setter:  <property>externalValue</property>.
207 	 *
208 	 * <p>
209 	 * The email address of the contact person/organization.
210 	 *
211 	 * @param value
212 	 * 	The new value for this property.
213 	 * 	<br>MUST be in the format of an email address.
214 	 * 	<br>Can be <jk>null</jk> to unset the property.
215 	 * @return This object
216 	 */
217 	public Example setExternalValue(String value) {
218 		externalValue = value;
219 		return this;
220 	}
221 
222 	/**
223 	 * Bean property setter:  <property>summary</property>.
224 	 *
225 	 * <p>
226 	 * The identifying name of the contact person/organization.
227 	 *
228 	 * @param value
229 	 * 	The new value for this property.
230 	 * 	<br>Can be <jk>null</jk> to unset the property.
231 	 * @return This object
232 	 */
233 	public Example setSummary(String value) {
234 		summary = value;
235 		return this;
236 	}
237 
238 	/**
239 	 * Bean property setter:  <property>value</property>.
240 	 *
241 	 * <p>
242 	 * Declares the value of the parameter that the server will use if none is provided, for example a <js>"count"</js>
243 	 * to control the number of results per page might default to 100 if not supplied by the client in the request.
244 	 * (Note: <js>"default"</js> has no meaning for required parameters.)
245 	 * Unlike JSON Schema this value MUST conform to the defined <code>type</code> for this parameter.
246 	 *
247 	 * @param val The new value for this property.
248 	 * 	<br>Can be <jk>null</jk> to unset the property.
249 	 * @return This object
250 	 */
251 	public Example setValue(Object val) {
252 		value = val;
253 		return this;
254 	}
255 
256 	@Override /* Overridden from OpenApiElement */
257 	public Example strict() {
258 		super.strict();
259 		return this;
260 	}
261 
262 	@Override /* Overridden from OpenApiElement */
263 	public Example strict(Object value) {
264 		super.strict(value);
265 		return this;
266 	}
267 }