diff --git a/parser.go b/parser.go index 11618acf0..f13192fc6 100644 --- a/parser.go +++ b/parser.go @@ -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 } @@ -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 } @@ -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), }, @@ -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{ diff --git a/testdata/nested/api/api.go b/testdata/nested/api/api.go index 8f7ddda13..e7869da85 100644 --- a/testdata/nested/api/api.go +++ b/testdata/nested/api/api.go @@ -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 diff --git a/testdata/nested/expected.json b/testdata/nested/expected.json index c8dba775a..e6ebb1cc4 100644 --- a/testdata/nested/expected.json +++ b/testdata/nested/expected.json @@ -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"