Skip to content

Commit

Permalink
Merge pull request #311 from mikedanese/fix-byte
Browse files Browse the repository at this point in the history
swagger: report golang type []byte as json type string
  • Loading branch information
emicklei authored Aug 14, 2016
2 parents dd9d28b + 1c48722 commit 89ef8af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 13 additions & 0 deletions swagger/model_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ func (b modelBuilder) addModel(st reflect.Type, nameOverride string) *Model {
if b.isPrimitiveType(modelName) {
return nil
}
// golang encoding/json packages says array and slice values encode as
// JSON arrays, except that []byte encodes as a base64-encoded string.
// If we see a []byte here, treat it at as a primitive type (string)
// and deal with it in buildArrayTypeProperty.
if (st.Kind() == reflect.Slice || st.Kind() == reflect.Array) &&
st.Elem().Kind() == reflect.Uint8 {
return nil
}
// see if we already have visited this model
if _, ok := b.Models.At(modelName); ok {
return nil
Expand Down Expand Up @@ -276,6 +284,11 @@ func (b modelBuilder) buildArrayTypeProperty(field reflect.StructField, jsonName
return jsonName, prop
}
fieldType := field.Type
if fieldType.Elem().Kind() == reflect.Uint8 {
stringt := "string"
prop.Type = &stringt
return jsonName, prop
}
var pType = "array"
prop.Type = &pType
isPrimitive := b.isPrimitiveType(fieldType.Elem().Name())
Expand Down
5 changes: 1 addition & 4 deletions swagger/model_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,7 @@ func TestRegion_Issue113(t *testing.T) {
],
"properties": {
"id": {
"type": "array",
"items": {
"type": "integer"
}
"type": "string"
},
"name": {
"type": "string"
Expand Down

0 comments on commit 89ef8af

Please sign in to comment.