Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some golints #530

Merged
merged 5 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions openapi2/openapi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ type T struct {
Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
}

// MarshalJSON returns the JSON encoding of T.
func (doc *T) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(doc)
}

// UnmarshalJSON sets T to a copy of data.
func (doc *T) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, doc)
}
Expand Down Expand Up @@ -64,10 +66,12 @@ type PathItem struct {
Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"`
}

// MarshalJSON returns the JSON encoding of PathItem.
func (pathItem *PathItem) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(pathItem)
}

// UnmarshalJSON sets PathItem to a copy of data.
func (pathItem *PathItem) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, pathItem)
}
Expand Down Expand Up @@ -155,10 +159,12 @@ type Operation struct {
Security *SecurityRequirements `json:"security,omitempty" yaml:"security,omitempty"`
}

// MarshalJSON returns the JSON encoding of Operation.
func (operation *Operation) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(operation)
}

// UnmarshalJSON sets Operation to a copy of data.
func (operation *Operation) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, operation)
}
Expand Down Expand Up @@ -207,10 +213,12 @@ type Parameter struct {
Default interface{} `json:"default,omitempty" yaml:"default,omitempty"`
}

// MarshalJSON returns the JSON encoding of Parameter.
func (parameter *Parameter) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(parameter)
}

// UnmarshalJSON sets Parameter to a copy of data.
func (parameter *Parameter) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, parameter)
}
Expand All @@ -224,10 +232,12 @@ type Response struct {
Examples map[string]interface{} `json:"examples,omitempty" yaml:"examples,omitempty"`
}

// MarshalJSON returns the JSON encoding of Response.
func (response *Response) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(response)
}

// UnmarshalJSON sets Response to a copy of data.
func (response *Response) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, response)
}
Expand All @@ -236,10 +246,12 @@ type Header struct {
Parameter
}

// MarshalJSON returns the JSON encoding of Header.
func (header *Header) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(header)
}

// UnmarshalJSON sets Header to a copy of data.
func (header *Header) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, header)
}
Expand All @@ -260,10 +272,12 @@ type SecurityScheme struct {
Tags openapi3.Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
}

// MarshalJSON returns the JSON encoding of SecurityScheme.
func (securityScheme *SecurityScheme) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(securityScheme)
}

// UnmarshalJSON sets SecurityScheme to a copy of data.
func (securityScheme *SecurityScheme) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, securityScheme)
}
6 changes: 4 additions & 2 deletions openapi3/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Callbacks map[string]*CallbackRef

var _ jsonpointer.JSONPointable = (*Callbacks)(nil)

