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

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

// ***************************************************************************************************************************
// * 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.common.internal.StringUtils.*;
import static org.apache.juneau.internal.CollectionUtils.*;
import static org.apache.juneau.internal.ConverterUtils.*;

import java.util.*;

import org.apache.juneau.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.marshaller.*;

/**
 * A limited subset of JSON-Schema's items object.
 *
 * 

* It is used by parameter definitions that are not located in "body". * *

Example:
*

* // Construct using SwaggerBuilder. * Items items = items("string").minLength(2); * * // Serialize using JsonSerializer. * String json = JsonSerializer.DEFAULT.toString(items); * * // Or just use toString() which does the same as above. * json = items.toString(); *

*

* // Output * { * "type": "string", * "minLength": 2 * } *

* *
See Also:
*/ @Bean(properties="type,format,items,collectionFormat,default,maximum,exclusiveMaximum,minimum,exclusiveMinimum,maxLength,minLength,pattern,maxItems,minItems,uniqueItems,enum,multipleOf,$ref,*") @FluentSetters public class Items 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 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 Set _enum; /** * Default constructor. */ public Items() {} /** * Copy constructor. * * @param copyFrom The object to copy. */ public Items(Items copyFrom) { super(copyFrom); this.collectionFormat = copyFrom.collectionFormat; this._default = copyFrom._default; this._enum = copyOf(copyFrom._enum); this.exclusiveMaximum = copyFrom.exclusiveMaximum; this.exclusiveMinimum = copyFrom.exclusiveMinimum; this.format = copyFrom.format; this.items = copyFrom.items == null ? null : copyFrom.items.copy(); this.maximum = copyFrom.maximum; this.maxItems = copyFrom.maxItems; this.maxLength = copyFrom.maxLength; this.minimum = copyFrom.minimum; this.minItems = copyFrom.minItems; this.minLength = copyFrom.minLength; this.multipleOf = copyFrom.multipleOf; this.pattern = copyFrom.pattern; this.ref = copyFrom.ref; this.type = copyFrom.type; this.uniqueItems = copyFrom.uniqueItems; } /** * Make a deep copy of this object. * * @return A deep copy of this object. */ public Items copy() { return new Items(this); } @Override /* SwaggerElement */ protected Items strict() { super.strict(); return this; } //----------------------------------------------------------------------------------------------------------------- // Properties //----------------------------------------------------------------------------------------------------------------- /** * 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. *
*
Can be null to unset the property. * @return This object. */ public Items setCollectionFormat(String value) { if (isStrict() && ! ArrayUtils.contains(value, VALID_COLLECTION_FORMATS)) throw new BasicRuntimeException( "Invalid value passed in to setCollectionFormat(String). Value=''{0}'', valid values={1}", value, Json5.of(VALID_COLLECTION_FORMATS) ); collectionFormat = value; return this; } /** * Bean property getter: default. * *

* Declares the value of the item 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 data type. *
* * @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 item 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 data type. *
* * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setDefault(Object value) { _default = value; return this; } /** * Bean property getter: enum. * * @return The property value, or null if it is not set. */ public Set getEnum() { return _enum; } /** * Bean property setter: enum. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setEnum(Collection value) { _enum = setFrom(value); return this; } /** * Bean property setter: enum. * * @param value * The new value for this property. * @return This object. */ public Items setEnum(Object...value) { return setEnum(Arrays.asList(value)); } /** * Bean property fluent setter: enum. * * @param value * The new value for this property. *
String values can be JSON arrays. * @return This object. */ public Items addEnum(Object...value) { setEnum(setBuilder(_enum).sparse().add(value).build()); return this; } /** * Bean property getter: exclusiveMaximum. * * @return The property value, or null if it is not set. */ public Boolean getExclusiveMaximum() { return exclusiveMaximum; } /** * Bean property setter: exclusiveMaximum. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setExclusiveMaximum(Boolean value) { exclusiveMaximum = value; return this; } /** * Bean property getter: exclusiveMinimum. * * @return The property value, or null if it is not set. */ public Boolean getExclusiveMinimum() { return exclusiveMinimum; } /** * Bean property setter: exclusiveMinimum. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setExclusiveMinimum(Boolean value) { exclusiveMinimum = value; return this; } /** * Bean property getter: format. * *

* The extending format for the previously mentioned type. * * @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. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setFormat(String value) { format = value; return this; } /** * 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. */ public Items setItems(Items value) { items = value; return this; } /** * Bean property getter: maximum. * * @return The property value, or null if it is not set. */ public Number getMaximum() { return maximum; } /** * Bean property setter: maximum. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMaximum(Number value) { maximum = value; return this; } /** * Bean property getter: maxItems. * * @return The property value, or null if it is not set. */ public Integer getMaxItems() { return maxItems; } /** * Bean property setter: maxItems. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMaxItems(Integer value) { maxItems = value; return this; } //----------------------------------------------------------------------------------------------------------------- // maxLength //----------------------------------------------------------------------------------------------------------------- /** * Bean property getter: maxLength. * * @return The property value, or null if it is not set. */ public Integer getMaxLength() { return maxLength; } /** * Bean property setter: maxLength. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMaxLength(Integer value) { maxLength = value; return this; } /** * Bean property getter: minimum. * * @return The property value, or null if it is not set. */ public Number getMinimum() { return minimum; } /** * Bean property setter: minimum. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMinimum(Number value) { minimum = value; return this; } /** * Bean property getter: minItems. * * @return The property value, or null if it is not set. */ public Integer getMinItems() { return minItems; } /** * Bean property setter: minItems. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMinItems(Integer value) { minItems = value; return this; } /** * Bean property getter: minLength. * * @return The property value, or null if it is not set. */ public Integer getMinLength() { return minLength; } /** * Bean property setter: minLength. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMinLength(Integer value) { minLength = value; return this; } /** * Bean property getter: multipleOf. * * @return The property value, or null if it is not set. */ public Number getMultipleOf() { return multipleOf; } /** * Bean property setter: multipleOf. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setMultipleOf(Number value) { multipleOf = value; return this; } /** * Bean property getter: pattern. * * @return The property value, or null if it is not set. */ public String getPattern() { return pattern; } /** * Bean property setter: pattern. * * @param value * The new value for this property. *
This string SHOULD be a valid regular expression. *
Can be null to unset the property. * @return This object. */ public Items setPattern(String value) { pattern = value; return this; } /** * Bean property getter: $ref. * * @return The property value, or null if it is not set. */ @Beanp("$ref") public String getRef() { return ref; } /** * Bean property setter: $ref. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ @Beanp("$ref") public Items setRef(String value) { ref = value; return this; } /** * Bean property getter: type. * *

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

* The internal type of the array. * * @param value * The new value for this property. *
Valid values: *

    *
  • "string" *
  • "number" *
  • "integer" *
  • "boolean" *
  • "array" *
*
Property value is required. * @return This object. */ public Items setType(String value) { if (isStrict() && ! ArrayUtils.contains(value, VALID_TYPES)) throw new BasicRuntimeException("Invalid value passed in to setType(String). Value=''{0}'', valid values={1}", value, Json5.of(VALID_TYPES)); type = value; return this; } /** * Bean property getter: uniqueItems. * * @return The property value, or null if it is not set. */ public Boolean getUniqueItems() { return uniqueItems; } /** * Bean property setter: uniqueItems. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public Items setUniqueItems(Boolean value) { uniqueItems = value; return this; } // // @Override /* SwaggerElement */ public T get(String property, Class type) { if (property == null) return null; switch (property) { case "collectionFormat": return toType(getCollectionFormat(), type); case "default": return toType(getDefault(), type); case "enum": return toType(getEnum(), type); case "exclusiveMaximum": return toType(getExclusiveMaximum(), type); case "exclusiveMinimum": return toType(getExclusiveMinimum(), type); case "format": return toType(getFormat(), type); case "items": return toType(getItems(), type); case "maximum": return toType(getMaximum(), type); case "maxItems": return toType(getMaxItems(), type); case "maxLength": return toType(getMaxLength(), type); case "minimum": return toType(getMinimum(), type); case "minItems": return toType(getMinItems(), type); case "minLength": return toType(getMinLength(), type); case "multipleOf": return toType(getMultipleOf(), type); case "pattern": return toType(getPattern(), type); case "$ref": return toType(getRef(), type); case "type": return toType(getType(), type); case "uniqueItems": return toType(getUniqueItems(), type); default: return super.get(property, type); } } @Override /* SwaggerElement */ public Items set(String property, Object value) { if (property == null) return this; switch (property) { case "collectionFormat": return setCollectionFormat(stringify(value)); case "default": return setDefault(value); case "enum": return setEnum(listBuilder(Object.class).sparse().addAny(value).build()); case "exclusiveMaximum": return setExclusiveMaximum(toBoolean(value)); case "exclusiveMinimum": return setExclusiveMinimum(toBoolean(value)); case "format": return setFormat(stringify(value)); case "items": return setItems(toType(value,Items.class)); case "maximum": return setMaximum(toNumber(value)); case "maxItems": return setMaxItems(toInteger(value)); case "maxLength": return setMaxLength(toInteger(value)); case "minimum": return setMinimum(toNumber(value)); case "minItems": return setMinItems(toInteger(value)); case "minLength": return setMinLength(toInteger(value)); case "multipleOf": return setMultipleOf(toNumber(value)); case "pattern": return setPattern(stringify(value)); case "$ref": return setRef(stringify(value)); case "type": return setType(stringify(value)); case "uniqueItems": return setUniqueItems(toBoolean(value)); default: super.set(property, value); return this; } } @Override /* SwaggerElement */ public Set keySet() { Set s = setBuilder(String.class) .addIf(collectionFormat != null, "collectionFormat") .addIf(_default != null, "default") .addIf(_enum != null, "enum") .addIf(exclusiveMaximum != null, "exclusiveMaximum") .addIf(exclusiveMinimum != null, "exclusiveMinimum") .addIf(format != null, "format") .addIf(items != null, "items") .addIf(maximum != null, "maximum") .addIf(maxItems != null, "maxItems") .addIf(maxLength != null, "maxLength") .addIf(minimum != null, "minimum") .addIf(minItems != null, "minItems") .addIf(minLength != null, "minLength") .addIf(multipleOf != null, "multipleOf") .addIf(pattern != null, "pattern") .addIf(ref != null, "$ref") .addIf(type != null, "type") .addIf(uniqueItems != null, "uniqueItems") .build(); 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 Items resolveRefs(Swagger swagger, Deque refStack, int maxDepth) { if (ref != null) { if (refStack.contains(ref) || refStack.size() >= maxDepth) return this; refStack.addLast(ref); Items r = swagger.findRef(ref, Items.class).resolveRefs(swagger, refStack, maxDepth); refStack.removeLast(); return r; } set("properties", resolveRefs(get("properties"), swagger, refStack, maxDepth)); if (items != null) items = items.resolveRefs(swagger, refStack, maxDepth); set("example", null); return this; } /* Resolve references in extra attributes */ private Object resolveRefs(Object o, Swagger swagger, Deque refStack, int maxDepth) { if (o instanceof JsonMap) { JsonMap om = (JsonMap)o; Object ref = om.get("$ref"); if (ref instanceof CharSequence) { String sref = ref.toString(); if (refStack.contains(sref) || refStack.size() >= maxDepth) return o; refStack.addLast(sref); Object o2 = swagger.findRef(sref, Object.class); o2 = resolveRefs(o2, swagger, refStack, maxDepth); refStack.removeLast(); return o2; } om.entrySet().forEach(x -> x.setValue(resolveRefs(x.getValue(), swagger, refStack, maxDepth))); } if (o instanceof JsonList) for (ListIterator li = ((JsonList)o).listIterator(); li.hasNext();) li.set(resolveRefs(li.next(), swagger, refStack, maxDepth)); return o; } }