Skip to content

Commit

Permalink
support for read only, test by adding read only attribute to id of pet (
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens van den Brink authored and ubogdan committed Sep 23, 2019
1 parent b09d3f5 commit efcfb3d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
25 changes: 21 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ type structField struct {
arrayType string
formatType string
isRequired bool
readOnly bool
crossPkg string
exampleValue interface{}
maximum *float64
Expand Down Expand Up @@ -835,6 +836,9 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
Ref: jsonreference.MustCreateRef("#/definitions/" + pkgName + "." + structField.schemaType),
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
ReadOnly: structField.readOnly,
},
}
} else if structField.schemaType == "array" { // array field type
// if defined -- ref it
Expand All @@ -855,6 +859,9 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
},
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
ReadOnly: structField.readOnly,
},
}
} else if structField.arrayType == "object" {
// Anonymous struct
Expand Down Expand Up @@ -883,7 +890,11 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
},
},
},
}}
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
ReadOnly: structField.readOnly,
},
}
}
}
} else {
Expand Down Expand Up @@ -914,7 +925,8 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: structField.exampleValue,
Example: structField.exampleValue,
ReadOnly: structField.readOnly,
},
}
}
Expand All @@ -937,7 +949,8 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
Default: structField.defaultValue,
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: structField.exampleValue,
Example: structField.exampleValue,
ReadOnly: structField.readOnly,
},
VendorExtensible: spec.VendorExtensible{
Extensions: structField.extensions,
Expand Down Expand Up @@ -989,7 +1002,8 @@ func (parser *Parser) parseStructField(pkgName string, field *ast.Field) (map[st
Default: structField.defaultValue,
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: structField.exampleValue,
Example: structField.exampleValue,
ReadOnly: structField.readOnly,
},
}
}
Expand Down Expand Up @@ -1185,6 +1199,9 @@ func (parser *Parser) parseField(field *ast.Field) (*structField, error) {
}
structField.minLength = minLength
}
if readOnly := structTag.Get("readonly"); readOnly != "" {
structField.readOnly = readOnly == "true"
}

return structField, nil
}
Expand Down
1 change: 1 addition & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ func TestParseSimpleApi1(t *testing.T) {
"id": {
"type": "integer",
"format": "int64",
"readOnly": true,
"example": 1
},
"int_array": {
Expand Down
2 changes: 1 addition & 1 deletion testdata/simple/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Pet struct {
ID int `json:"id" example:"1" format:"int64"`
ID int `json:"id" example:"1" format:"int64" readonly:"true"`
Category struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"category_name"`
Expand Down

0 comments on commit efcfb3d

Please sign in to comment.