// JSONLookup implements github.com/go-openapi/jsonpointer#JSONPointable
func (c Callbacks) JSONLookup(token string) (interface{}, error) {
ref, ok := c[token]
if ref == nil || !ok {
Expand All @@ -27,8 +28,9 @@ func (c Callbacks) JSONLookup(token string) (interface{}, error) {
// See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#callbackObject
type Callback map[string]*PathItem

func (value Callback) Validate(ctx context.Context) error {
for _, v := range value {
// Validate returns an error if Callback does not comply with the OpenAPI spec.
func (callback Callback) Validate(ctx context.Context) error {
for _, v := range callback {
if err := v.Validate(ctx); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions openapi3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func NewComponents() Components {
return Components{}
}

// MarshalJSON returns the JSON encoding of Components.
func (components *Components) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(components)
}

// UnmarshalJSON sets Components to a copy of data.
func (components *Components) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, components)
}

// Validate returns an error if Components does not comply with the OpenAPI spec.
func (components *Components) Validate(ctx context.Context) (err error) {
for k, v := range components.Schemas {
if err = ValidateIdentifier(k); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions openapi3/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func (content Content) Get(mime string) *MediaType {
return content["*/*"]
}

func (value Content) Validate(ctx context.Context) error {
for _, v := range value {
// Validate MediaType
// Validate returns an error if Content does not comply with the OpenAPI spec.
func (content Content) Validate(ctx context.Context) error {
for _, v := range content {
if err := v.Validate(ctx); err != nil {
return err
}
Expand Down
13 changes: 8 additions & 5 deletions openapi3/discriminator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ type Discriminator struct {
Mapping map[string]string `json:"mapping,omitempty" yaml:"mapping,omitempty"`
}

func (value *Discriminator) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(value)
// MarshalJSON returns the JSON encoding of Discriminator.
func (discriminator *Discriminator) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(discriminator)
}

func (value *Discriminator) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, value)
// UnmarshalJSON sets Discriminator to a copy of data.
func (discriminator *Discriminator) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, discriminator)
}

func (value *Discriminator) Validate(ctx context.Context) error {
// Validate returns an error if Discriminator does not comply with the OpenAPI spec.
func (discriminator *Discriminator) Validate(ctx context.Context) error {
return nil
}
11 changes: 7 additions & 4 deletions openapi3/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ func (encoding *Encoding) WithHeaderRef(name string, ref *HeaderRef) *Encoding {
return encoding
}

// MarshalJSON returns the JSON encoding of Encoding.
func (encoding *Encoding) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(encoding)
}

// UnmarshalJSON sets Encoding to a copy of data.
func (encoding *Encoding) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, encoding)
}
Expand All @@ -62,11 +64,12 @@ func (encoding *Encoding) SerializationMethod() *SerializationMethod {
return sm
}

func (value *Encoding) Validate(ctx context.Context) error {
if value == nil {
// Validate returns an error if Encoding does not comply with the OpenAPI spec.
func (encoding *Encoding) Validate(ctx context.Context) error {
if encoding == nil {
return nil
}
for k, v := range value.Headers {
for k, v := range encoding.Headers {
if err := ValidateIdentifier(k); err != nil {
return nil
}
Expand All @@ -76,7 +79,7 @@ func (value *Encoding) Validate(ctx context.Context) error {
}

// Validate a media types's serialization method.
sm := value.SerializationMethod()
sm := encoding.SerializationMethod()
switch {
case sm.Style == SerializationForm && sm.Explode,
sm.Style == SerializationForm && !sm.Explode,
Expand Down
6 changes: 5 additions & 1 deletion openapi3/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Examples map[string]*ExampleRef

var _ jsonpointer.JSONPointable = (*Examples)(nil)

// JSONLookup implements github.com/go-openapi/jsonpointer#JSONPointable
func (e Examples) JSONLookup(token string) (interface{}, error) {
ref, ok := e[token]
if ref == nil || !ok {
Expand Down Expand Up @@ -41,14 +42,17 @@ func NewExample(value interface{}) *Example {
}
}

// MarshalJSON returns the JSON encoding of Example.
func (example *Example) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(example)
}

// UnmarshalJSON sets Example to a copy of data.
func (example *Example) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, example)
}

func (value *Example) Validate(ctx context.Context) error {
// Validate returns an error if Example does not comply with the OpenAPI spec.
func (example *Example) Validate(ctx context.Context) error {
return nil // TODO
}
3 changes: 3 additions & 0 deletions openapi3/external_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ type ExternalDocs struct {
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}

// MarshalJSON returns the JSON encoding of ExternalDocs.
func (e *ExternalDocs) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(e)
}

// UnmarshalJSON sets ExternalDocs to a copy of data.
func (e *ExternalDocs) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, e)
}

// Validate returns an error if ExternalDocs does not comply with the OpenAPI spec.
func (e *ExternalDocs) Validate(ctx context.Context) error {
if e.URL == "" {
return errors.New("url is required")
Expand Down
68 changes: 36 additions & 32 deletions openapi3/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Headers map[string]*HeaderRef

var _ jsonpointer.JSONPointable = (*Headers)(nil)

// JSONLookup implements github.com/go-openapi/jsonpointer#JSONPointable
func (h Headers) JSONLookup(token string) (interface{}, error) {
ref, ok := h[token]
if ref == nil || !ok {
Expand All @@ -33,33 +34,35 @@ type Header struct {

var _ jsonpointer.JSONPointable = (*Header)(nil)

func (value *Header) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, value)
// UnmarshalJSON sets Header to a copy of data.
func (header *Header) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, header)
}

// SerializationMethod returns a header's serialization method.
func (value *Header) SerializationMethod() (*SerializationMethod, error) {
style := value.Style
func (header *Header) SerializationMethod() (*SerializationMethod, error) {
style := header.Style
if style == "" {
style = SerializationSimple
}
explode := false
if value.Explode != nil {
explode = *value.Explode
if header.Explode != nil {
explode = *header.Explode
}
return &SerializationMethod{Style: style, Explode: explode}, nil
}

func (value *Header) Validate(ctx context.Context) error {
if value.Name != "" {
// Validate returns an error if Header does not comply with the OpenAPI spec.
func (header *Header) Validate(ctx context.Context) error {
if header.Name != "" {
return errors.New("header 'name' MUST NOT be specified, it is given in the corresponding headers map")
}
if value.In != "" {
if header.In != "" {
return errors.New("header 'in' MUST NOT be specified, it is implicitly in header")
}

// Validate a parameter's serialization method.
sm, err := value.SerializationMethod()
sm, err := header.SerializationMethod()
if err != nil {
return err
}
Expand All @@ -70,59 +73,60 @@ func (value *Header) Validate(ctx context.Context) error {
return fmt.Errorf("header schema is invalid: %v", e)
}

if (value.Schema == nil) == (value.Content == nil) {
e := fmt.Errorf("parameter must contain exactly one of content and schema: %v", value)
if (header.Schema == nil) == (header.Content == nil) {
e := fmt.Errorf("parameter must contain exactly one of content and schema: %v", header)
return fmt.Errorf("header schema is invalid: %v", e)
}
if schema := value.Schema; schema != nil {
if schema := header.Schema; schema != nil {
if err := schema.Validate(ctx); err != nil {
return fmt.Errorf("header schema is invalid: %v", err)
}
}

if content := value.Content; content != nil {
if content := header.Content; content != nil {
if err := content.Validate(ctx); err != nil {
return fmt.Errorf("header content is invalid: %v", err)
}
}
return nil
}

func (value Header) JSONLookup(token string) (interface{}, error) {
// JSONLookup implements github.com/go-openapi/jsonpointer#JSONPointable
func (header Header) JSONLookup(token string) (interface{}, error) {
switch token {
case "schema":
if value.Schema != nil {
if value.Schema.Ref != "" {
return &Ref{Ref: value.Schema.Ref}, nil
if header.Schema != nil {
if header.Schema.Ref != "" {
return &Ref{Ref: header.Schema.Ref}, nil
}
return value.Schema.Value, nil
return header.Schema.Value, nil
}
case "name":
return value.Name, nil
return header.Name, nil
case "in":
return value.In, nil
return header.In, nil
case "description":
return value.Description, nil
return header.Description, nil
case "style":
return value.Style, nil
return header.Style, nil
case "explode":
return value.Explode, nil
return header.Explode, nil
case "allowEmptyValue":
return value.AllowEmptyValue, nil
return header.AllowEmptyValue, nil
case "allowReserved":
return value.AllowReserved, nil
return header.AllowReserved, nil
case "deprecated":
return value.Deprecated, nil
return header.Deprecated, nil
case "required":
return value.Required, nil
return header.Required, nil
case "example":
return value.Example, nil
return header.Example, nil
case "examples":
return value.Examples, nil
return header.Examples, nil
case "content":
return value.Content, nil
return header.Content, nil
}

v, _, err := jsonpointer.GetForToken(value.ExtensionProps, token)
v, _, err := jsonpointer.GetForToken(header.ExtensionProps, token)
return v, err
}
Loading