Skip to content

Commit

Permalink
fix: issue while parsing map alias (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
x0pla-adeo authored Sep 22, 2023
1 parent 149c4f9 commit 2dab208
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
3 changes: 3 additions & 0 deletions docparser/datatest/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// GetUser returns a user corresponding to specified id
// @openapi:path
// /pets:
//
// get:
// description: "Returns all pets from the system that the user has access to"
// operationId: GetUser
Expand Down Expand Up @@ -63,6 +64,7 @@ func GetUser() {}
// PostFoo returns a user corresponding to specified id
// @openapi:path
// /pets:
//
// post:
// description: "Returns all pets from the system that the user has access to"
// requestBody:
Expand Down Expand Up @@ -114,6 +116,7 @@ type Pet struct {
StrData map[string]string `json:"strData"`
Children map[string]Pet `json:"children"`
IntData map[string]int `json:"IntData"`
PtrStringMapAlias *MapStringString `json:"PtrStringMapAlias"`
ByteData []byte `json:"ByteData"`
JSONData json.RawMessage `json:"json_data"`
CustomString otherpackage.CustomString `json:"custom_string"`
Expand Down
38 changes: 13 additions & 25 deletions docparser/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ func (spec *openAPI) replaceSchemaNameToCustom(s *schema) {
if !ok {
return
}
refSplit[3] = meta.CustomName()
if meta.CustomName() != "" {
refSplit[3] = meta.CustomName()
}
}
s.Ref = strings.Join(refSplit, "/")
}
Expand Down Expand Up @@ -416,34 +418,20 @@ func (spec *openAPI) composeSpecSchemas() {
}
}

func (spec *openAPI) parseMaps(mp *ast.MapType) (*schema, []error) {
func (spec *openAPI) parseMaps(f *ast.File, mp *ast.MapType) (*schema, []error) {
errors := make([]error, 0)

// only map[string]
if i, ok := mp.Key.(*ast.Ident); ok {
t, _, err := parseIdentProperty(i)
if err != nil {
errors = append(errors, BuildError{
Err: err,
Message: "could not parse ident property from map",
})
}

if t != "string" {
return nil, errors
}
p, err := parseNamedType(f, mp, nil)
if err != nil {
logrus.WithError(err).Error("Can't parse the type of field in map")
errors = append(errors, BuildError{
Err: err,
Message: "can't parse the type of field in map",
})
}

e := newEntity()
e.Type = "object"
e.AdditionalProperties = &schema{}

// map[string]interface{}
if _, ok := mp.Value.(*ast.InterfaceType); ok {
return &e, errors
}
return p, errors

return nil, errors
}

func (spec *openAPI) parseStructs(f *ast.File, tpe *ast.StructType) (interface{}, []error) {
Expand Down Expand Up @@ -575,7 +563,7 @@ func (spec *openAPI) parseSchemas(f *ast.File) (errors []error) {
switch n := ts.Type.(type) {
case *ast.MapType:
var errs []error
entity, errs = spec.parseMaps(n)
entity, errs = spec.parseMaps(f, n)
if len(errs) != 0 {
errors = append(errors, errs...)
}
Expand Down

0 comments on commit 2dab208

Please sign in to comment.