Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #19 from neotoolkit/fix/requestbodies-comp
Browse files Browse the repository at this point in the history
fix(openapi): change RequestBodies obj
  • Loading branch information
sashamelentyev authored Feb 14, 2022
2 parents c416d8b + 39395a6 commit c5fb599
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions components.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package openapi

// Components -.
type Components struct {
Schemas Schemas `json:"schemas,omitempty" yaml:"schemas,omitempty"`
RequestBodies Schemas `json:"requestBodies,omitempty" yaml:"requestBodies,omitempty"`
Schemas Schemas `json:"schemas,omitempty" yaml:"schemas,omitempty"`
RequestBodies RequestBodies `json:"requestBodies,omitempty" yaml:"requestBodies,omitempty"`
}
24 changes: 19 additions & 5 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ func (e *SchemaError) Error() string {
return "unknown schema " + e.Ref
}

// RequestBodyError -.
type RequestBodyError struct {
Ref string
}

// Error -.
func (e *RequestBodyError) Error() string {
return "unknown request body " + e.Ref
}

// OpenAPI Object
// See specification https://swagger.io/specification/#openapi-object
type OpenAPI struct {
Expand All @@ -26,19 +36,23 @@ type OpenAPI struct {
Info Info `json:"info" yaml:"info"`
}

// LookupByReference -.
func (api OpenAPI) LookupByReference(ref string) (Schema, error) {
// LookupSchemaByReference -.
func (api OpenAPI) LookupSchemaByReference(ref string) (Schema, error) {
schema, ok := api.Components.Schemas[schemaKey(ref)]
if ok {
return *schema, nil
}

schema, ok = api.Components.RequestBodies[requestBodiesKey(ref)]
return Schema{}, &SchemaError{Ref: ref}
}

func (api OpenAPI) LookupRequestBodyByReference(ref string) (RequestBody, error) {
requestBody, ok := api.Components.RequestBodies[requestBodiesKey(ref)]
if ok {
return *schema, nil
return *requestBody, nil
}

return Schema{}, &SchemaError{Ref: ref}
return RequestBody{}, &RequestBodyError{Ref: ref}
}

func schemaKey(ref string) string {
Expand Down
21 changes: 20 additions & 1 deletion openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,29 @@ func TestSchemaError(t *testing.T) {
func TestLookupByReference(t *testing.T) {
api := openapi.OpenAPI{}

schema, err := api.LookupByReference("")
schema, err := api.LookupSchemaByReference("")

var schemaErr *openapi.SchemaError

require.Equal(t, openapi.Schema{}, schema)
require.True(t, errors.As(err, &schemaErr))
}

func TestRequestBodyError(t *testing.T) {
got := &openapi.RequestBodyError{
Ref: "test",
}

require.Equal(t, got.Error(), "unknown request body test")
}

func TestLookupRequestBodyByReference(t *testing.T) {
api := openapi.OpenAPI{}

schema, err := api.LookupRequestBodyByReference("")

var requestBodyErr *openapi.RequestBodyError

require.Equal(t, openapi.RequestBody{}, schema)
require.True(t, errors.As(err, &requestBodyErr))
}
3 changes: 3 additions & 0 deletions requestbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ type RequestBody struct {
Content Content `json:"content,omitempty" yaml:"content,omitempty"`
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
}

// RequestBodies -.
type RequestBodies map[string]*RequestBody
4 changes: 2 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ type Schemas map[string]*Schema

// SchemaContext -.
type SchemaContext interface {
LookupByReference(ref string) (Schema, error)
LookupSchemaByReference(ref string) (Schema, error)
}

// ResponseByExample -.
func (s Schema) ResponseByExample(schemaContext SchemaContext) (interface{}, error) {
if s.Ref != "" {
schema, err := schemaContext.LookupByReference(s.Ref)
schema, err := schemaContext.LookupSchemaByReference(s.Ref)
if err != nil {
return nil, fmt.Errorf("lookup: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type schemaContextStub struct{}

func (s schemaContextStub) LookupByReference(ref string) (openapi.Schema, error) {
func (s schemaContextStub) LookupSchemaByReference(ref string) (openapi.Schema, error) {
userSchema := openapi.Schema{
Properties: openapi.Schemas{
"id": &openapi.Schema{
Expand Down

0 comments on commit c5fb599

Please sign in to comment.