001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.dto.openapi3;
014
015import static org.apache.juneau.internal.ConverterUtils.*;
016
017import org.apache.juneau.annotation.Bean;
018import org.apache.juneau.internal.*;
019
020import java.util.Set;
021
022import static org.apache.juneau.internal.CollectionUtils.*;
023
024/**
025 * Describes a single operation parameter.
026 *
027 * <p>
028 * A unique parameter is defined by a combination of a name and location.
029 *
030 * <p>
031 * There are five possible parameter types.
032 * <ul class='spaced-list'>
033 *    <li><js>"path"</js> - Used together with Path Templating, where the parameter value is actually part of the
034 *       operation's URL.
035 *       This does not include the host or base path of the API.
036 *       For example, in <code>/items/{itemId}</code>, the path parameter is <code>itemId</code>.
037 *    <li><js>"query"</js> - Parameters that are appended to the URL.
038 *       For example, in <code>/items?id=###</code>, the query parameter is <code>id</code>.
039 *    <li><js>"header"</js> - Custom headers that are expected as part of the request.
040 *    <li><js>"body"</js> - The payload that's appended to the HTTP request.
041 *       Since there can only be one payload, there can only be one body parameter.
042 *       The name of the body parameter has no effect on the parameter itself and is used for documentation purposes
043 *       only.
044 *       Since Form parameters are also in the payload, body and form parameters cannot exist together for the same
045 *       operation.
046 *    <li><js>"formData"</js> - Used to describe the payload of an HTTP request when either
047 *       <code>application/x-www-form-urlencoded</code>, <code>multipart/form-data</code> or both are used as the
048 *       content type of the request (in Swagger's definition, the consumes property of an operation).
049 *       This is the only parameter type that can be used to send files, thus supporting the file type.
050 *       Since form parameters are sent in the payload, they cannot be declared together with a body parameter for the
051 *       same operation.
052 *       Form parameters have a different format based on the content-type used (for further details, consult
053 *       <code>http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4</code>):
054 *       <ul>
055 *          <li><js>"application/x-www-form-urlencoded"</js> - Similar to the format of Query parameters but as a
056 *             payload.
057 *             For example, <code>foo=1&amp;bar=swagger</code> - both <code>foo</code> and <code>bar</code> are form
058 *             parameters.
059 *             This is normally used for simple parameters that are being transferred.
060 *          <li><js>"multipart/form-data"</js> - each parameter takes a section in the payload with an internal header.
061 *             For example, for the header <code>Content-Disposition: form-data; name="submit-name"</code> the name of
062 *             the parameter is <code>submit-name</code>.
063 *             This type of form parameters is more commonly used for file transfers.
064 *       </ul>
065 *    </li>
066 * </ul>
067 *
068 * <h5 class='section'>Example:</h5>
069 * <p class='bcode'>
070 *    <jc>// Construct using SwaggerBuilder.</jc>
071 *    ParameterInfo x = <jsm>parameterInfo</jsm>(<js>"query"</js>, <js>"foo"</js>);
072 *
073 *    <jc>// Serialize using JsonSerializer.</jc>
074 *    String json = JsonSerializer.<jsf>DEFAULT</jsf>.toString(x);
075 *
076 *    <jc>// Or just use toString() which does the same as above.</jc>
077 *    String json = x.toString();
078 * </p>
079 * <p class='bcode'>
080 *    <jc>// Output</jc>
081 *    {
082 *       <js>"in"</js>: <js>"query"</js>,
083 *       <js>"name"</js>: <js>"foo"</js>
084 *    }
085 * </p>
086 */
087@Bean(properties="implicit,password,clientCredentials,authorizationCode,*")
088@FluentSetters
089public class OAuthFlows extends OpenApiElement {
090
091   private OAuthFlow implicit,
092         password,
093         clientCredentials,
094         authorizationCode;
095
096   /**
097    * Default constructor.
098    */
099   public OAuthFlows() {}
100
101   /**
102    * Copy constructor.
103    *
104    * @param copyFrom The object to copy.
105    */
106   public OAuthFlows(OAuthFlows copyFrom) {
107      super(copyFrom);
108
109      this.implicit = copyFrom.implicit;
110      this.password = copyFrom.password;
111      this.clientCredentials = copyFrom.clientCredentials;
112      this.authorizationCode = copyFrom.authorizationCode;
113   }
114
115   /**
116    * Make a deep copy of this object.
117    *
118    * @return A deep copy of this object.
119    */
120   public OAuthFlows copy() {
121      return new OAuthFlows(this);
122   }
123
124   @Override /* SwaggerElement */
125   protected OAuthFlows strict() {
126      super.strict();
127      return this;
128   }
129
130   /**
131    * Bean property getter:  <property>implicit</property>.
132    *
133    * <p>
134    * Describes the type of items in the array.
135    *
136    * @return The property value, or <jk>null</jk> if it is not set.
137    */
138   public OAuthFlow getImplicit() {
139      return implicit;
140   }
141
142   /**
143    * Bean property setter:  <property>items</property>.
144    *
145    * <p>
146    * Describes the type of items in the array.
147    *
148    * @param value
149    *    The new value for this property.
150    *    <br>Property value is required if <code>type</code> is <js>"array"</js>.
151    *    <br>Can be <jk>null</jk> to unset the property.
152    * @return This object
153    */
154   public OAuthFlows setImplicit(OAuthFlow value) {
155      implicit = value;
156      return this;
157   }
158
159   /**
160    * Bean property getter:  <property>password</property>.
161    *
162    * <p>
163    * Describes the type of items in the array.
164    *
165    * @return The property value, or <jk>null</jk> if it is not set.
166    */
167   public OAuthFlow getPassword() {
168      return password;
169   }
170
171   /**
172    * Bean property setter:  <property>items</property>.
173    *
174    * <p>
175    * Describes the type of items in the array.
176    *
177    * @param value
178    *    The new value for this property.
179    *    <br>Property value is required if <code>type</code> is <js>"array"</js>.
180    *    <br>Can be <jk>null</jk> to unset the property.
181    * @return This object
182    */
183   public OAuthFlows setPassword(OAuthFlow value) {
184      password = value;
185      return this;
186   }
187
188   /**
189    * Bean property getter:  <property>clientCredentials</property>.
190    *
191    * <p>
192    * Describes the type of items in the array.
193    *
194    * @return The property value, or <jk>null</jk> if it is not set.
195    */
196   public OAuthFlow getClientCredentials() {
197      return clientCredentials;
198   }
199
200   /**
201    * Bean property setter:  <property>items</property>.
202    *
203    * <p>
204    * Describes the type of items in the array.
205    *
206    * @param value
207    *    The new value for this property.
208    *    <br>Property value is required if <code>type</code> is <js>"array"</js>.
209    *    <br>Can be <jk>null</jk> to unset the property.
210    * @return This object
211    */
212   public OAuthFlows setClientCredentials(OAuthFlow value) {
213      clientCredentials = value;
214      return this;
215   }
216
217   /**
218    * Bean property getter:  <property>authorizationCode</property>.
219    *
220    * <p>
221    * Describes the type of items in the array.
222    *
223    * @return The property value, or <jk>null</jk> if it is not set.
224    */
225   public OAuthFlow getAuthorizationCode() {
226      return authorizationCode;
227   }
228
229   /**
230    * Bean property setter:  <property>authorizationCode</property>.
231    *
232    * <p>
233    * Describes the type of items in the array.
234    *
235    * @param value
236    *    The new value for this property.
237    *    <br>Property value is required if <code>type</code> is <js>"array"</js>.
238    *    <br>Can be <jk>null</jk> to unset the property.
239    * @return This object
240    */
241   public OAuthFlows setAuthorizationCode(OAuthFlow value) {
242      authorizationCode = value;
243      return this;
244   }
245
246   // <FluentSetters>
247
248   // </FluentSetters>
249
250   @Override /* SwaggerElement */
251   public <T> T get(String property, Class<T> type) {
252      if (property == null)
253         return null;
254      switch (property) {
255         case "implicit": return toType(getImplicit(), type);
256         case "password": return toType(getPassword(), type);
257         case "clientCredentials": return toType(getClientCredentials(), type);
258         case "authorizationCode": return toType(getAuthorizationCode(), type);
259         default: return super.get(property, type);
260      }
261   }
262
263   @Override /* SwaggerElement */
264   public OAuthFlows set(String property, Object value) {
265      if (property == null)
266         return this;
267      switch (property) {
268         case "implicit": return setImplicit(toType(value, OAuthFlow.class));
269         case "password": return setPassword(toType(value, OAuthFlow.class));
270         case "clientCredentials": return setClientCredentials(toType(value, OAuthFlow.class));
271         case "authorizationCode": return setAuthorizationCode(toType(value, OAuthFlow.class));
272         default:
273            super.set(property, value);
274            return this;
275      }
276   }
277
278   @Override /* SwaggerElement */
279   public Set<String> keySet() {
280      Set<String> s = setBuilder(String.class)
281         .addIf(implicit != null, "implicit")
282         .addIf(password != null, "password")
283         .addIf(clientCredentials != null, "clientCredentials")
284         .addIf(authorizationCode != null, "authorizationCode")
285         .build();
286      return new MultiSet<>(s, super.keySet());
287   }
288
289}