Skip to content

Commit

Permalink
openapi3: split info.go three ways (#805)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp authored Jun 18, 2023
1 parent 0683bc5 commit 41d2575
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 97 deletions.
56 changes: 56 additions & 0 deletions openapi3/contact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package openapi3

import (
"context"
"encoding/json"
)

// Contact is specified by OpenAPI/Swagger standard version 3.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contact-object
type Contact struct {
Extensions map[string]interface{} `json:"-" yaml:"-"`

Name string `json:"name,omitempty" yaml:"name,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Email string `json:"email,omitempty" yaml:"email,omitempty"`
}

// MarshalJSON returns the JSON encoding of Contact.
func (contact Contact) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 3+len(contact.Extensions))
for k, v := range contact.Extensions {
m[k] = v
}
if x := contact.Name; x != "" {
m["name"] = x
}
if x := contact.URL; x != "" {
m["url"] = x
}
if x := contact.Email; x != "" {
m["email"] = x
}
return json.Marshal(m)
}

// UnmarshalJSON sets Contact to a copy of data.
func (contact *Contact) UnmarshalJSON(data []byte) error {
type ContactBis Contact
var x ContactBis
if err := json.Unmarshal(data, &x); err != nil {
return err
}
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "name")
delete(x.Extensions, "url")
delete(x.Extensions, "email")
*contact = Contact(x)
return nil
}

// Validate returns an error if Contact does not comply with the OpenAPI spec.
func (contact *Contact) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)

return validateExtensions(ctx, contact.Extensions)
}
97 changes: 0 additions & 97 deletions openapi3/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,100 +86,3 @@ func (info *Info) Validate(ctx context.Context, opts ...ValidationOption) error

return validateExtensions(ctx, info.Extensions)
}

// Contact is specified by OpenAPI/Swagger standard version 3.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#contact-object
type Contact struct {
Extensions map[string]interface{} `json:"-" yaml:"-"`

Name string `json:"name,omitempty" yaml:"name,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
Email string `json:"email,omitempty" yaml:"email,omitempty"`
}

// MarshalJSON returns the JSON encoding of Contact.
func (contact Contact) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 3+len(contact.Extensions))
for k, v := range contact.Extensions {
m[k] = v
}
if x := contact.Name; x != "" {
m["name"] = x
}
if x := contact.URL; x != "" {
m["url"] = x
}
if x := contact.Email; x != "" {
m["email"] = x
}
return json.Marshal(m)
}

// UnmarshalJSON sets Contact to a copy of data.
func (contact *Contact) UnmarshalJSON(data []byte) error {
type ContactBis Contact
var x ContactBis
if err := json.Unmarshal(data, &x); err != nil {
return err
}
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "name")
delete(x.Extensions, "url")
delete(x.Extensions, "email")
*contact = Contact(x)
return nil
}

// Validate returns an error if Contact does not comply with the OpenAPI spec.
func (contact *Contact) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)

return validateExtensions(ctx, contact.Extensions)
}

// License is specified by OpenAPI/Swagger standard version 3.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#license-object
type License struct {
Extensions map[string]interface{} `json:"-" yaml:"-"`

Name string `json:"name" yaml:"name"` // Required
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}

// MarshalJSON returns the JSON encoding of License.
func (license License) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 2+len(license.Extensions))
for k, v := range license.Extensions {
m[k] = v
}
m["name"] = license.Name
if x := license.URL; x != "" {
m["url"] = x
}
return json.Marshal(m)
}

// UnmarshalJSON sets License to a copy of data.
func (license *License) UnmarshalJSON(data []byte) error {
type LicenseBis License
var x LicenseBis
if err := json.Unmarshal(data, &x); err != nil {
return err
}
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "name")
delete(x.Extensions, "url")
*license = License(x)
return nil
}

// Validate returns an error if License does not comply with the OpenAPI spec.
func (license *License) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)

if license.Name == "" {
return errors.New("value of license name must be a non-empty string")
}

return validateExtensions(ctx, license.Extensions)
}
54 changes: 54 additions & 0 deletions openapi3/license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package openapi3

import (
"context"
"encoding/json"
"errors"
)

// License is specified by OpenAPI/Swagger standard version 3.
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#license-object
type License struct {
Extensions map[string]interface{} `json:"-" yaml:"-"`

Name string `json:"name" yaml:"name"` // Required
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}

// MarshalJSON returns the JSON encoding of License.
func (license License) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 2+len(license.Extensions))
for k, v := range license.Extensions {
m[k] = v
}
m["name"] = license.Name
if x := license.URL; x != "" {
m["url"] = x
}
return json.Marshal(m)
}

// UnmarshalJSON sets License to a copy of data.
func (license *License) UnmarshalJSON(data []byte) error {
type LicenseBis License
var x LicenseBis
if err := json.Unmarshal(data, &x); err != nil {
return err
}
_ = json.Unmarshal(data, &x.Extensions)
delete(x.Extensions, "name")
delete(x.Extensions, "url")
*license = License(x)
return nil
}

// Validate returns an error if License does not comply with the OpenAPI spec.
func (license *License) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)

if license.Name == "" {
return errors.New("value of license name must be a non-empty string")
}

return validateExtensions(ctx, license.Extensions)
}

0 comments on commit 41d2575

Please sign in to comment.