All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.dto.swagger.HeaderInfo Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.dto.swagger;

import static org.apache.juneau.internal.BeanPropertyUtils.*;
import static org.apache.juneau.internal.ArrayUtils.*;

import java.util.*;

import org.apache.juneau.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.utils.*;

/**
 * Describes a single HTTP header.
 *
 * 
Example:
*

* // Construct using SwaggerBuilder. * HeaderInfo x = headerInfo("integer").description("The number of allowed requests in the current period"); * * // Serialize using JsonSerializer. * String json = JsonSerializer.DEFAULT.toString(x); * * // Or just use toString() which does the same as above. * String json = x.toString(); *

*

* // Output * { * "description": "The number of allowed requests in the current period", * "type": "integer" * } *

* *
See Also:
*
    *
*/ @Bean(properties="description,type,format,items,collectionFormat,default,maximum,exclusiveMaximum,minimum,exclusiveMinimum,maxLength,minLength,pattern,maxItems,minItems,uniqueItems,enum,multipleOf,$ref,x-example,*") @SuppressWarnings({"unchecked"}) public class HeaderInfo extends SwaggerElement { private static final String[] VALID_TYPES = {"string", "number", "integer", "boolean", "array"}; private static final String[] VALID_COLLECTION_FORMATS = {"csv","ssv","tsv","pipes","multi"}; private String description, type, format, collectionFormat, pattern, ref; private Number maximum, minimum, multipleOf; private Integer maxLength, minLength, maxItems, minItems; private Boolean exclusiveMaximum, exclusiveMinimum, uniqueItems; private Items items; private Object _default; private List _enum; private Object example; /** * Default constructor. */ public HeaderInfo() {} /** * Copy constructor. * * @param copyFrom The object to copy. */ public HeaderInfo(HeaderInfo copyFrom) { super(copyFrom); this.description = copyFrom.description; this.type = copyFrom.type; this.format = copyFrom.format; this.collectionFormat = copyFrom.collectionFormat; this.pattern = copyFrom.pattern; this.maximum = copyFrom.maximum; this.minimum = copyFrom.minimum; this.multipleOf = copyFrom.multipleOf; this.maxLength = copyFrom.maxLength; this.minLength = copyFrom.minLength; this.maxItems = copyFrom.maxItems; this.minItems = copyFrom.minItems; this.exclusiveMaximum = copyFrom.exclusiveMaximum; this.exclusiveMinimum = copyFrom.exclusiveMinimum; this.uniqueItems = copyFrom.uniqueItems; this._default = copyFrom._default; this.items = copyFrom.items == null ? null : copyFrom.items.copy(); this._enum = newList(copyFrom._enum); this.example = copyFrom.example; } /** * Make a deep copy of this object. * * @return A deep copy of this object. */ public HeaderInfo copy() { return new HeaderInfo(this); } @Override /* SwaggerElement */ protected HeaderInfo strict() { super.strict(); return this; } /** * Bean property getter: description. * *

* A short description of the header. * * @return The property value, or null if it is not set. */ public String getDescription() { return description; } /** * Bean property setter: description. * *

* A short description of the header. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setDescription(String value) { description = value; return this; } /** * Same as {@link #setDescription(String)}. * * @param value * The new value for this property. *
Non-String values will be converted to String using toString(). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo description(Object value) { return setDescription(toStringVal(value)); } /** * Bean property getter: type. * *

* The type of the object. * * @return The property value, or null if it is not set. */ public String getType() { return type; } /** * Bean property setter: type. * *

* The type of the object. * *

See Also:
*
    *
* * @param value * The new value for this property. *
Property value is required. *
Valid values: *
    *
  • "string" *
  • "number" *
  • "integer" *
  • "boolean" *
  • "array" *
* @return This object (for method chaining). */ public HeaderInfo setType(String value) { if (isStrict() && ! contains(value, VALID_TYPES)) throw new FormattedRuntimeException( "Invalid value passed in to setType(String). Value=''{0}'', valid values={1}", value, VALID_TYPES ); type = value; return this; } /** * Same as {@link #setType(String)}. * * @param value * The new value for this property. *
Non-String values will be converted to String using toString(). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo type(Object value) { return setType(toStringVal(value)); } /** * Bean property getter: format. * *

* The extending format for the previously mentioned type. * *

See Also:
*
    *
* * @return The property value, or null if it is not set. */ public String getFormat() { return format; } /** * Bean property setter: format. * *

* The extending format for the previously mentioned type. * *

See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setFormat(String value) { format = value; return this; } /** * Same as {@link #setFormat(String)}. * * @param value * The new value for this property. *
Non-String values will be converted to String using toString(). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo format(Object value) { return setFormat(toStringVal(value)); } /** * Bean property getter: items. * *

* Describes the type of items in the array. * * @return The property value, or null if it is not set. */ public Items getItems() { return items; } /** * Bean property setter: items. * *

* Describes the type of items in the array. * * @param value * The new value for this property. *
Property value is required if type is "array". *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setItems(Items value) { items = value; return this; } /** * Same as {@link #setItems(Items)}. * * @param value * The new value for this property. *
Valid types: *

    *
  • {@link Items} *
  • String - JSON object representation of {@link Items} *
    Example:
    *

    * items("{type:'type',format:'format',...}"); *

    *
*
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo items(Object value) { return setItems(toType(value, Items.class)); } /** * Bean property getter: collectionFormat. * *

* Determines the format of the array if type array is used. * * @return The property value, or null if it is not set. */ public String getCollectionFormat() { return collectionFormat; } /** * Bean property setter: collectionFormat. * *

* Determines the format of the array if type array is used. * * @param value * The new value for this property. *
Valid values: *

    *
  • "csv" (default) - comma separated values foo,bar. *
  • "ssv" - space separated values foo bar. *
  • "tsv" - tab separated values foo\tbar. *
  • "pipes" - pipe separated values foo|bar. *
* @return This object (for method chaining). */ public HeaderInfo setCollectionFormat(String value) { if (isStrict() && ! contains(value, VALID_COLLECTION_FORMATS)) throw new FormattedRuntimeException( "Invalid value passed in to setCollectionFormat(String). Value=''{0}'', valid values={1}", value, VALID_COLLECTION_FORMATS ); collectionFormat = value; return this; } /** * Same as {@link #setCollectionFormat(String)}. * * @param value * The new value for this property. *
Non-String values will be converted to String using toString(). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo collectionFormat(Object value) { return setCollectionFormat(toStringVal(value)); } /** * Bean property getter: default. * *

* Declares the value of the header that the server will use if none is provided. * *

Notes:
*
    *
  • * "default" has no meaning for required items. *
  • * Unlike JSON Schema this value MUST conform to the defined type for the header. *
* *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Object getDefault() { return _default; } /** * Bean property setter: default. * *

* Declares the value of the header that the server will use if none is provided. * *

Notes:
*
    *
  • * "default" has no meaning for required items. *
  • * Unlike JSON Schema this value MUST conform to the defined type for the header. *
* *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setDefault(Object value) { _default = value; return this; } /** * Same as {@link #setDefault(Object)}. * * @param value The new value for this property. * @return This object (for method chaining). */ public HeaderInfo _default(Object value) { return setDefault(value); } /** * Bean property getter: maximum. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Number getMaximum() { return maximum; } /** * Bean property setter: maximum. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMaximum(Number value) { maximum = value; return this; } /** * Same as {@link #setMaximum(Number)}. * * @param value * The new value for this property. *
Non-Number values will be converted to Number using toString() then best number match. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo maximum(Object value) { return setMaximum(toNumber(value)); } /** * Bean property getter: exclusiveMaximum. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Boolean getExclusiveMaximum() { return exclusiveMaximum; } /** * Bean property setter: exclusiveMaximum. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setExclusiveMaximum(Boolean value) { exclusiveMaximum = value; return this; } /** * Same as {@link #setExclusiveMaximum(Boolean)}. * * @param value * The new value for this property. *
Non-boolean values will be converted to boolean using Boolean.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo exclusiveMaximum(Object value) { return setExclusiveMaximum(toBoolean(value)); } /** * Bean property getter: minimum. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Number getMinimum() { return minimum; } /** * Bean property setter: minimum. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMinimum(Number value) { minimum = value; return this; } /** * Same as {@link #setMinimum(Number)}. * * @param value * The new value for this property. *
Non-Number values will be converted to Number using toString() then best number match. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo minimum(Object value) { return setMinimum(toNumber(value)); } /** * Bean property getter: exclusiveMinimum. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Boolean getExclusiveMinimum() { return exclusiveMinimum; } /** * Bean property setter: exclusiveMinimum. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setExclusiveMinimum(Boolean value) { exclusiveMinimum = value; return this; } /** * Same as {@link #setExclusiveMinimum(Boolean)}. * * @param value * The new value for this property. *
Non-boolean values will be converted to boolean using Boolean.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo exclusiveMinimum(Object value) { return setExclusiveMinimum(toBoolean(value)); } /** * Bean property getter: maxLength. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Integer getMaxLength() { return maxLength; } /** * Bean property setter: maxLength. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMaxLength(Integer value) { maxLength = value; return this; } /** * Same as {@link #setMaxLength(Integer)}. * * @param value * The new value for this property. *
Non-Integer values will be converted to Integer using Integer.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo maxLength(Object value) { return setMaxLength(toInteger(value)); } /** * Bean property getter: minLength. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Integer getMinLength() { return minLength; } /** * Bean property setter: minLength. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMinLength(Integer value) { minLength = value; return this; } /** * Same as {@link #setMinLength(Integer)}. * * @param value * The new value for this property. *
Non-Integer values will be converted to Integer using Integer.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo minLength(Object value) { return setMinLength(toInteger(value)); } /** * Bean property getter: pattern. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public String getPattern() { return pattern; } /** * Bean property setter: pattern. * *

* This string SHOULD be a valid regular expression. * *

See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setPattern(String value) { pattern = value; return this; } /** * Same as {@link #setPattern(String)}. * * @param value * The new value for this property. *
Non-String values will be converted to String using toString(). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo pattern(Object value) { return setPattern(toStringVal(value)); } /** * Bean property getter: maxItems. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Integer getMaxItems() { return maxItems; } /** * Bean property setter: maxItems. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMaxItems(Integer value) { maxItems = value; return this; } /** * Same as {@link #setMaxItems(Integer)}. * * @param value * The new value for this property. *
Non-Integer values will be converted to Integer using Integer.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo maxItems(Object value) { return setMaxItems(toInteger(value)); } /** * Bean property getter: minItems. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Integer getMinItems() { return minItems; } /** * Bean property setter: minItems. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMinItems(Integer value) { minItems = value; return this; } /** * Same as {@link #setMinItems(Integer)}. * * @param value * The new value for this property. *
Non-Integer values will be converted to Integer using Integer.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo minItems(Object value) { return setMinItems(toInteger(value)); } /** * Bean property getter: uniqueItems. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Boolean getUniqueItems() { return uniqueItems; } /** * Bean property setter: uniqueItems. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setUniqueItems(Boolean value) { uniqueItems = value; return this; } /** * Same as {@link #setUniqueItems(Boolean)}. * * @param value * The new value for this property. *
Non-boolean values will be converted to boolean using Boolean.valueOf(value.toString()). *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo uniqueItems(Object value) { return setUniqueItems(toBoolean(value)); } /** * Bean property getter: enum. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public List getEnum() { return _enum; } /** * Bean property setter: enum. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setEnum(Collection value) { _enum = newList(value); return this; } /** * Adds one or more values to the enum property. * * @param values * The values to add to this property. *
Ignored if null. * @return This object (for method chaining). */ public HeaderInfo addEnum(Collection values) { _enum = addToList(_enum, values); return this; } /** * Adds one or more values to the enum property. * * @param values * The values to add to this property. *
Valid types: *
    *
  • Object *
  • Collection<Object> *
  • String - JSON array representation of Collection<Object>. *
    Example:
    *

    * _enum("['foo','bar']"); *

    *
  • String - Individual values. *
    Example:
    *

    * _enum("foo", "bar"); *

    *
*
Ignored if null. * @return This object (for method chaining). */ public HeaderInfo _enum(Object...values) { _enum = addToList(_enum, values, Object.class); return this; } /** * Bean property getter: multipleOf. * *
See Also:
*
    *
* * @return The property value, or null if it is not set. */ public Number getMultipleOf() { return multipleOf; } /** * Bean property setter: multipleOf. * *
See Also:
*
    *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo setMultipleOf(Number value) { multipleOf = value; return this; } /** * Same as {@link #setMultipleOf(Number)}. * * @param value * The new value for this property. *
Non-Number values will be converted to Number using toString() then best number match. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo multipleOf(Object value) { return setMultipleOf(toNumber(value)); } /** * Bean property getter: $ref. * * @return The property value, or null if it is not set. */ @BeanProperty("$ref") public String getRef() { return ref; } /** * Returns true if this object has a "$ref" attribute. * * @return true if this object has a "$ref" attribute. */ public boolean hasRef() { return ref != null; } /** * Bean property setter: $ref. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ @BeanProperty("$ref") public HeaderInfo setRef(Object value) { ref = StringUtils.asString(value); return this; } /** * Same as {@link #setRef(Object)}. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo ref(Object value) { return setRef(value); } /** * Bean property getter: x-example. * * @return The property value, or null if it is not set. */ @BeanProperty("x-example") public Object getExample() { return example; } /** * Bean property setter: examples. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ @BeanProperty("x-example") public HeaderInfo setExample(Object value) { example = value; return this; } /** * Bean property setter: examples. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object (for method chaining). */ public HeaderInfo example(Object value) { example = value; return this; } @Override /* SwaggerElement */ public T get(String property, Class type) { if (property == null) return null; switch (property) { case "description": return (T)getDescription(); case "type": return toType(getType(), type); case "format": return toType(getFormat(), type); case "items": return toType(getItems(), type); case "collectionFormat": return toType(getCollectionFormat(), type); case "$ref": return toType(getRef(), type); case "default": return toType(getDefault(), type); case "maximum": return toType(getMaximum(), type); case "exclusiveMaximum": return toType(getExclusiveMaximum(), type); case "minimum": return toType(getMinimum(), type); case "exclusiveMinimum": return toType(getExclusiveMinimum(), type); case "maxLength": return toType(getMaxLength(), type); case "minLength": return toType(getMinLength(), type); case "pattern": return toType(getPattern(), type); case "maxItems": return toType(getMaxItems(), type); case "minItems": return toType(getMinItems(), type); case "uniqueItems": return toType(getUniqueItems(), type); case "enum": return toType(getEnum(), type); case "multipleOf": return toType(getMultipleOf(), type); case "x-example": return toType(getExample(), type); default: return super.get(property, type); } } @Override /* SwaggerElement */ public HeaderInfo set(String property, Object value) { if (property == null) return this; switch (property) { case "description": return description(value); case "type": return type(value); case "format": return format(value); case "items": return items(value); case "collectionFormat": return collectionFormat(value); case "$ref": return ref(value); case "default": return _default(value); case "maximum": return maximum(value); case "exclusiveMaximum": return exclusiveMaximum(value); case "minimum": return minimum(value); case "exclusiveMinimum": return exclusiveMinimum(value); case "maxLength": return maxLength(value); case "minLength": return minLength(value); case "pattern": return pattern(value); case "maxItems": return maxItems(value); case "minItems": return minItems(value); case "uniqueItems": return uniqueItems(value); case "enum": return setEnum(null)._enum(value); case "multipleOf": return multipleOf(value); case "x-example": return example(value); default: super.set(property, value); return this; } } @Override /* SwaggerElement */ public Set keySet() { ASet s = new ASet() .appendIf(description != null, "description") .appendIf(type != null, "type") .appendIf(format != null, "format") .appendIf(items != null, "items") .appendIf(collectionFormat != null, "collectionFormat") .appendIf(ref != null, "$ref") .appendIf(_default != null, "default") .appendIf(maximum != null, "maximum") .appendIf(exclusiveMaximum != null, "exclusiveMaximum") .appendIf(minimum != null, "minimum") .appendIf(exclusiveMinimum != null, "exclusiveMinimum") .appendIf(maxLength != null, "maxLength") .appendIf(minLength != null, "minLength") .appendIf(pattern != null, "pattern") .appendIf(maxItems != null, "maxItems") .appendIf(minItems != null, "minItems") .appendIf(uniqueItems != null, "uniqueItems") .appendIf(_enum != null, "enum") .appendIf(multipleOf != null, "multipleOf") .appendIf(example != null, "example"); return new MultiSet<>(s, super.keySet()); } /** * Resolves any "$ref" attributes in this element. * * @param swagger The swagger document containing the definitions. * @param refStack Keeps track of previously-visited references so that we don't cause recursive loops. * @param maxDepth * The maximum depth to resolve references. *
After that level is reached, $ref references will be left alone. *
Useful if you have very complex models and you don't want your swagger page to be overly-complex. * @return * This object with references resolved. *
May or may not be the same object. */ public HeaderInfo resolveRefs(Swagger swagger, Deque refStack, int maxDepth) { if (ref != null) { if (refStack.contains(ref) || refStack.size() >= maxDepth) return this; refStack.addLast(ref); HeaderInfo r = swagger.findRef(ref, HeaderInfo.class).resolveRefs(swagger, refStack, maxDepth); refStack.removeLast(); return r; } if (items != null) items = items.resolveRefs(swagger, refStack, maxDepth); return this; } }