123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645 |
- package spec
- import (
- "encoding/json"
- "fmt"
- "strings"
- "github.com/go-openapi/jsonpointer"
- "github.com/go-openapi/swag"
- )
- func BooleanProperty() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"boolean"}}}
- }
- func BoolProperty() *Schema { return BooleanProperty() }
- func StringProperty() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}
- }
- func CharProperty() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}}}
- }
- func Float64Property() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"number"}, Format: "double"}}
- }
- func Float32Property() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"number"}, Format: "float"}}
- }
- func Int8Property() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int8"}}
- }
- func Int16Property() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int16"}}
- }
- func Int32Property() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int32"}}
- }
- func Int64Property() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"integer"}, Format: "int64"}}
- }
- func StrFmtProperty(format string) *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}, Format: format}}
- }
- func DateProperty() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}, Format: "date"}}
- }
- func DateTimeProperty() *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"string"}, Format: "date-time"}}
- }
- func MapProperty(property *Schema) *Schema {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"object"},
- AdditionalProperties: &SchemaOrBool{Allows: true, Schema: property}}}
- }
- func RefProperty(name string) *Schema {
- return &Schema{SchemaProps: SchemaProps{Ref: MustCreateRef(name)}}
- }
- func RefSchema(name string) *Schema {
- return &Schema{SchemaProps: SchemaProps{Ref: MustCreateRef(name)}}
- }
- func ArrayProperty(items *Schema) *Schema {
- if items == nil {
- return &Schema{SchemaProps: SchemaProps{Type: []string{"array"}}}
- }
- return &Schema{SchemaProps: SchemaProps{Items: &SchemaOrArray{Schema: items}, Type: []string{"array"}}}
- }
- func ComposedSchema(schemas ...Schema) *Schema {
- s := new(Schema)
- s.AllOf = schemas
- return s
- }
- type SchemaURL string
- func (r SchemaURL) MarshalJSON() ([]byte, error) {
- if r == "" {
- return []byte("{}"), nil
- }
- v := map[string]interface{}{"$schema": string(r)}
- return json.Marshal(v)
- }
- func (r *SchemaURL) UnmarshalJSON(data []byte) error {
- var v map[string]interface{}
- if err := json.Unmarshal(data, &v); err != nil {
- return err
- }
- return r.fromMap(v)
- }
- func (r *SchemaURL) fromMap(v map[string]interface{}) error {
- if v == nil {
- return nil
- }
- if vv, ok := v["$schema"]; ok {
- if str, ok := vv.(string); ok {
- u, err := parseURL(str)
- if err != nil {
- return err
- }
- *r = SchemaURL(u.String())
- }
- }
- return nil
- }
- 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"`
- }
- 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"`
- }
- type Schema struct {
- VendorExtensible
- SchemaProps
- SwaggerSchemaProps
- ExtraProps map[string]interface{} `json:"-"`
- }
- func (s Schema) JSONLookup(token string) (interface{}, error) {
- if ex, ok := s.Extensions[token]; ok {
- return &ex, nil
- }
- if ex, ok := s.ExtraProps[token]; ok {
- return &ex, nil
- }
- r, _, err := jsonpointer.GetForToken(s.SchemaProps, token)
- if r != nil || (err != nil && !strings.HasPrefix(err.Error(), "object has no field")) {
- return r, err
- }
- r, _, err = jsonpointer.GetForToken(s.SwaggerSchemaProps, token)
- return r, err
- }
- func (s *Schema) WithID(id string) *Schema {
- s.ID = id
- return s
- }
- func (s *Schema) WithTitle(title string) *Schema {
- s.Title = title
- return s
- }
- func (s *Schema) WithDescription(description string) *Schema {
- s.Description = description
- return s
- }
- func (s *Schema) WithProperties(schemas map[string]Schema) *Schema {
- s.Properties = schemas
- return s
- }
- func (s *Schema) SetProperty(name string, schema Schema) *Schema {
- if s.Properties == nil {
- s.Properties = make(map[string]Schema)
- }
- s.Properties[name] = schema
- return s
- }
- func (s *Schema) WithAllOf(schemas ...Schema) *Schema {
- s.AllOf = schemas
- return s
- }
- func (s *Schema) WithMaxProperties(max int64) *Schema {
- s.MaxProperties = &max
- return s
- }
- func (s *Schema) WithMinProperties(min int64) *Schema {
- s.MinProperties = &min
- return s
- }
- func (s *Schema) Typed(tpe, format string) *Schema {
- s.Type = []string{tpe}
- s.Format = format
- return s
- }
- func (s *Schema) AddType(tpe, format string) *Schema {
- s.Type = append(s.Type, tpe)
- if format != "" {
- s.Format = format
- }
- return s
- }
- func (s *Schema) AsNullable() *Schema {
- s.Nullable = true
- return s
- }
- func (s *Schema) CollectionOf(items Schema) *Schema {
- s.Type = []string{jsonArray}
- s.Items = &SchemaOrArray{Schema: &items}
- return s
- }
- func (s *Schema) WithDefault(defaultValue interface{}) *Schema {
- s.Default = defaultValue
- return s
- }
- func (s *Schema) WithRequired(items ...string) *Schema {
- s.Required = items
- return s
- }
- func (s *Schema) AddRequired(items ...string) *Schema {
- s.Required = append(s.Required, items...)
- return s
- }
- func (s *Schema) WithMaxLength(max int64) *Schema {
- s.MaxLength = &max
- return s
- }
- func (s *Schema) WithMinLength(min int64) *Schema {
- s.MinLength = &min
- return s
- }
- func (s *Schema) WithPattern(pattern string) *Schema {
- s.Pattern = pattern
- return s
- }
- func (s *Schema) WithMultipleOf(number float64) *Schema {
- s.MultipleOf = &number
- return s
- }
- func (s *Schema) WithMaximum(max float64, exclusive bool) *Schema {
- s.Maximum = &max
- s.ExclusiveMaximum = exclusive
- return s
- }
- func (s *Schema) WithMinimum(min float64, exclusive bool) *Schema {
- s.Minimum = &min
- s.ExclusiveMinimum = exclusive
- return s
- }
- func (s *Schema) WithEnum(values ...interface{}) *Schema {
- s.Enum = append([]interface{}{}, values...)
- return s
- }
- func (s *Schema) WithMaxItems(size int64) *Schema {
- s.MaxItems = &size
- return s
- }
- func (s *Schema) WithMinItems(size int64) *Schema {
- s.MinItems = &size
- return s
- }
- func (s *Schema) UniqueValues() *Schema {
- s.UniqueItems = true
- return s
- }
- func (s *Schema) AllowDuplicates() *Schema {
- s.UniqueItems = false
- return s
- }
- func (s *Schema) AddToAllOf(schemas ...Schema) *Schema {
- s.AllOf = append(s.AllOf, schemas...)
- return s
- }
- func (s *Schema) WithDiscriminator(discriminator string) *Schema {
- s.Discriminator = discriminator
- return s
- }
- func (s *Schema) AsReadOnly() *Schema {
- s.ReadOnly = true
- return s
- }
- func (s *Schema) AsWritable() *Schema {
- s.ReadOnly = false
- return s
- }
- func (s *Schema) WithExample(example interface{}) *Schema {
- s.Example = example
- return s
- }
- func (s *Schema) WithExternalDocs(description, url string) *Schema {
- if description == "" && url == "" {
- s.ExternalDocs = nil
- return s
- }
- if s.ExternalDocs == nil {
- s.ExternalDocs = &ExternalDocumentation{}
- }
- s.ExternalDocs.Description = description
- s.ExternalDocs.URL = url
- return s
- }
- func (s *Schema) WithXMLName(name string) *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Name = name
- return s
- }
- func (s *Schema) WithXMLNamespace(namespace string) *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Namespace = namespace
- return s
- }
- func (s *Schema) WithXMLPrefix(prefix string) *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Prefix = prefix
- return s
- }
- func (s *Schema) AsXMLAttribute() *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Attribute = true
- return s
- }
- func (s *Schema) AsXMLElement() *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Attribute = false
- return s
- }
- func (s *Schema) AsWrappedXML() *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Wrapped = true
- return s
- }
- func (s *Schema) AsUnwrappedXML() *Schema {
- if s.XML == nil {
- s.XML = new(XMLObject)
- }
- s.XML.Wrapped = false
- return s
- }
- func (s *Schema) SetValidations(val SchemaValidations) {
- s.Maximum = val.Maximum
- s.ExclusiveMaximum = val.ExclusiveMaximum
- s.Minimum = val.Minimum
- s.ExclusiveMinimum = val.ExclusiveMinimum
- s.MaxLength = val.MaxLength
- s.MinLength = val.MinLength
- s.Pattern = val.Pattern
- s.MaxItems = val.MaxItems
- s.MinItems = val.MinItems
- s.UniqueItems = val.UniqueItems
- s.MultipleOf = val.MultipleOf
- s.Enum = val.Enum
- s.MinProperties = val.MinProperties
- s.MaxProperties = val.MaxProperties
- s.PatternProperties = val.PatternProperties
- }
- func (s *Schema) WithValidations(val SchemaValidations) *Schema {
- s.SetValidations(val)
- return s
- }
- func (s Schema) Validations() SchemaValidations {
- return SchemaValidations{
- CommonValidations: CommonValidations{
- Maximum: s.Maximum,
- ExclusiveMaximum: s.ExclusiveMaximum,
- Minimum: s.Minimum,
- 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,
- },
- MinProperties: s.MinProperties,
- MaxProperties: s.MaxProperties,
- PatternProperties: s.PatternProperties,
- }
- }
- func (s Schema) MarshalJSON() ([]byte, error) {
- b1, err := json.Marshal(s.SchemaProps)
- if err != nil {
- return nil, fmt.Errorf("schema props %v", err)
- }
- b2, err := json.Marshal(s.VendorExtensible)
- if err != nil {
- return nil, fmt.Errorf("vendor props %v", err)
- }
- b3, err := s.Ref.MarshalJSON()
- if err != nil {
- return nil, fmt.Errorf("ref prop %v", err)
- }
- b4, err := s.Schema.MarshalJSON()
- if err != nil {
- return nil, fmt.Errorf("schema prop %v", err)
- }
- b5, err := json.Marshal(s.SwaggerSchemaProps)
- if err != nil {
- return nil, fmt.Errorf("common validations %v", err)
- }
- var b6 []byte
- if s.ExtraProps != nil {
- jj, err := json.Marshal(s.ExtraProps)
- if err != nil {
- return nil, fmt.Errorf("extra props %v", err)
- }
- b6 = jj
- }
- return swag.ConcatJSON(b1, b2, b3, b4, b5, b6), nil
- }
- func (s *Schema) UnmarshalJSON(data []byte) error {
- props := struct {
- SchemaProps
- SwaggerSchemaProps
- }{}
- if err := json.Unmarshal(data, &props); err != nil {
- return err
- }
- sch := Schema{
- SchemaProps: props.SchemaProps,
- SwaggerSchemaProps: props.SwaggerSchemaProps,
- }
- var d map[string]interface{}
- if err := json.Unmarshal(data, &d); err != nil {
- return err
- }
- _ = sch.Ref.fromMap(d)
- _ = sch.Schema.fromMap(d)
- delete(d, "$ref")
- delete(d, "$schema")
- for _, pn := range swag.DefaultJSONNameProvider.GetJSONNames(s) {
- delete(d, pn)
- }
- for k, vv := range d {
- lk := strings.ToLower(k)
- if strings.HasPrefix(lk, "x-") {
- if sch.Extensions == nil {
- sch.Extensions = map[string]interface{}{}
- }
- sch.Extensions[k] = vv
- continue
- }
- if sch.ExtraProps == nil {
- sch.ExtraProps = map[string]interface{}{}
- }
- sch.ExtraProps[k] = vv
- }
- *s = sch
- return nil
- }
|