Skip to content

Commit

Permalink
Use collectionFormat multi for query params of repeated fields
Browse files Browse the repository at this point in the history
Solves #756
  • Loading branch information
bmperrea committed Mar 10, 2019
1 parent 9bb0d96 commit 79316f1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 48 deletions.
20 changes: 11 additions & 9 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,14 @@ func queryParams(message *descriptor.Message, field *descriptor.Field, prefix st
}

param := swaggerParameterObject{
Description: desc,
In: "query",
Default: schema.Default,
Type: schema.Type,
Items: schema.Items,
Format: schema.Format,
Required: required,
Description: desc,
In: "query",
Default: schema.Default,
Type: schema.Type,
Items: schema.Items,
Format: schema.Format,
CollectionFormat: schema.CollectionFormat,
Required: required,
}

if reg.GetUseJSONNamesForFields() {
Expand Down Expand Up @@ -423,6 +424,7 @@ func schemaOfField(f *descriptor.Field, reg *descriptor.Registry, refs refMap) s
Type: "array",
Items: (*swaggerItemsObject)(&core),
},
CollectionFormat: "multi",
}
case object:
ret = swaggerSchemaObject{
Expand Down Expand Up @@ -1255,9 +1257,9 @@ func updateSwaggerDataFromComments(swaggerObject interface{}, comment string, is
}
// overrides the schema value only if it's empty
// keep the comment precedence when updating the package definition
if descriptionValue.Len() == 0 || isPackageObject {
if descriptionValue.Len() == 0 || isPackageObject {
descriptionValue.Set(reflect.ValueOf(description))
}
}
}
return nil
}
Expand Down
94 changes: 55 additions & 39 deletions protoc-gen-swagger/genswagger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func TestMessageToQueryParameters(t *testing.T) {
Type: protodescriptor.FieldDescriptorProto_TYPE_DOUBLE.Enum(),
Number: proto.Int32(2),
},
{
Name: proto.String("c"),
Type: protodescriptor.FieldDescriptorProto_TYPE_STRING.Enum(),
Label: protodescriptor.FieldDescriptorProto_LABEL_REPEATED.Enum(),
Number: proto.Int32(3),
},
},
},
},
Expand All @@ -76,6 +82,13 @@ func TestMessageToQueryParameters(t *testing.T) {
Type: "number",
Format: "double",
},
swaggerParameterObject{
Name: "c",
In: "query",
Required: false,
Type: "array",
CollectionFormat: "multi",
},
},
},
{
Expand Down Expand Up @@ -192,6 +205,10 @@ func TestMessageToQueryParameters(t *testing.T) {
if err != nil {
t.Fatalf("failed to convert message to query parameters: %s", err)
}
// avoid checking Items for array types
for i := range params {
params[i].Items = nil
}
if !reflect.DeepEqual(params, test.Params) {
t.Errorf("expected %v, got %v", test.Params, params)
}
Expand Down Expand Up @@ -1505,106 +1522,105 @@ func TestProtoComments(t *testing.T) {
func TestUpdateSwaggerDataFromComments(t *testing.T) {

tests := []struct {
descr string
swaggerObject interface{}
comments string
expectedError error
expectedSwaggerObject interface{}
descr string
swaggerObject interface{}
comments string
expectedError error
expectedSwaggerObject interface{}
}{
{
descr: "empty comments",
swaggerObject: nil,
descr: "empty comments",
swaggerObject: nil,
expectedSwaggerObject: nil,
comments: "",
expectedError: nil,
comments: "",
expectedError: nil,
},
{
descr: "set field to read only",
descr: "set field to read only",
swaggerObject: &swaggerSchemaObject{},
expectedSwaggerObject: &swaggerSchemaObject{
ReadOnly: true,
ReadOnly: true,
Description: "... Output only. ...",
},
comments: "... Output only. ...",
comments: "... Output only. ...",
expectedError: nil,
},
{
descr: "set title",
descr: "set title",
swaggerObject: &swaggerSchemaObject{},
expectedSwaggerObject: &swaggerSchemaObject{
Title: "Comment with no trailing dot",
},
comments: "Comment with no trailing dot",
comments: "Comment with no trailing dot",
expectedError: nil,
},
{
descr: "set description",
descr: "set description",
swaggerObject: &swaggerSchemaObject{},
expectedSwaggerObject: &swaggerSchemaObject{
Description: "Comment with trailing dot.",
},
comments: "Comment with trailing dot.",
comments: "Comment with trailing dot.",
expectedError: nil,
},
{
descr: "use info object",
swaggerObject: &swaggerObject{
Info: swaggerInfoObject{
},
Info: swaggerInfoObject{},
},
expectedSwaggerObject: &swaggerObject{
Info: swaggerInfoObject{
Description: "Comment with trailing dot.",
},
},
comments: "Comment with trailing dot.",
comments: "Comment with trailing dot.",
expectedError: nil,
},
{
descr: "multi line comment with title",
descr: "multi line comment with title",
swaggerObject: &swaggerSchemaObject{},
expectedSwaggerObject: &swaggerSchemaObject {
Title: "First line",
expectedSwaggerObject: &swaggerSchemaObject{
Title: "First line",
Description: "Second line",
},
comments: "First line\n\nSecond line",
comments: "First line\n\nSecond line",
expectedError: nil,
},
{
descr: "multi line comment no title",
descr: "multi line comment no title",
swaggerObject: &swaggerSchemaObject{},
expectedSwaggerObject: &swaggerSchemaObject {
expectedSwaggerObject: &swaggerSchemaObject{
Description: "First line.\n\nSecond line",
},
comments: "First line.\n\nSecond line",
comments: "First line.\n\nSecond line",
expectedError: nil,
},
{
descr: "multi line comment with summary with dot",
descr: "multi line comment with summary with dot",
swaggerObject: &swaggerOperationObject{},
expectedSwaggerObject: &swaggerOperationObject {
Summary: "First line.",
expectedSwaggerObject: &swaggerOperationObject{
Summary: "First line.",
Description: "Second line",
},
comments: "First line.\n\nSecond line",
comments: "First line.\n\nSecond line",
expectedError: nil,
},
{
descr: "multi line comment with summary no dot",
descr: "multi line comment with summary no dot",
swaggerObject: &swaggerOperationObject{},
expectedSwaggerObject: &swaggerOperationObject {
Summary: "First line",
expectedSwaggerObject: &swaggerOperationObject{
Summary: "First line",
Description: "Second line",
},
comments: "First line\n\nSecond line",
comments: "First line\n\nSecond line",
expectedError: nil,
},
{
descr: "multi line comment with summary no dot",
swaggerObject: &schemaCore{},
descr: "multi line comment with summary no dot",
swaggerObject: &schemaCore{},
expectedSwaggerObject: &schemaCore{},
comments: "Any comment",
expectedError: errors.New("no description nor summary property"),
comments: "Any comment",
expectedError: errors.New("no description nor summary property"),
},
}

Expand Down Expand Up @@ -1632,4 +1648,4 @@ func TestUpdateSwaggerDataFromComments(t *testing.T) {
}
})
}
}
}
1 change: 1 addition & 0 deletions protoc-gen-swagger/genswagger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ type swaggerSchemaObject struct {
MaxProperties uint64 `json:"maxProperties,omitempty"`
MinProperties uint64 `json:"minProperties,omitempty"`
Required []string `json:"required,omitempty"`
CollectionFormat string `json:"collectionFormat,omitempty"`
}

// http://swagger.io/specification/#referenceObject
Expand Down

0 comments on commit 79316f1

Please sign in to comment.