Skip to content

Commit

Permalink
Fix embedded types handling (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Aug 25, 2020
1 parent 3b9595c commit c774271
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v0.2.0
uses: golangci/golangci-lint-action@v2.2.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26
version: v1.30

# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x]
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x]
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand All @@ -32,5 +32,5 @@ jobs:
- name: Test
run: go test -mod=vendor -v -cover -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload code coverage
if: matrix.go-version == '1.14.x'
if: matrix.go-version == '1.15.x'
uses: codecov/codecov-action@v1
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
GOLANGCI_LINT_VERSION := "v1.27.0"
GOLANGCI_LINT_VERSION := "v1.30.0"

gen:
cd resources/schema/ && json-cli gen-go openapi3.json --output ../../openapi3/entities.go --package-name openapi3 --with-zero-values --fluent-setters --root-name Spec
gofmt -w ./openapi3/entities.go

lint:
@test -s $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /tmp $(GOLANGCI_LINT_VERSION) && mv /tmp/golangci-lint $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION))
@$(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) run ./...
@$(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) run ./... --fix
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
require (
github.com/stretchr/testify v1.5.1
github.com/swaggest/assertjson v1.0.0
github.com/swaggest/jsonschema-go v0.3.5
github.com/swaggest/jsonschema-go v0.3.7
github.com/swaggest/refl v0.1.3
github.com/swaggest/swgen v0.6.23
gopkg.in/yaml.v2 v2.3.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ github.com/swaggest/jsonschema-go v0.2.1/go.mod h1:QFauBdPTrU1UltwocM5FzOWnVjVVt
github.com/swaggest/jsonschema-go v0.2.4/go.mod h1:m4VV88Gbi7lCrt9ckJzigK1rMlEeFjdZUkJr1o5MnDE=
github.com/swaggest/jsonschema-go v0.3.5 h1:zOUyPaLfoqgoPB/HnaM3rApLD/nT/BrzZhDeZj9yVUY=
github.com/swaggest/jsonschema-go v0.3.5/go.mod h1:TrWgbug4p2ZgcxnHDz+CvYvEtJ5KckL/XOV4mSR6FGw=
github.com/swaggest/jsonschema-go v0.3.6 h1:fqRQuCX377UnEfAaTwwhaDYchW7y1SnXU1SCihetfKI=
github.com/swaggest/jsonschema-go v0.3.6/go.mod h1:TrWgbug4p2ZgcxnHDz+CvYvEtJ5KckL/XOV4mSR6FGw=
github.com/swaggest/jsonschema-go v0.3.7 h1:EgSPKzzjRrMKGdq7PVF/ziMilQag8HUD8XYCZoii8S8=
github.com/swaggest/jsonschema-go v0.3.7/go.mod h1:TrWgbug4p2ZgcxnHDz+CvYvEtJ5KckL/XOV4mSR6FGw=
github.com/swaggest/openapi-go v0.1.3/go.mod h1:Zx4ZgJ7XvlFH9wCOHE7u8RAjLfiHAnCHeaD5kUDujVM=
github.com/swaggest/refl v0.1.0 h1:mz3skba8ewcPmxDryM9rETv4fSuAX4GqNxTuDfPyUM0=
github.com/swaggest/refl v0.1.0/go.mod h1:kmYWhxNEvjfRDdMRqpaR/vLULk/SotJs9HFUCIVMK8o=
Expand Down
3 changes: 3 additions & 0 deletions openapi3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ func (s *Spec) SetupOperation(method, path string, setup ...func(*Operation) err

if found := paramIndex[p.Parameter.Name+string(p.Parameter.In)]; found {
errs = append(errs, "duplicate parameter in "+string(p.Parameter.In)+": "+p.Parameter.Name)

continue
}

if found := pathParams[p.Parameter.Name]; !found && p.Parameter.In == ParameterInPath {
errs = append(errs, "missing path parameter placeholder in url: "+p.Parameter.Name)

continue
}

Expand Down Expand Up @@ -109,6 +111,7 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {

return s.SetupOperation(method, path, func(op *Operation) error {
*op = operation

return nil
})
}
2 changes: 2 additions & 0 deletions openapi3/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ func (s *SchemaOrRef) toJSONSchema(ctx toJSONSchemaContext) jsonschema.SchemaOrB
func (s *SchemaOrRef) FromJSONSchema(schema jsonschema.SchemaOrBool) {
if schema.TypeBoolean != nil {
s.fromBool(*schema.TypeBoolean)

return
}

js := schema.TypeObject
if js.Ref != nil {
s.SchemaReference = &SchemaReference{Ref: *js.Ref}

return
}

Expand Down
12 changes: 7 additions & 5 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ func (r *Reflector) parseRequestBody(o *Operation, input interface{}, tag, mime
}

// JSON can be a map or array without field tags.
if !hasTaggedFields && !refl.IsSliceOrMap(input) {
if !hasTaggedFields && !refl.IsSliceOrMap(input) && refl.FindEmbeddedSliceOrMap(input) == nil {
return nil
}

hasFileUpload := false
definitionPefix := ""
definitionPrefix := ""

if tag != tagJSON {
definitionPefix += strings.Title(tag)
definitionPrefix += strings.Title(tag)
}

schema, err := r.Reflect(input,
jsonschema.DefinitionsPrefix("#/components/schemas/"+definitionPefix),
jsonschema.DefinitionsPrefix("#/components/schemas/"+definitionPrefix),
jsonschema.RootRef,
jsonschema.PropertyNameTag(tag),
jsonschema.InterceptType(func(v reflect.Value, s *jsonschema.Schema) (bool, error) {
Expand All @@ -121,6 +121,7 @@ func (r *Reflector) parseRequestBody(o *Operation, input interface{}, tag, mime
s.WithFormat("binary")

hasFileUpload = true

return true, nil
}

Expand All @@ -144,7 +145,7 @@ func (r *Reflector) parseRequestBody(o *Operation, input interface{}, tag, mime

s.FromJSONSchema(def)

r.SpecEns().ComponentsEns().SchemasEns().WithMapOfSchemaOrRefValuesItem(definitionPefix+name, s)
r.SpecEns().ComponentsEns().SchemasEns().WithMapOfSchemaOrRefValuesItem(definitionPrefix+name, s)
}

if mime == mimeFormUrlencoded && hasFileUpload {
Expand All @@ -165,6 +166,7 @@ func (r *Reflector) parseParametersIn(o *Operation, input interface{}, in Parame
jsonschema.DefinitionsPrefix("#/components/schemas/"),
jsonschema.CollectDefinitions(r.collectDefinition),
jsonschema.PropertyNameTag(string(in)),
jsonschema.SkipEmbeddedMapsSlices,
jsonschema.InterceptProperty(func(name string, field reflect.StructField, propertySchema *jsonschema.Schema) error {
s := SchemaOrRef{}
s.FromJSONSchema(propertySchema.ToSchemaOrBool())
Expand Down
75 changes: 75 additions & 0 deletions openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var _ jsonschema.Preparer = &Resp{}

func (r *Resp) PrepareJSONSchema(s *jsonschema.Schema) error {
s.WithExtraPropertiesItem("x-foo", "bar")

return nil
}

Expand Down Expand Up @@ -245,3 +246,77 @@ func TestReflector_SetJSONResponse(t *testing.T) {

assertjson.Equal(t, expected, b)
}

type Identity struct {
ID string `path:"id"`
}

type Data []string

type PathParamAndBody struct {
Identity
Data
}

func TestReflector_SetRequest_pathParamAndBody(t *testing.T) {
reflector := openapi3.Reflector{}
op := openapi3.Operation{}

err := reflector.SetRequest(&op, new(PathParamAndBody), http.MethodPost)
assert.NoError(t, err)

s := reflector.SpecEns()
s.Info.Title = apiName
s.Info.Version = apiVersion
assert.NoError(t, s.AddOperation(http.MethodPost, "/somewhere/{id}", op))

b, err := json.MarshalIndent(s, "", " ")
assert.NoError(t, err)

expected := []byte(`{
"openapi": "3.0.2",
"info": {
"title": "SampleAPI",
"version": "1.2.3"
},
"paths": {
"/somewhere/{id}": {
"post": {
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Openapi3TestPathParamAndBody"
}
}
}
},
"responses": {}
}
}
},
"components": {
"schemas": {
"Openapi3TestPathParamAndBody": {
"type": "array",
"items": {
"type": "string"
},
"nullable": true
}
}
}
}`)

assertjson.Equal(t, expected, b, string(b))
}
2 changes: 1 addition & 1 deletion openapi3/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *Spec) UnmarshalYAML(data []byte) error {

// MarshalYAML produces YAML bytes.
func (s *Spec) MarshalYAML() ([]byte, error) {
//return ya.Marshal(s)
// return ya.Marshal(s)
jsonData, err := s.MarshalJSON()
if err != nil {
return nil, err
Expand Down

0 comments on commit c774271

Please sign in to comment.