Skip to content

Commit

Permalink
Title used a bit less often, which is important for swaggerSchemaObject.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivucica committed May 7, 2016
1 parent 0f189fd commit 9a9e135
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/examplepb/streamless_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
"uint32_value": {
"type": "integer",
"format": "int64",
"description": "TODO(yugui) add bytes_value"
"title": "TODO(yugui) add bytes_value"
},
"uint64_value": {
"type": "integer",
Expand Down
27 changes: 18 additions & 9 deletions protoc-gen-swagger/genswagger/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,18 @@ func applyTemplate(p param) (string, error) {
// will be updated instead. (JSON always gets applied directly to the passed
// object.)
//
// If there is no 'Summary', the same behavior will be attempted on 'Title'.
// If there is no 'Summary', the same behavior will be attempted on 'Title',
// but only if the last character is not a period.
//
// To apply additional Swagger properties, one can pass valid JSON as described
// before. This JSON gets parsed and applied to the passed swaggerObject
// directly. This lets users easily apply custom properties such as contact
// details, API base path, et al.
func updateSwaggerDataFromComments(swaggerObject interface{}, comment string) error {
if len(comment) == 0 {
return nil
}

// Find a section containing additional Swagger metadata.
matches := swaggerExtrasRegexp.FindStringSubmatch(comment)

Expand Down Expand Up @@ -597,8 +602,10 @@ func updateSwaggerDataFromComments(swaggerObject interface{}, comment string) er
// Figure out which properties to update.
summaryValue := infoObjectValue.FieldByName("Summary")
descriptionValue := infoObjectValue.FieldByName("Description")
usingTitle := false
if !summaryValue.CanSet() {
summaryValue = infoObjectValue.FieldByName("Title")
usingTitle = true
}

// If there is a summary (or summary-equivalent), use the first
Expand All @@ -608,16 +615,18 @@ func updateSwaggerDataFromComments(swaggerObject interface{}, comment string) er

summary := strings.TrimSpace(paragraphs[0])
description := strings.TrimSpace(strings.Join(paragraphs[1:], "\n\n"))
if len(summary) > 0 {
summaryValue.Set(reflect.ValueOf(summary))
}
if len(description) > 0 {
if !descriptionValue.CanSet() {
return fmt.Errorf("Object that has Summary but no Description?")
if !usingTitle || summary[len(summary)-1] != '.' {
if len(summary) > 0 {
summaryValue.Set(reflect.ValueOf(summary))
}
descriptionValue.Set(reflect.ValueOf(description))
if len(description) > 0 {
if !descriptionValue.CanSet() {
return fmt.Errorf("Encountered object type with a summary, but no description")
}
descriptionValue.Set(reflect.ValueOf(description))
}
return nil
}
return nil
}

// There was no summary field on the swaggerObject. Try to apply the
Expand Down

0 comments on commit 9a9e135

Please sign in to comment.