001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.bean.openapi3;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.marshall.internal.ConverterUtils.*;
021
022import java.util.*;
023
024import org.apache.juneau.commons.collections.*;
025
026/**
027 * information for Examples object.
028 *
029 * <p>
030 * The Example Object provides an example of a media type. The example object is mutually exclusive of the examples
031 * object. Furthermore, if referencing a schema which contains an example, the example value shall override the example
032 * provided by the schema.
033 *
034 * <h5 class='section'>OpenAPI Specification:</h5>
035 * <p>
036 * The Example Object is composed of the following fields:
037 * <ul class='spaced-list'>
038 *    <li><c>summary</c> (string) - Short description for the example
039 *    <li><c>description</c> (string) - Long description for the example. CommonMark syntax MAY be used for rich text representation
040 *    <li><c>value</c> (any) - Embedded literal example. The value field and externalValue field are mutually exclusive
041 *    <li><c>externalValue</c> (string) - A URI that points to the literal example. This provides the capability to reference
042 *       examples that cannot easily be included in JSON or YAML documents. The value field and externalValue field are mutually exclusive
043 * </ul>
044 *
045 * <h5 class='section'>Example:</h5>
046 * <p class='bcode'>
047 *    <jc>// Construct using SwaggerBuilder.</jc>
048 *    Example <jv>x</jv> = <jsm>example</jsm>()
049 *       .setSummary(<js>"User example"</js>)
050 *       .setValue(<js>"John Doe"</js>);
051 *
052 *    <jc>// Serialize using JsonSerializer.</jc>
053 *    String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>);
054 *
055 *    <jc>// Or just use toString() which does the same as above.</jc>
056 *    <jv>json</jv> = <jv>x</jv>.toString();
057 * </p>
058 * <p class='bcode'>
059 *    <jc>// Output</jc>
060 *    {
061 *       <js>"summary"</js>: <js>"User example"</js>,
062 *       <js>"value"</js>: <js>"John Doe"</js>
063 *    }
064 * </p>
065 *
066 * <h5 class='section'>See Also:</h5><ul>
067 *    <li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#example-object">OpenAPI Specification &gt; Example Object</a>
068 *    <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/adding-examples/">OpenAPI Adding Examples</a>
069 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
070 * </ul>
071 */
072@SuppressWarnings({
073   "java:S1192" // Duplicated literals (property names) read more clearly inline than as constants
074})
075public class Example extends OpenApiElement {
076
077   private String summary;
078   private String description;
079   private String externalValue;
080   private Object value;
081
082   /**
083    * Default constructor.
084    */
085   public Example() {}
086
087   /**
088    * Copy constructor.
089    *
090    * @param copyFrom The object to copy.
091    */
092   public Example(Example copyFrom) {
093      super(copyFrom);
094
095      this.summary = copyFrom.summary;
096      this.description = copyFrom.description;
097      this.externalValue = copyFrom.externalValue;
098      this.value = copyFrom.value;
099   }
100
101   /**
102    * Make a deep copy of this object.
103    *
104    * @return A deep copy of this object.
105    */
106   public Example copy() {
107      return new Example(this);
108   }
109
110   @Override /* Overridden from OpenApiElement */
111   public <T> T get(String property, Class<T> type) {
112      reqnn("property", property);
113      return switch (property) {
114         case "description" -> toType(getDescription(), type);
115         case "externalValue" -> toType(getExternalValue(), type);
116         case "summary" -> toType(getSummary(), type);
117         case "value" -> toType(getValue(), type);
118         default -> super.get(property, type);
119      };
120   }
121
122   /**
123    * Bean property getter:  <property>description</property>.
124    *
125    * <p>
126    * Long description for the example.
127    *
128    * @return The property value, or <jk>null</jk> if it is not set.
129    */
130   public String getDescription() { return description; }
131
132   /**
133    * Bean property getter:  <property>externalValue</property>.
134    *
135    * <p>
136    * A URI that points to the literal example.
137    *
138    * @return The property value, or <jk>null</jk> if it is not set.
139    */
140   public String getExternalValue() { return externalValue; }
141
142   /**
143    * Bean property getter:  <property>summary</property>.
144    *
145    * <p>
146    * Short description for the example.
147    *
148    * @return The property value, or <jk>null</jk> if it is not set.
149    */
150   public String getSummary() { return summary; }
151
152   /**
153    * Bean property getter:  <property>value</property>.
154    *
155    * <p>
156    * Embedded literal example.
157    *
158    * @return The property value, or <jk>null</jk> if it is not set.
159    */
160   public Object getValue() { return value; }
161
162   @Override /* Overridden from OpenApiElement */
163   public Set<String> keySet() {
164      // @formatter:off
165      var s = stb(String.class)
166         .addIf(nn(description), "description")
167         .addIf(nn(externalValue), "externalValue")
168         .addIf(nn(summary), "summary")
169         .addIf(nn(value), "value")
170         .build();
171      // @formatter:on
172      return new MultiSet<>(s, super.keySet());
173   }
174
175   @Override /* Overridden from OpenApiElement */
176   public Example set(String property, Object value) {
177      reqnn("property", property);
178      return switch (property) {
179         case "description" -> setDescription(s(value));
180         case "externalValue" -> setExternalValue(s(value));
181         case "summary" -> setSummary(s(value));
182         case "value" -> setValue(value);
183         default -> {
184            super.set(property, value);
185            yield this;
186         }
187      };
188   }
189
190   /**
191    * Bean property setter:  <property>description</property>.
192    * @param value
193    *    The new value for this property.
194    *    <br>Can be <jk>null</jk> to unset the property.
195    * @return This object
196    */
197   public Example setDescription(String value) {
198      description = value;
199      return this;
200   }
201
202   /**
203    * Bean property setter:  <property>externalValue</property>.
204    *
205    * <p>
206    * A URI that points to the literal example.
207    *
208    * @param value
209    *    The new value for this property.
210    *    <br>Can be <jk>null</jk> to unset the property.
211    * @return This object
212    */
213   public Example setExternalValue(String value) {
214      externalValue = value;
215      return this;
216   }
217
218   /**
219    * Bean property setter:  <property>summary</property>.
220    *
221    * <p>
222    * Short description for the example.
223    *
224    * @param value
225    *    The new value for this property.
226    *    <br>Can be <jk>null</jk> to unset the property.
227    * @return This object
228    */
229   public Example setSummary(String value) {
230      summary = value;
231      return this;
232   }
233
234   /**
235    * Bean property setter:  <property>value</property>.
236    *
237    * <p>
238    * Embedded literal example.
239    *
240    * @param val The new value for this property.
241    *    <br>Can be <jk>null</jk> to unset the property.
242    * @return This object
243    */
244   public Example setValue(Object val) {
245      value = val;
246      return this;
247   }
248
249   @Override /* Overridden from OpenApiElement */
250   public Example strict() {
251      super.strict();
252      return this;
253   }
254
255   @Override /* Overridden from OpenApiElement */
256   public Example strict(Object value) {
257      super.strict(value);
258      return this;
259   }
260}