Skip to content

Commit

Permalink
fix: bug for required properties inherited (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensayshi authored and easonlin404 committed Dec 3, 2018
1 parent 1c8533a commit 559d011
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
26 changes: 19 additions & 7 deletions parser.go
Expand Up @@ -432,7 +432,7 @@ func (parser *Parser) ParseDefinition(pkgName, typeName string, typeSpec *ast.Ty
parser.structStack = append(parser.structStack, refTypeName)

log.Println("Generating " + refTypeName)
parser.swagger.Definitions[refTypeName] = parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
parser.swagger.Definitions[refTypeName] = parser.parseTypeExpr(pkgName, typeName, typeSpec.Type, true)
}

func (parser *Parser) collectRequiredFields(pkgName string, properties map[string]spec.Schema) (requiredFields []string) {
Expand All @@ -457,7 +457,6 @@ func (parser *Parser) collectRequiredFields(pkgName string, properties map[strin
}
if tname != "object" {
requiredFields = append(requiredFields, prop.SchemaProps.Required...)
prop.SchemaProps.Required = make([]string, 0)
}
properties[k] = prop
}
Expand All @@ -474,7 +473,7 @@ func fullTypeName(pkgName, typeName string) string {

// parseTypeExpr parses given type expression that corresponds to the type under
// given name and package, and returns swagger schema for it.
func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr) spec.Schema {
func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr, flattenRequired bool) spec.Schema {
switch expr := typeExpr.(type) {
// type Foo struct {...}
case *ast.StructType:
Expand All @@ -497,11 +496,24 @@ func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr)
}
}

required := parser.collectRequiredFields(pkgName, properties)

// unset required from properties because we've aggregated them
if flattenRequired {
for k, prop := range properties {
tname := prop.SchemaProps.Type[0]
if tname != "object" {
prop.SchemaProps.Required = make([]string, 0)
}
properties[k] = prop
}
}

return spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: properties,
Required: parser.collectRequiredFields(pkgName, properties),
Required: required,
},
}

Expand All @@ -516,11 +528,11 @@ func (parser *Parser) parseTypeExpr(pkgName, typeName string, typeExpr ast.Expr)

// type Foo *Baz
case *ast.StarExpr:
return parser.parseTypeExpr(pkgName, typeName, expr.X)
return parser.parseTypeExpr(pkgName, typeName, expr.X, true)

// type Foo []Baz
case *ast.ArrayType:
itemSchema := parser.parseTypeExpr(pkgName, "", expr.Elt)
itemSchema := parser.parseTypeExpr(pkgName, "", expr.Elt, true)
return spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"array"},
Expand Down Expand Up @@ -734,7 +746,7 @@ func (parser *Parser) parseAnonymousField(pkgName string, field *ast.Field) map[
}

typeSpec := parser.TypeDefinitions[pkgName][typeName]
schema := parser.parseTypeExpr(pkgName, typeName, typeSpec.Type)
schema := parser.parseTypeExpr(pkgName, typeName, typeSpec.Type, false)

schemaType := "unknown"
if len(schema.SchemaProps.Type) > 0 {
Expand Down
15 changes: 15 additions & 0 deletions parser_test.go
Expand Up @@ -681,6 +681,21 @@ func TestParseSimpleApi(t *testing.T) {
}
}
},
"web.Pet5": {
"type": "object",
"required": [
"name",
"odd"
],
"properties": {
"name": {
"type": "string"
},
"odd": {
"type": "boolean"
}
}
},
"web.RevValue": {
"type": "object",
"properties": {
Expand Down
5 changes: 5 additions & 0 deletions testdata/simple/api/api.go
Expand Up @@ -92,3 +92,8 @@ func AnonymousStructArray() {
type Pet3 struct {
ID int `json:"id"`
}

// @Success 200 {object} web.Pet5 "ok"
func GetPet5() {

}
9 changes: 9 additions & 0 deletions testdata/simple/web/handler.go
Expand Up @@ -78,3 +78,12 @@ type RevValue struct {
Cross cross.Cross `json:"cross"`
Crosses []cross.Cross `json:"crosses"`
}

type Pet4 struct {
Name string `json:"name" binding:"required"`
}

type Pet5 struct {
*Pet4
Odd bool `json:"odd" binding:"required"`
}

0 comments on commit 559d011

Please sign in to comment.