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

org.apache.juneau.dto.swagger.SchemaInfo 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.annotation.*;
import org.apache.juneau.internal.*;

/**
 * The Schema Object allows the definition of input and output data types.
 *
 * 

* These types can be objects, but also primitives and arrays. * This object is based on the JSON Schema Specification Draft 4 and uses a predefined subset of it. * On top of this subset, there are extensions provided by this specification to allow for more complete documentation. * *

* Further information about the properties can be found in JSON Schema Core and JSON Schema Validation. * Unless stated otherwise, the property definitions follow the JSON Schema specification as referenced here. * *

Example:
*

* // Construct using SwaggerBuilder. * SchemaInfo info = schemaInfo() * .type("string") * .title("foo") * * // Serialize using JsonSerializer. * String json = JsonSerializer.DEFAULT.toString(info); * * // Or just use toString() which does the same as above. * json = info.toString(); *

*

* // Output * { * "type": "string", * "title": "foo" * } *

* *
See Also:
*/ @Bean(properties="format,title,description,default,multipleOf,maximum,exclusiveMaximum,minimum,exclusiveMinimum,maxLength,minLength,pattern,maxItems,minItems,uniqueItems,maxProperties,minProperties,required,requiredProperties,enum,type,items,allOf,properties,additionalProperties,discriminator,readOnly,xml,externalDocs,example,$ref,*") @FluentSetters public class SchemaInfo extends SwaggerElement { private String format, title, description, pattern, type, discriminator, ref; private Number multipleOf, maximum, minimum; private Integer maxLength, minLength, maxItems, minItems, maxProperties, minProperties; private Boolean exclusiveMaximum, exclusiveMinimum, uniqueItems, readOnly, required; private Object _default, example; private Items items; private Xml xml; private ExternalDocumentation externalDocs; private Set _enum, allOf; private Set requiredProperties; private Map properties; private SchemaInfo additionalProperties; /** * Default constructor. */ public SchemaInfo() {} /** * Copy constructor. * * @param copyFrom The object to copy. */ public SchemaInfo(SchemaInfo copyFrom) { super(copyFrom); this.additionalProperties = copyFrom.additionalProperties == null ? null : copyFrom.additionalProperties.copy(); this.allOf = copyOf(copyFrom.allOf); this._default = copyFrom._default; this.description = copyFrom.description; this.discriminator = copyFrom.discriminator; this._enum = copyOf(copyFrom._enum); this.example = copyFrom.example; this.exclusiveMaximum = copyFrom.exclusiveMaximum; this.exclusiveMinimum = copyFrom.exclusiveMinimum; this.externalDocs = copyFrom.externalDocs == null ? null : copyFrom.externalDocs.copy(); 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.maxProperties = copyFrom.maxProperties; this.minimum = copyFrom.minimum; this.minItems = copyFrom.minItems; this.minLength = copyFrom.minLength; this.minProperties = copyFrom.minProperties; this.multipleOf = copyFrom.multipleOf; this.pattern = copyFrom.pattern; this.readOnly = copyFrom.readOnly; this.ref = copyFrom.ref; this.required = copyFrom.required; this.requiredProperties = copyOf(copyFrom.requiredProperties); this.title = copyFrom.title; this.type = copyFrom.type; this.uniqueItems = copyFrom.uniqueItems; this.xml = copyFrom.xml == null ? null : copyFrom.xml.copy(); if (copyFrom.properties == null) { this.properties = null; } else { this.properties = map(); copyFrom.properties.forEach((k,v) -> this.properties.put(k, v.copy())); } } /** * Make a deep copy of this object. * * @return A deep copy of this object. */ public SchemaInfo copy() { return new SchemaInfo(this); } //----------------------------------------------------------------------------------------------------------------- // Properties //----------------------------------------------------------------------------------------------------------------- /** * Bean property getter: additionalProperties. * * @return The property value, or null if it is not set. */ public SchemaInfo getAdditionalProperties() { return additionalProperties; } /** * Bean property setter: additionalProperties. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setAdditionalProperties(SchemaInfo value) { additionalProperties = value; return this; } /** * Bean property getter: allOf. * * @return The property value, or null if it is not set. */ public Set getAllOf() { return allOf; } /** * Bean property setter: allOf. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setAllOf(Collection value) { allOf = setFrom(value); return this; } /** * Bean property appender: allOf. * * @param values * The values to add to this property. *
Ignored if null. * @return This object. */ public SchemaInfo addAllOf(Object...values) { allOf = setBuilder(allOf).sparse().add(values).build(); return this; } /** * Bean property fluent setter: allOf. * * @param value * The new value for this property. *
Strings can contains JSON arrays. *
Valid types: * @return This object. */ public SchemaInfo setAllOf(Object...value) { setAllOf(setBuilder(Object.class).sparse().addAny(value).build()); return this; } /** * Bean property getter: default. * *

* Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object. * * @return The property value, or null if it is not set. */ public Object getDefault() { return _default; } /** * Bean property setter: default. * *

* Unlike JSON Schema, the value MUST conform to the defined type for the Schema Object. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setDefault(Object value) { _default = value; return this; } /** * Bean property getter: description. * * @return The property value, or null if it is not set. */ public String getDescription() { return description; } /** * Bean property setter: description. * * @param value * The new value for this property. *
GFM syntax can be used for rich text representation. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setDescription(String value) { description = value; return this; } /** * Bean property getter: discriminator. * * @return The property value, or null if it is not set. */ public String getDiscriminator() { return discriminator; } /** * Bean property setter: discriminator. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setDiscriminator(String value) { discriminator = 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 SchemaInfo setEnum(Collection value) { _enum = setFrom(value); return this; } /** * Bean property appender: enum. * * @param value * The values to add to this property. *
Ignored if null. * @return This object. */ public SchemaInfo addEnum(Object...value) { _enum = setBuilder(_enum).sparse().add(value).build(); return this; } /** * Bean property fluent setter: enum. * * @param value * The new value for this property. *
Strings can be JSON arrays. * @return This object. */ public SchemaInfo setEnum(Object...value) { setEnum(setBuilder(Object.class).sparse().addAny(value).build()); return this; } /** * Bean property getter: example. * * @return The property value, or null if it is not set. */ public Object getExample() { return example; } /** * Bean property setter: example. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setExample(Object value) { example = value; 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 SchemaInfo 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 SchemaInfo setExclusiveMinimum(Boolean value) { exclusiveMinimum = value; return this; } /** * Bean property getter: externalDocs. * * @return The property value, or null if it is not set. */ public ExternalDocumentation getExternalDocs() { return externalDocs; } /** * Bean property setter: externalDocs. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setExternalDocs(ExternalDocumentation value) { externalDocs = value; return this; } /** * Bean property getter: format. * * @return The property value, or null if it is not set. */ public String getFormat() { return format; } /** * Bean property setter: format. * * @param value * The new value for this property. *
Can be null to unset the property. *
Formats defined by the OAS include: *
    *
  • "int32" *
  • "int64" *
  • "float" *
  • "double" *
  • "byte" *
  • "binary" *
  • "date" *
  • "date-time" *
  • "password" *
* @return This object. */ public SchemaInfo setFormat(String value) { format = value; return this; } /** * Bean property getter: items. * * @return The property value, or null if it is not set. */ public Items getItems() { return items; } /** * Bean property setter: items. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo 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 SchemaInfo 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 SchemaInfo setMaxItems(Integer value) { maxItems = value; return this; } /** * 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 SchemaInfo setMaxLength(Integer value) { maxLength = value; return this; } /** * Bean property getter: maxProperties. * * @return The property value, or null if it is not set. */ public Integer getMaxProperties() { return maxProperties; } /** * Bean property setter: maxProperties. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setMaxProperties(Integer value) { maxProperties = 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 SchemaInfo 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 SchemaInfo 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 SchemaInfo setMinLength(Integer value) { minLength = value; return this; } /** * Bean property getter: minProperties. * * @return The property value, or null if it is not set. */ public Integer getMinProperties() { return minProperties; } /** * Bean property setter: minProperties. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setMinProperties(Integer value) { minProperties = 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 SchemaInfo 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 SchemaInfo setPattern(String value) { pattern = value; return this; } /** * Bean property getter: properties. * * @return The property value, or null if it is not set. */ public Map getProperties() { return properties; } /** * Bean property setter: properties. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setProperties(Map value) { properties = copyOf(value); return this; } /** * Bean property appender: properties. * * @param key The property key. * @param value The property value. * @return This object. */ public SchemaInfo addProperty(String key, SchemaInfo value) { properties = mapBuilder(properties).sparse().add(key, value).build(); return this; } /** * Bean property getter: readOnly. * * @return The property value, or null if it is not set. */ public Boolean getReadOnly() { return readOnly; } /** * Bean property setter: readOnly. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setReadOnly(Boolean value) { readOnly = 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 SchemaInfo setRef(String value) { ref = value; return this; } /** * Bean property getter: required. * * @return The property value, or null if it is not set. */ public Boolean getRequired() { return required; } /** * Bean property setter: required. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setRequired(Boolean value) { required = value; return this; } /** * Bean property getter: requiredProperties. * *

* The list of required properties. * * @return The property value, or null if it is not set. */ public Set getRequiredProperties() { return requiredProperties; } /** * Bean property setter: requiredProperties. * *

* The list of required properties. * * @param value * The new value for this property. *
Valid values: *

    *
  • "http" *
  • "https" *
  • "ws" *
  • "wss" *
*
Can be null to unset the property. * @return This object. */ public SchemaInfo setRequiredProperties(Collection value) { requiredProperties = setFrom(value); return this; } /** * Bean property appender: requiredProperties. * *

* The list of required properties. * * @param value * The values to add to this property. *
Ignored if null. * @return This object. */ public SchemaInfo addRequiredProperties(String...value) { requiredProperties = setBuilder(requiredProperties).sparse().add(value).build(); return this; } /** * Bean property fluent setter: requiredProperties. * * @param value * The new value for this property. * @return This object. */ public SchemaInfo setRequiredProperties(String...value) { setRequiredProperties(setBuilder(String.class).sparse().addJson(value).build()); return this; } /** * Bean property getter: title. * * @return The property value, or null if it is not set. */ public String getTitle() { return title; } /** * Bean property setter: title. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setTitle(String value) { title = value; return this; } /** * Bean property getter: type. * * @return The property value, or null if it is not set. */ public String getType() { return type; } /** * Bean property setter: type. * * @param value * The new value for this property. *
Can be null to unset the property. *
Possible values include: *

    *
  • "object" *
  • "string" *
  • "number" *
  • "integer" *
  • "boolean" *
  • "array" *
  • "file" *
* @return This object. */ public SchemaInfo setType(String value) { 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 SchemaInfo setUniqueItems(Boolean value) { uniqueItems = value; return this; } /** * Bean property getter: xml. * * @return The property value, or null if it is not set. */ public Xml getXml() { return xml; } /** * Bean property setter: xml. * * @param value * The new value for this property. *
Can be null to unset the property. * @return This object. */ public SchemaInfo setXml(Xml value) { xml = value; return this; } // // @Override /* SwaggerElement */ public T get(String property, Class type) { if (property == null) return null; switch (property) { case "additionalProperties": return toType(getAdditionalProperties(), type); case "allOf": return toType(getAllOf(), type); case "default": return toType(getDefault(), type); case "description": return toType(getDescription(), type); case "discriminator": return toType(getDiscriminator(), type); case "enum": return toType(getEnum(), type); case "example": return toType(getExample(), type); case "exclusiveMaximum": return toType(getExclusiveMaximum(), type); case "exclusiveMinimum": return toType(getExclusiveMinimum(), type); case "externalDocs": return toType(getExternalDocs(), 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 "maxProperties": return toType(getMaxProperties(), type); case "minimum": return toType(getMinimum(), type); case "minItems": return toType(getMinItems(), type); case "minLength": return toType(getMinLength(), type); case "minProperties": return toType(getMinProperties(), type); case "multipleOf": return toType(getMultipleOf(), type); case "pattern": return toType(getPattern(), type); case "properties": return toType(getProperties(), type); case "readOnly": return toType(getReadOnly(), type); case "$ref": return toType(getRef(), type); case "rquired": return toType(getRequired(), type); case "requiredProperties": return toType(getRequiredProperties(), type); case "title": return toType(getTitle(), type); case "type": return toType(getType(), type); case "uniqueItems": return toType(getUniqueItems(), type); case "xml": return toType(getXml(), type); default: return super.get(property, type); } } @Override /* SwaggerElement */ public SchemaInfo set(String property, Object value) { if (property == null) return this; switch (property) { case "additionalProperties": return setAdditionalProperties(toType(value, SchemaInfo.class)); case "allOf": return setAllOf(value); case "default": return setDefault(value); case "description": return setDescription(stringify(value)); case "discriminator": return setDiscriminator(stringify(value)); case "enum": return setEnum(value); case "example": return setExample(value); case "exclusiveMaximum": return setExclusiveMaximum(toBoolean(value)); case "exclusiveMinimum": return setExclusiveMinimum(toBoolean(value)); case "externalDocs": return setExternalDocs(toType(value, ExternalDocumentation.class)); 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 "maxProperties": return setMaxProperties(toInteger(value)); case "minimum": return setMinimum(toNumber(value)); case "minItems": return setMinItems(toInteger(value)); case "minLength": return setMinLength(toInteger(value)); case "minProperties": return setMinProperties(toInteger(value)); case "multipleOf": return setMultipleOf(toNumber(value)); case "pattern": return setPattern(stringify(value)); case "properties": return setProperties(mapBuilder(String.class,SchemaInfo.class).sparse().addAny(value).build()); case "readOnly": return setReadOnly(toBoolean(value)); case "$ref": return setRef(stringify(value)); case "required": return setRequired(toBoolean(value)); case "requiredProperties": return setRequiredProperties(listBuilder(String.class).sparse().addAny(value).build()); case "title": return setTitle(stringify(value)); case "type": return setType(stringify(value)); case "uniqueItems": return setUniqueItems(toBoolean(value)); case "xml": return setXml(toType(value, Xml.class)); default: super.set(property, value); return this; } } @Override /* SwaggerElement */ public Set keySet() { Set s = setBuilder(String.class) .addIf(additionalProperties != null, "additionalProperties") .addIf(allOf != null, "allOf") .addIf(_default != null, "default") .addIf(description != null, "description") .addIf(discriminator != null, "discriminator") .addIf(_enum != null, "enum") .addIf(example != null, "example") .addIf(exclusiveMaximum != null, "exclusiveMaximum") .addIf(exclusiveMinimum != null, "exclusiveMinimum") .addIf(externalDocs != null, "externalDocs") .addIf(format != null, "format") .addIf(items != null, "items") .addIf(maximum != null, "maximum") .addIf(maxItems != null, "maxItems") .addIf(maxLength != null, "maxLength") .addIf(maxProperties != null, "maxProperties") .addIf(minimum != null, "minimum") .addIf(minItems != null, "minItems") .addIf(minLength != null, "minLength") .addIf(minProperties != null, "minProperties") .addIf(multipleOf != null, "multipleOf") .addIf(pattern != null, "pattern") .addIf(properties != null, "properties") .addIf(readOnly != null, "readOnly") .addIf(ref != null, "$ref") .addIf(required != null, "required") .addIf(requiredProperties != null, "requiredProperties") .addIf(title != null, "title") .addIf(type != null, "type") .addIf(uniqueItems != null, "uniqueItems") .addIf(xml != null, "xml") .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 SchemaInfo resolveRefs(Swagger swagger, Deque refStack, int maxDepth) { if (ref != null) { if (refStack.contains(ref) || refStack.size() >= maxDepth) return this; refStack.addLast(ref); SchemaInfo r = swagger.findRef(ref, SchemaInfo.class).resolveRefs(swagger, refStack, maxDepth); refStack.removeLast(); return r; } if (items != null) items = items.resolveRefs(swagger, refStack, maxDepth); if (properties != null) properties.entrySet().forEach(x -> x.setValue(x.getValue().resolveRefs(swagger, refStack, maxDepth))); if (additionalProperties != null) additionalProperties = additionalProperties.resolveRefs(swagger, refStack, maxDepth); this.example = null; return this; } }