Skip to content

Commit

Permalink
refactor getBodyParameterBodySchema to pull out logic into new functi…
Browse files Browse the repository at this point in the history
…on (bodyParameterExists)

- use the first body parameter returned - no longer check for multiple body params
- remove test case checking for multiple body params
  • Loading branch information
lillchan committed Oct 28, 2019
1 parent 8941d42 commit 08732cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
19 changes: 9 additions & 10 deletions openapi/openapi_v2_spec_analyser.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,25 +377,24 @@ func (specAnalyser *specV2Analyser) postDefined(resourceRootPath string) bool {
return true
}

func (specAnalyser *specV2Analyser) getBodyParameterBodySchema(resourceRootPostOperation *spec.Operation) (*spec.Schema, error) {
func (SpecAnalyser *specV2Analyser) bodyParameterExists(resourceRootPostOperation *spec.Operation) (*spec.Parameter, error) {
if resourceRootPostOperation == nil {
return nil, fmt.Errorf("resource root operation does not have a POST operation")
}
bodyCounter := 0
var bodyParameter spec.Parameter

for _, parameter := range resourceRootPostOperation.Parameters {
if parameter.In == "body" {
bodyCounter = bodyCounter + 1
bodyParameter = parameter
return &parameter, nil
}
}

if bodyCounter <= 0 {
return nil, fmt.Errorf("resource root operation missing the body parameter")
}
return nil, fmt.Errorf("resource root operation missing the body parameter")
}

if bodyCounter > 1 {
return nil, fmt.Errorf("resource root operation contains multiple 'body' parameters")
func (specAnalyser *specV2Analyser) getBodyParameterBodySchema(resourceRootPostOperation *spec.Operation) (*spec.Schema, error) {
bodyParameter, err := specAnalyser.bodyParameterExists(resourceRootPostOperation)
if err != nil {
return nil, err
}

if bodyParameter.Schema == nil {
Expand Down
27 changes: 0 additions & 27 deletions openapi/openapi_v2_spec_analyser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,33 +193,6 @@ func Test_getBodyParameterBodySchema(t *testing.T) {
So(err.Error(), ShouldEqual, "resource root operation missing the body parameter")
})
})
Convey("When getResourcePayloadSchemaRef method is called with an operation that has multiple body parameters", func() {
operation := &spec.Operation{
OperationProps: spec.OperationProps{
Parameters: []spec.Parameter{
{
ParamProps: spec.ParamProps{
In: "body",
Name: "first body",
},
},
{
ParamProps: spec.ParamProps{
In: "body",
Name: "second body",
},
},
},
},
}
_, err := specV2Analyser.getBodyParameterBodySchema(operation)
Convey("Then the error returned should not be nil", func() {
So(err, ShouldNotBeNil)
})
Convey("And the error message should be", func() {
So(err.Error(), ShouldContainSubstring, "operation contains multiple 'body' parameters")
})
})
Convey("When getBodyParameterBodySchema is called with an Operation with OperationProps with a Parameter with an In:body ParamProp and NO Schema ParamProp", func() {
resourceRootPostOperation := &spec.Operation{}
param := spec.Parameter{ParamProps: spec.ParamProps{In: "body"}}
Expand Down

0 comments on commit 08732cf

Please sign in to comment.