Skip to content

Commit

Permalink
fix: required tag doesn't work with nested object property #504 (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
takushi-m authored and easonlin404 committed Oct 25, 2019
1 parent ab69c23 commit c05c273
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
19 changes: 12 additions & 7 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,7 @@ func (parser *Parser) collectRequiredFields(pkgName string, properties map[strin
tspec := parser.TypeDefinitions[pkgName][tname]
parser.ParseDefinition(pkgName, tname, tspec)
}
if tname != "object" {
requiredFields = append(requiredFields, prop.SchemaProps.Required...)
}
requiredFields = append(requiredFields, prop.SchemaProps.Required...)
properties[k] = prop
}

Expand Down Expand Up @@ -730,10 +728,7 @@ func (parser *Parser) parseStruct(pkgName string, fields *ast.FieldList) (spec.S

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

Expand Down Expand Up @@ -828,10 +823,15 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
// write definition if not yet present
parser.ParseDefinition(pkgName, structField.schemaType,
parser.TypeDefinitions[pkgName][structField.schemaType])
required := make([]string, 0)
if structField.isRequired {
required = append(required, structField.name)
}
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"}, // to avoid swagger validation error
Description: desc,
Required: required,
Ref: spec.Ref{
Ref: jsonreference.MustCreateRef("#/definitions/" + pkgName + "." + structField.schemaType),
},
Expand All @@ -845,10 +845,15 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
if _, ok := parser.TypeDefinitions[pkgName][structField.arrayType]; ok { // user type in array
parser.ParseDefinition(pkgName, structField.arrayType,
parser.TypeDefinitions[pkgName][structField.arrayType])
required := make([]string, 0)
if structField.isRequired {
required = append(required, structField.name)
}
properties[structField.name] = spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{structField.schemaType},
Description: desc,
Required: required,
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Expand Down
9 changes: 8 additions & 1 deletion testdata/nested/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import (
)

type Foo struct {
Field1 string
Field1 string `validate:"required"`
OutsideData *nested2.Body
InsideData Bar `validate:"required"`
ArrayField1 []string `validate:"required"`
ArrayField2 []Bar `validate:"required"`
}

type Bar struct {
Field string
}

// @Description get Foo
Expand Down
30 changes: 30 additions & 0 deletions testdata/nested/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,42 @@
}
},
"definitions": {
"api.Bar": {
"type": "object",
"properties": {
"field": {
"type": "string"
}
}
},
"api.Foo": {
"type": "object",
"required": [
"arrayField1",
"arrayField2",
"field1",
"insideData"
],
"properties": {
"arrayField1": {
"type": "array",
"items": {
"type": "string"
}
},
"arrayField2": {
"type": "array",
"items": {
"$ref": "#/definitions/api.Bar"
}
},
"field1": {
"type": "string"
},
"insideData": {
"type": "object",
"$ref": "#/definitions/api.Bar"
},
"outsideData": {
"type": "object",
"$ref": "#/definitions/nested2.Body"
Expand Down

0 comments on commit c05c273

Please sign in to comment.