Skip to content

Commit

Permalink
Merge pull request #23 from strvcom/feat/SGO-99-generate-body
Browse files Browse the repository at this point in the history
feat: expand request body
  • Loading branch information
xburda4 committed Sep 22, 2022
2 parents ab74d59 + 45f1b72 commit 1ed9778
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ How to release a new version:

## [Unreleased]

## [0.1.1] - 2022-09-22
### Added
- Added openapi support for extending RequestBody.

## [0.1.0] - 2022-08-01
### Added
- Added Changelog.

[Unreleased]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.1.0...HEAD
[Unreleased]: https://github.com/strvcom/strv-backend-go-tea/compare/v0.1.1...HEAD
[0.1.1]: https://github.com/strvcom/strv-backend-go-tea/releases/tag/v0.1.1
[0.1.0]: https://github.com/strvcom/strv-backend-go-tea/releases/tag/v0.1.0
14 changes: 14 additions & 0 deletions pkg/openapi/spec/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,20 @@ func composeOperation(op *Operation, resolver *schemaLoader, basePath string) er
}
}

if op.RequestBody != nil {
for mediaType := range op.RequestBody.RequestBodyProps.Content {
if op.RequestBody.RequestBodyProps.Content == nil {
continue
}
content := op.RequestBody.RequestBodyProps.Content[mediaType]

if err := composeContent(&content, resolver, basePath); resolver.shouldStopOnError(err) {
return err
}
op.RequestBody.RequestBodyProps.Content[mediaType] = content
}
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/openapi/spec/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type OperationProps struct {
Security []map[string][]string `json:"security,omitempty"`
Parameters []specs.Parameter `json:"parameters,omitempty"`
Responses *Responses `json:"responses,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty"`
}

// MarshalJSON takes care of serializing operation properties to JSON
Expand Down
60 changes: 60 additions & 0 deletions pkg/openapi/spec/request_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package spec

import (
"encoding/json"
"reflect"

"github.com/go-openapi/jsonpointer"
specs "github.com/go-openapi/spec"
"github.com/go-openapi/swag"
)

type RequestBody struct {
specs.Schema
specs.Refable
specs.VendorExtensible
RequestBodyProps
}

type RequestBodyProps struct {
Description string `json:"description,omitempty"`
Content map[string]Content `json:"content,omitempty"`
Required bool `json:"required"`
}

// JSONLookup implements an interface to customize json pointer lookup
func (rb RequestBody) JSONLookup(token string) (any, error) {
if ex, ok := rb.Extensions[token]; ok {
return &ex, nil
}
r, _, err := jsonpointer.GetForToken(rb.RequestBodyProps, token)
return r, err
}

// UnmarshalJSON hydrates this items instance with the data from JSON
func (rb *RequestBody) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &rb.RequestBodyProps); err != nil {
return err
}
if err := json.Unmarshal(data, &rb.VendorExtensible); err != nil {
return err
}
if reflect.DeepEqual(RequestBodyProps{}, rb.RequestBodyProps) {
rb.RequestBodyProps = RequestBodyProps{}
}
return nil
}

// MarshalJSON converts this items object to JSON
func (rb RequestBody) MarshalJSON() ([]byte, error) {
b1, err := json.Marshal(rb.RequestBodyProps)
if err != nil {
return nil, err
}
b2, err := json.Marshal(rb.VendorExtensible)
if err != nil {
return nil, err
}
concated := swag.ConcatJSON(b1, b2)
return concated, nil
}

0 comments on commit 1ed9778

Please sign in to comment.