forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/go-openapi/spec/schema.go

1106 lines
18 KiB
Go
Raw Normal View History

2024-05-14 13:07:09 +00:00
// Copyright 2015 go-swagger maintainers
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-05-14 13:07:09 +00:00
// you may not use this file except in compliance with the License.
2024-05-14 13:07:09 +00:00
// You may obtain a copy of the License at
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-05-14 13:07:09 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-05-14 13:07:09 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-05-14 13:07:09 +00:00
// See the License for the specific language governing permissions and
2024-05-14 13:07:09 +00:00
// limitations under the License.
package spec
import (
"encoding/json"
"fmt"
"strings"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/swag"
)
// BooleanProperty creates a boolean property
2024-05-14 13:07:09 +00:00
func BooleanProperty() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"boolean"}}}
2024-05-14 13:07:09 +00:00
}
// BoolProperty creates a boolean property
2024-05-14 13:07:09 +00:00
func BoolProperty() *Schema { return BooleanProperty() }
// StringProperty creates a string property
2024-05-14 13:07:09 +00:00
func StringProperty() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}
2024-05-14 13:07:09 +00:00
}
// CharProperty creates a string property
2024-05-14 13:07:09 +00:00
func CharProperty() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}
2024-05-14 13:07:09 +00:00
}
// Float64Property creates a float64/double property
2024-05-14 13:07:09 +00:00
func Float64Property() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"number"}, Format: "double"}}
2024-05-14 13:07:09 +00:00
}
// Float32Property creates a float32/float property
2024-05-14 13:07:09 +00:00
func Float32Property() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"number"}, Format: "float"}}
2024-05-14 13:07:09 +00:00
}
// Int8Property creates an int8 property
2024-05-14 13:07:09 +00:00
func Int8Property() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int8"}}
2024-05-14 13:07:09 +00:00
}
// Int16Property creates an int16 property
2024-05-14 13:07:09 +00:00
func Int16Property() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int16"}}
2024-05-14 13:07:09 +00:00
}
// Int32Property creates an int32 property
2024-05-14 13:07:09 +00:00
func Int32Property() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int32"}}
2024-05-14 13:07:09 +00:00
}
// Int64Property creates an int64 property
2024-05-14 13:07:09 +00:00
func Int64Property() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int64"}}
2024-05-14 13:07:09 +00:00
}
// StrFmtProperty creates a property for the named string format
2024-05-14 13:07:09 +00:00
func StrFmtProperty(format string) *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}, Format: format}}
2024-05-14 13:07:09 +00:00
}
// DateProperty creates a date property
2024-05-14 13:07:09 +00:00
func DateProperty() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}, Format: "date"}}
2024-05-14 13:07:09 +00:00
}
// DateTimeProperty creates a date time property
2024-05-14 13:07:09 +00:00
func DateTimeProperty() *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}, Format: "date-time"}}
2024-05-14 13:07:09 +00:00
}
// MapProperty creates a map property
2024-05-14 13:07:09 +00:00
func MapProperty(property *Schema) *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"object"},
2024-05-14 13:07:09 +00:00
AdditionalProperties: &SchemaOrBool{Allows: true, Schema: property}}}
2024-05-14 13:07:09 +00:00
}
// RefProperty creates a ref property
2024-05-14 13:07:09 +00:00
func RefProperty(name string) *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Ref: MustCreateRef(name)}}
2024-05-14 13:07:09 +00:00
}
// RefSchema creates a ref property
2024-05-14 13:07:09 +00:00
func RefSchema(name string) *Schema {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Ref: MustCreateRef(name)}}
2024-05-14 13:07:09 +00:00
}
// ArrayProperty creates an array property
2024-05-14 13:07:09 +00:00
func ArrayProperty(items *Schema) *Schema {
2024-05-14 13:07:09 +00:00
if items == nil {
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Type: []string{"array"}}}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return &Schema{SchemaProps: SchemaProps{Items: &SchemaOrArray{Schema: items}, Type: []string{"array"}}}
2024-05-14 13:07:09 +00:00
}
// ComposedSchema creates a schema with allOf
2024-05-14 13:07:09 +00:00
func ComposedSchema(schemas ...Schema) *Schema {
2024-05-14 13:07:09 +00:00
s := new(Schema)
2024-05-14 13:07:09 +00:00
s.AllOf = schemas
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// SchemaURL represents a schema url
2024-05-14 13:07:09 +00:00
type SchemaURL string
// MarshalJSON marshal this to JSON
2024-05-14 13:07:09 +00:00
func (r SchemaURL) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
if r == "" {
2024-05-14 13:07:09 +00:00
return []byte("{}"), nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
v := map[string]interface{}{"$schema": string(r)}
2024-05-14 13:07:09 +00:00
return json.Marshal(v)
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON unmarshal this from JSON
2024-05-14 13:07:09 +00:00
func (r *SchemaURL) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
var v map[string]interface{}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &v); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return r.fromMap(v)
2024-05-14 13:07:09 +00:00
}
func (r *SchemaURL) fromMap(v map[string]interface{}) error {
2024-05-14 13:07:09 +00:00
if v == nil {
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if vv, ok := v["$schema"]; ok {
2024-05-14 13:07:09 +00:00
if str, ok := vv.(string); ok {
2024-05-14 13:07:09 +00:00
u, err := parseURL(str)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
*r = SchemaURL(u.String())
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return nil
2024-05-14 13:07:09 +00:00
}
// SchemaProps describes a JSON schema (draft 4)
2024-05-14 13:07:09 +00:00
type SchemaProps struct {
ID string `json:"id,omitempty"`
Ref Ref `json:"-"`
Schema SchemaURL `json:"-"`
Description string `json:"description,omitempty"`
Type StringOrArray `json:"type,omitempty"`
Nullable bool `json:"nullable,omitempty"`
Format string `json:"format,omitempty"`
Title string `json:"title,omitempty"`
Default interface{} `json:"default,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty"`
MinLength *int64 `json:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty"`
MinItems *int64 `json:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
MaxProperties *int64 `json:"maxProperties,omitempty"`
MinProperties *int64 `json:"minProperties,omitempty"`
Required []string `json:"required,omitempty"`
Items *SchemaOrArray `json:"items,omitempty"`
AllOf []Schema `json:"allOf,omitempty"`
OneOf []Schema `json:"oneOf,omitempty"`
AnyOf []Schema `json:"anyOf,omitempty"`
Not *Schema `json:"not,omitempty"`
Properties SchemaProperties `json:"properties,omitempty"`
AdditionalProperties *SchemaOrBool `json:"additionalProperties,omitempty"`
PatternProperties SchemaProperties `json:"patternProperties,omitempty"`
Dependencies Dependencies `json:"dependencies,omitempty"`
AdditionalItems *SchemaOrBool `json:"additionalItems,omitempty"`
Definitions Definitions `json:"definitions,omitempty"`
2024-05-14 13:07:09 +00:00
}
// SwaggerSchemaProps are additional properties supported by swagger schemas, but not JSON-schema (draft 4)
2024-05-14 13:07:09 +00:00
type SwaggerSchemaProps struct {
Discriminator string `json:"discriminator,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
XML *XMLObject `json:"xml,omitempty"`
ExternalDocs *ExternalDocumentation `json:"externalDocs,omitempty"`
Example interface{} `json:"example,omitempty"`
2024-05-14 13:07:09 +00:00
}
// Schema the schema object allows the definition of input and output data types.
2024-05-14 13:07:09 +00:00
// These types can be objects, but also primitives and arrays.
2024-05-14 13:07:09 +00:00
// This object is based on the [JSON Schema Specification Draft 4](http://json-schema.org/)
2024-05-14 13:07:09 +00:00
// and uses a predefined subset of it.
2024-05-14 13:07:09 +00:00
// On top of this subset, there are extensions provided by this specification to allow for more complete documentation.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// For more information: http://goo.gl/8us55a#schemaObject
2024-05-14 13:07:09 +00:00
type Schema struct {
VendorExtensible
2024-05-14 13:07:09 +00:00
SchemaProps
2024-05-14 13:07:09 +00:00
SwaggerSchemaProps
2024-05-14 13:07:09 +00:00
ExtraProps map[string]interface{} `json:"-"`
}
// JSONLookup implements an interface to customize json pointer lookup
2024-05-14 13:07:09 +00:00
func (s Schema) JSONLookup(token string) (interface{}, error) {
2024-05-14 13:07:09 +00:00
if ex, ok := s.Extensions[token]; ok {
2024-05-14 13:07:09 +00:00
return &ex, nil
2024-05-14 13:07:09 +00:00
}
if ex, ok := s.ExtraProps[token]; ok {
2024-05-14 13:07:09 +00:00
return &ex, nil
2024-05-14 13:07:09 +00:00
}
r, _, err := jsonpointer.GetForToken(s.SchemaProps, token)
2024-05-14 13:07:09 +00:00
if r != nil || (err != nil && !strings.HasPrefix(err.Error(), "object has no field")) {
2024-05-14 13:07:09 +00:00
return r, err
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
r, _, err = jsonpointer.GetForToken(s.SwaggerSchemaProps, token)
2024-05-14 13:07:09 +00:00
return r, err
2024-05-14 13:07:09 +00:00
}
// WithID sets the id for this schema, allows for chaining
2024-05-14 13:07:09 +00:00
func (s *Schema) WithID(id string) *Schema {
2024-05-14 13:07:09 +00:00
s.ID = id
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithTitle sets the title for this schema, allows for chaining
2024-05-14 13:07:09 +00:00
func (s *Schema) WithTitle(title string) *Schema {
2024-05-14 13:07:09 +00:00
s.Title = title
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithDescription sets the description for this schema, allows for chaining
2024-05-14 13:07:09 +00:00
func (s *Schema) WithDescription(description string) *Schema {
2024-05-14 13:07:09 +00:00
s.Description = description
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithProperties sets the properties for this schema
2024-05-14 13:07:09 +00:00
func (s *Schema) WithProperties(schemas map[string]Schema) *Schema {
2024-05-14 13:07:09 +00:00
s.Properties = schemas
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// SetProperty sets a property on this schema
2024-05-14 13:07:09 +00:00
func (s *Schema) SetProperty(name string, schema Schema) *Schema {
2024-05-14 13:07:09 +00:00
if s.Properties == nil {
2024-05-14 13:07:09 +00:00
s.Properties = make(map[string]Schema)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.Properties[name] = schema
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithAllOf sets the all of property
2024-05-14 13:07:09 +00:00
func (s *Schema) WithAllOf(schemas ...Schema) *Schema {
2024-05-14 13:07:09 +00:00
s.AllOf = schemas
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMaxProperties sets the max number of properties an object can have
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMaxProperties(max int64) *Schema {
2024-05-14 13:07:09 +00:00
s.MaxProperties = &max
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMinProperties sets the min number of properties an object must have
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMinProperties(min int64) *Schema {
2024-05-14 13:07:09 +00:00
s.MinProperties = &min
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// Typed sets the type of this schema for a single value item
2024-05-14 13:07:09 +00:00
func (s *Schema) Typed(tpe, format string) *Schema {
2024-05-14 13:07:09 +00:00
s.Type = []string{tpe}
2024-05-14 13:07:09 +00:00
s.Format = format
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AddType adds a type with potential format to the types for this schema
2024-05-14 13:07:09 +00:00
func (s *Schema) AddType(tpe, format string) *Schema {
2024-05-14 13:07:09 +00:00
s.Type = append(s.Type, tpe)
2024-05-14 13:07:09 +00:00
if format != "" {
2024-05-14 13:07:09 +00:00
s.Format = format
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsNullable flags this schema as nullable.
2024-05-14 13:07:09 +00:00
func (s *Schema) AsNullable() *Schema {
2024-05-14 13:07:09 +00:00
s.Nullable = true
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// CollectionOf a fluent builder method for an array parameter
2024-05-14 13:07:09 +00:00
func (s *Schema) CollectionOf(items Schema) *Schema {
2024-05-14 13:07:09 +00:00
s.Type = []string{jsonArray}
2024-05-14 13:07:09 +00:00
s.Items = &SchemaOrArray{Schema: &items}
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithDefault sets the default value on this parameter
2024-05-14 13:07:09 +00:00
func (s *Schema) WithDefault(defaultValue interface{}) *Schema {
2024-05-14 13:07:09 +00:00
s.Default = defaultValue
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithRequired flags this parameter as required
2024-05-14 13:07:09 +00:00
func (s *Schema) WithRequired(items ...string) *Schema {
2024-05-14 13:07:09 +00:00
s.Required = items
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AddRequired adds field names to the required properties array
2024-05-14 13:07:09 +00:00
func (s *Schema) AddRequired(items ...string) *Schema {
2024-05-14 13:07:09 +00:00
s.Required = append(s.Required, items...)
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMaxLength sets a max length value
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMaxLength(max int64) *Schema {
2024-05-14 13:07:09 +00:00
s.MaxLength = &max
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMinLength sets a min length value
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMinLength(min int64) *Schema {
2024-05-14 13:07:09 +00:00
s.MinLength = &min
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithPattern sets a pattern value
2024-05-14 13:07:09 +00:00
func (s *Schema) WithPattern(pattern string) *Schema {
2024-05-14 13:07:09 +00:00
s.Pattern = pattern
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMultipleOf sets a multiple of value
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMultipleOf(number float64) *Schema {
2024-05-14 13:07:09 +00:00
s.MultipleOf = &number
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMaximum sets a maximum number value
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMaximum(max float64, exclusive bool) *Schema {
2024-05-14 13:07:09 +00:00
s.Maximum = &max
2024-05-14 13:07:09 +00:00
s.ExclusiveMaximum = exclusive
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMinimum sets a minimum number value
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMinimum(min float64, exclusive bool) *Schema {
2024-05-14 13:07:09 +00:00
s.Minimum = &min
2024-05-14 13:07:09 +00:00
s.ExclusiveMinimum = exclusive
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithEnum sets a the enum values (replace)
2024-05-14 13:07:09 +00:00
func (s *Schema) WithEnum(values ...interface{}) *Schema {
2024-05-14 13:07:09 +00:00
s.Enum = append([]interface{}{}, values...)
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMaxItems sets the max items
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMaxItems(size int64) *Schema {
2024-05-14 13:07:09 +00:00
s.MaxItems = &size
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithMinItems sets the min items
2024-05-14 13:07:09 +00:00
func (s *Schema) WithMinItems(size int64) *Schema {
2024-05-14 13:07:09 +00:00
s.MinItems = &size
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// UniqueValues dictates that this array can only have unique items
2024-05-14 13:07:09 +00:00
func (s *Schema) UniqueValues() *Schema {
2024-05-14 13:07:09 +00:00
s.UniqueItems = true
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AllowDuplicates this array can have duplicates
2024-05-14 13:07:09 +00:00
func (s *Schema) AllowDuplicates() *Schema {
2024-05-14 13:07:09 +00:00
s.UniqueItems = false
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AddToAllOf adds a schema to the allOf property
2024-05-14 13:07:09 +00:00
func (s *Schema) AddToAllOf(schemas ...Schema) *Schema {
2024-05-14 13:07:09 +00:00
s.AllOf = append(s.AllOf, schemas...)
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithDiscriminator sets the name of the discriminator field
2024-05-14 13:07:09 +00:00
func (s *Schema) WithDiscriminator(discriminator string) *Schema {
2024-05-14 13:07:09 +00:00
s.Discriminator = discriminator
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsReadOnly flags this schema as readonly
2024-05-14 13:07:09 +00:00
func (s *Schema) AsReadOnly() *Schema {
2024-05-14 13:07:09 +00:00
s.ReadOnly = true
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsWritable flags this schema as writeable (not read-only)
2024-05-14 13:07:09 +00:00
func (s *Schema) AsWritable() *Schema {
2024-05-14 13:07:09 +00:00
s.ReadOnly = false
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithExample sets the example for this schema
2024-05-14 13:07:09 +00:00
func (s *Schema) WithExample(example interface{}) *Schema {
2024-05-14 13:07:09 +00:00
s.Example = example
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithExternalDocs sets/removes the external docs for/from this schema.
2024-05-14 13:07:09 +00:00
// When you pass empty strings as params the external documents will be removed.
2024-05-14 13:07:09 +00:00
// When you pass non-empty string as one value then those values will be used on the external docs object.
2024-05-14 13:07:09 +00:00
// So when you pass a non-empty description, you should also pass the url and vice versa.
2024-05-14 13:07:09 +00:00
func (s *Schema) WithExternalDocs(description, url string) *Schema {
2024-05-14 13:07:09 +00:00
if description == "" && url == "" {
2024-05-14 13:07:09 +00:00
s.ExternalDocs = nil
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
if s.ExternalDocs == nil {
2024-05-14 13:07:09 +00:00
s.ExternalDocs = &ExternalDocumentation{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.ExternalDocs.Description = description
2024-05-14 13:07:09 +00:00
s.ExternalDocs.URL = url
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithXMLName sets the xml name for the object
2024-05-14 13:07:09 +00:00
func (s *Schema) WithXMLName(name string) *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Name = name
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithXMLNamespace sets the xml namespace for the object
2024-05-14 13:07:09 +00:00
func (s *Schema) WithXMLNamespace(namespace string) *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Namespace = namespace
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// WithXMLPrefix sets the xml prefix for the object
2024-05-14 13:07:09 +00:00
func (s *Schema) WithXMLPrefix(prefix string) *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Prefix = prefix
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsXMLAttribute flags this object as xml attribute
2024-05-14 13:07:09 +00:00
func (s *Schema) AsXMLAttribute() *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Attribute = true
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsXMLElement flags this object as an xml node
2024-05-14 13:07:09 +00:00
func (s *Schema) AsXMLElement() *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Attribute = false
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsWrappedXML flags this object as wrapped, this is mostly useful for array types
2024-05-14 13:07:09 +00:00
func (s *Schema) AsWrappedXML() *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Wrapped = true
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// AsUnwrappedXML flags this object as an xml node
2024-05-14 13:07:09 +00:00
func (s *Schema) AsUnwrappedXML() *Schema {
2024-05-14 13:07:09 +00:00
if s.XML == nil {
2024-05-14 13:07:09 +00:00
s.XML = new(XMLObject)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
s.XML.Wrapped = false
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// SetValidations defines all schema validations.
2024-05-14 13:07:09 +00:00
//
2024-05-14 13:07:09 +00:00
// NOTE: Required, ReadOnly, AllOf, AnyOf, OneOf and Not are not considered.
2024-05-14 13:07:09 +00:00
func (s *Schema) SetValidations(val SchemaValidations) {
2024-05-14 13:07:09 +00:00
s.Maximum = val.Maximum
2024-05-14 13:07:09 +00:00
s.ExclusiveMaximum = val.ExclusiveMaximum
2024-05-14 13:07:09 +00:00
s.Minimum = val.Minimum
2024-05-14 13:07:09 +00:00
s.ExclusiveMinimum = val.ExclusiveMinimum
2024-05-14 13:07:09 +00:00
s.MaxLength = val.MaxLength
2024-05-14 13:07:09 +00:00
s.MinLength = val.MinLength
2024-05-14 13:07:09 +00:00
s.Pattern = val.Pattern
2024-05-14 13:07:09 +00:00
s.MaxItems = val.MaxItems
2024-05-14 13:07:09 +00:00
s.MinItems = val.MinItems
2024-05-14 13:07:09 +00:00
s.UniqueItems = val.UniqueItems
2024-05-14 13:07:09 +00:00
s.MultipleOf = val.MultipleOf
2024-05-14 13:07:09 +00:00
s.Enum = val.Enum
2024-05-14 13:07:09 +00:00
s.MinProperties = val.MinProperties
2024-05-14 13:07:09 +00:00
s.MaxProperties = val.MaxProperties
2024-05-14 13:07:09 +00:00
s.PatternProperties = val.PatternProperties
2024-05-14 13:07:09 +00:00
}
// WithValidations is a fluent method to set schema validations
2024-05-14 13:07:09 +00:00
func (s *Schema) WithValidations(val SchemaValidations) *Schema {
2024-05-14 13:07:09 +00:00
s.SetValidations(val)
2024-05-14 13:07:09 +00:00
return s
2024-05-14 13:07:09 +00:00
}
// Validations returns a clone of the validations for this schema
2024-05-14 13:07:09 +00:00
func (s Schema) Validations() SchemaValidations {
2024-05-14 13:07:09 +00:00
return SchemaValidations{
2024-05-14 13:07:09 +00:00
CommonValidations: CommonValidations{
Maximum: s.Maximum,
2024-05-14 13:07:09 +00:00
ExclusiveMaximum: s.ExclusiveMaximum,
Minimum: s.Minimum,
2024-05-14 13:07:09 +00:00
ExclusiveMinimum: s.ExclusiveMinimum,
MaxLength: s.MaxLength,
MinLength: s.MinLength,
Pattern: s.Pattern,
MaxItems: s.MaxItems,
MinItems: s.MinItems,
UniqueItems: s.UniqueItems,
MultipleOf: s.MultipleOf,
Enum: s.Enum,
2024-05-14 13:07:09 +00:00
},
MinProperties: s.MinProperties,
MaxProperties: s.MaxProperties,
2024-05-14 13:07:09 +00:00
PatternProperties: s.PatternProperties,
}
2024-05-14 13:07:09 +00:00
}
// MarshalJSON marshal this to JSON
2024-05-14 13:07:09 +00:00
func (s Schema) MarshalJSON() ([]byte, error) {
2024-05-14 13:07:09 +00:00
b1, err := json.Marshal(s.SchemaProps)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("schema props %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b2, err := json.Marshal(s.VendorExtensible)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("vendor props %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b3, err := s.Ref.MarshalJSON()
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("ref prop %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b4, err := s.Schema.MarshalJSON()
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("schema prop %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b5, err := json.Marshal(s.SwaggerSchemaProps)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("common validations %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
var b6 []byte
2024-05-14 13:07:09 +00:00
if s.ExtraProps != nil {
2024-05-14 13:07:09 +00:00
jj, err := json.Marshal(s.ExtraProps)
2024-05-14 13:07:09 +00:00
if err != nil {
2024-05-14 13:07:09 +00:00
return nil, fmt.Errorf("extra props %v", err)
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
b6 = jj
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
return swag.ConcatJSON(b1, b2, b3, b4, b5, b6), nil
2024-05-14 13:07:09 +00:00
}
// UnmarshalJSON marshal this from JSON
2024-05-14 13:07:09 +00:00
func (s *Schema) UnmarshalJSON(data []byte) error {
2024-05-14 13:07:09 +00:00
props := struct {
SchemaProps
2024-05-14 13:07:09 +00:00
SwaggerSchemaProps
}{}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &props); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
sch := Schema{
SchemaProps: props.SchemaProps,
2024-05-14 13:07:09 +00:00
SwaggerSchemaProps: props.SwaggerSchemaProps,
}
var d map[string]interface{}
2024-05-14 13:07:09 +00:00
if err := json.Unmarshal(data, &d); err != nil {
2024-05-14 13:07:09 +00:00
return err
2024-05-14 13:07:09 +00:00
}
_ = sch.Ref.fromMap(d)
2024-05-14 13:07:09 +00:00
_ = sch.Schema.fromMap(d)
delete(d, "$ref")
2024-05-14 13:07:09 +00:00
delete(d, "$schema")
2024-05-14 13:07:09 +00:00
for _, pn := range swag.DefaultJSONNameProvider.GetJSONNames(s) {
2024-05-14 13:07:09 +00:00
delete(d, pn)
2024-05-14 13:07:09 +00:00
}
for k, vv := range d {
2024-05-14 13:07:09 +00:00
lk := strings.ToLower(k)
2024-05-14 13:07:09 +00:00
if strings.HasPrefix(lk, "x-") {
2024-05-14 13:07:09 +00:00
if sch.Extensions == nil {
2024-05-14 13:07:09 +00:00
sch.Extensions = map[string]interface{}{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
sch.Extensions[k] = vv
2024-05-14 13:07:09 +00:00
continue
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
if sch.ExtraProps == nil {
2024-05-14 13:07:09 +00:00
sch.ExtraProps = map[string]interface{}{}
2024-05-14 13:07:09 +00:00
}
2024-05-14 13:07:09 +00:00
sch.ExtraProps[k] = vv
2024-05-14 13:07:09 +00:00
}
*s = sch
return nil
2024-05-14 13:07:09 +00:00
}