From 2cdbce3a7945fff2f27ad2b98a4ddbf2e47fe5f5 Mon Sep 17 00:00:00 2001 From: Francois Vanderkelen Date: Wed, 14 Oct 2020 17:21:15 +0200 Subject: [PATCH 1/3] wip --- docparser/model.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/docparser/model.go b/docparser/model.go index 1337012..10e6769 100644 --- a/docparser/model.go +++ b/docparser/model.go @@ -374,14 +374,23 @@ func replaceSchemaNameToCustom(s *schema) { if len(refSplit) != 4 { return } + if replacementSchema, found := registeredSchemas[refSplit[3]]; found { - meta, ok := replacementSchema.(metaSchema) + meta, ok := replacementSchema.(*schema) if !ok { return } - refSplit[3] = meta.CustomName() + switch meta.Type { + case "array": + s.Ref = meta.Ref + s.Items = meta.Items + s.Type = meta.Type + break + default: + refSplit[3] = meta.CustomName() + s.Ref = strings.Join(refSplit, "/") + } } - s.Ref = strings.Join(refSplit, "/") } func (spec *openAPI) composeSpecSchemas() { From ed3ff622474fc151ec4f350ce202e5ed0c67da0d Mon Sep 17 00:00:00 2001 From: Francois Vanderkelen Date: Thu, 15 Oct 2020 09:07:42 +0200 Subject: [PATCH 2/3] feat: Remove unused file from parseNamedType --- docparser/model.go | 8 ++++---- docparser/parser.go | 14 +++++++------- docparser/parser_test.go | 30 ++---------------------------- 3 files changed, 13 insertions(+), 39 deletions(-) diff --git a/docparser/model.go b/docparser/model.go index 10e6769..9ec2ef2 100644 --- a/docparser/model.go +++ b/docparser/model.go @@ -468,7 +468,7 @@ func (spec *openAPI) parseStructs(f *ast.File, tpe *ast.StructType) (interface{} e.Required = append(e.Required, j.name) } - p, err := parseNamedType(f, fld.Type, nil) + p, err := parseNamedType(fld.Type, nil) if err != nil { logrus.WithError(err).WithField("field", fld.Names[0]).Error("Can't parse the type of field in struct") errors = append(errors, BuildError{ @@ -495,7 +495,7 @@ func (spec *openAPI) parseStructs(f *ast.File, tpe *ast.StructType) (interface{} } } - p, err := parseNamedType(f, fld.Type, nil) + p, err := parseNamedType(fld.Type, nil) if err != nil { logrus.WithError(err).WithField("field", fld.Type).Error("Can't parse the type of composed field in struct") errors = append(errors, BuildError{ @@ -576,7 +576,7 @@ func (spec *openAPI) parseSchemas(f *ast.File) (errors []error) { case *ast.ArrayType: e := newEntity() - p, err := parseNamedType(f, n.Elt, nil) + p, err := parseNamedType(n.Elt, nil) if err != nil { logrus.WithError(err).Error("Can't parse the type of field in struct") errors = append(errors, &BuildError{ @@ -595,7 +595,7 @@ func (spec *openAPI) parseSchemas(f *ast.File) (errors []error) { entity = &e default: - p, err := parseNamedType(f, ts.Type, nil) + p, err := parseNamedType(ts.Type, nil) if err != nil { logrus.WithError(err).Error("can't parse custom type") errors = append(errors, BuildError{ diff --git a/docparser/parser.go b/docparser/parser.go index a093afa..af22535 100644 --- a/docparser/parser.go +++ b/docparser/parser.go @@ -78,7 +78,7 @@ func parseJSONTag(field *ast.Field) (j jsonTagInfo, err error) { return j, nil } -func parseNamedType(gofile *ast.File, expr ast.Expr, sel *ast.Ident) (*schema, error) { +func parseNamedType(expr ast.Expr, sel *ast.Ident) (*schema, error) { p := schema{} switch ftpe := expr.(type) { case *ast.Ident: // simple value @@ -98,7 +98,7 @@ func parseNamedType(gofile *ast.File, expr ast.Expr, sel *ast.Ident) (*schema, e p.Format = format return &p, nil case *ast.StarExpr: // pointer to something, optional by default - t, err := parseNamedType(gofile, ftpe.X, sel) + t, err := parseNamedType(ftpe.X, sel) if err != nil { return nil, err } @@ -109,7 +109,7 @@ func parseNamedType(gofile *ast.File, expr ast.Expr, sel *ast.Ident) (*schema, e } return t, nil case *ast.ArrayType: // slice type - cp, err := parseNamedType(gofile, ftpe.Elt, sel) + cp, err := parseNamedType(ftpe.Elt, sel) if err != nil { return nil, err } @@ -144,7 +144,7 @@ func parseNamedType(gofile *ast.File, expr ast.Expr, sel *ast.Ident) (*schema, e return nil, err } - pnt, err := parseNamedType(gofile, field.Type, nil) + pnt, err := parseNamedType(field.Type, nil) if err != nil { return nil, err } @@ -155,15 +155,15 @@ func parseNamedType(gofile *ast.File, expr ast.Expr, sel *ast.Ident) (*schema, e return &p, nil case *ast.SelectorExpr: - t, err := parseNamedType(gofile, ftpe.X, ftpe.Sel) + t, err := parseNamedType(ftpe.X, ftpe.Sel) if err != nil { return nil, err } return t, nil case *ast.MapType: - k, kerr := parseNamedType(gofile, ftpe.Key, sel) - v, verr := parseNamedType(gofile, ftpe.Value, sel) + k, kerr := parseNamedType(ftpe.Key, sel) + v, verr := parseNamedType(ftpe.Value, sel) if kerr != nil || verr != nil || k.Type != "string" { // keys can only be of type string return nil, fmt.Errorf("expr (%s) not yet unsupported", expr) diff --git a/docparser/parser_test.go b/docparser/parser_test.go index eb9fae5..d9331a8 100644 --- a/docparser/parser_test.go +++ b/docparser/parser_test.go @@ -139,32 +139,6 @@ func TestParseNamedType(t *testing.T) { }, }}, }, - { - description: "Should *ast.ArrayType with *ast.StructType", - expr: &ast.ArrayType{ - Elt: &ast.StructType{ - Fields: &ast.FieldList{ - List: []*ast.Field{ - { - Type: &ast.Ident{Name: "string"}, - Tag: &ast.BasicLit{Value: "`json:\"str\"`"}, - }, - }, - }, - }, - }, - expectedSchema: &schema{ - Type: "array", - Items: map[string]interface{}{ - "type": "object", - "properties": map[string]*schema{ - "str": { - Type: "string", - }, - }, - }, - }, - }, { description: "Should *ast.StructType to anonymous struct", expr: &ast.StructType{ @@ -279,7 +253,7 @@ func TestParseNamedType(t *testing.T) { } for _, tc := range testCases { t.Run(tc.description, func(t *testing.T) { - schema, err := parseNamedType(tc.gofile, tc.expr, nil) + schema, err := parseNamedType(tc.expr, nil) if len(tc.expectedError) > 0 { if (err != nil) && (err.Error() != tc.expectedError) { t.Errorf("got error: %v, wantErr: %v", err, tc.expectedError) @@ -547,4 +521,4 @@ func TestParseIdentProperty(t *testing.T) { } }) } -} +} \ No newline at end of file From 01dbd97d5e83eadd8573509a3278bdda96c5007b Mon Sep 17 00:00:00 2001 From: Francois Vanderkelen Date: Thu, 15 Oct 2020 10:02:56 +0200 Subject: [PATCH 3/3] fix: Revert test removal --- docparser/parser_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docparser/parser_test.go b/docparser/parser_test.go index d9331a8..ef56600 100644 --- a/docparser/parser_test.go +++ b/docparser/parser_test.go @@ -250,6 +250,32 @@ func TestParseNamedType(t *testing.T) { expr: &ast.FuncType{}, expectedError: "expr (&{%!s(token.Pos=0) %!s(*ast.FieldList=) %!s(*ast.FieldList=)}) type (&{%!s(token.Pos=0) %!s(*ast.FieldList=) %!s(*ast.FieldList=)}) is unsupported for a schema", }, + { + description: "Should *ast.ArrayType with *ast.StructType", + expr: &ast.ArrayType{ + Elt: &ast.StructType{ + Fields: &ast.FieldList{ + List: []*ast.Field{ + { + Type: &ast.Ident{Name: "string"}, + Tag: &ast.BasicLit{Value: "`json:\"str\"`"}, + }, + }, + }, + }, + }, + expectedSchema: &schema{ + Type: "array", + Items: map[string]interface{}{ + "type": "object", + "properties": map[string]*schema{ + "str": { + Type: "string", + }, + }, + }, + }, + }, } for _, tc := range testCases { t.Run(tc.description, func(t *testing.T) {