Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Comments for google types (#68)
Browse files Browse the repository at this point in the history
* Well-known types were skipping descriptions

* Making comments work for google wellknown protos

Co-authored-by: Chrusty <>
  • Loading branch information
chrusty authored Jul 20, 2021
1 parent 0f15c52 commit b140dad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion internal/converter/testdata/google_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const GoogleValue = `{
{
"type": "string"
}
]
],
"description": "` + "`Value`" + ` represents a dynamically typed value which can be either\n null, a number, a string, a boolean, a recursive struct value, or a\n list of values. A producer of value is expected to set one of that\n variants, absence of any variant indicates an error.\n\n The JSON representation for ` + "`Value`" + ` is JSON value."
}
},
"additionalProperties": true,
Expand Down
1 change: 1 addition & 0 deletions internal/converter/testdata/proto/WellKnown.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ message WellKnown {
map<int32, google.protobuf.Int32Value> map_of_integers = 2;
map<int32, int32> map_of_scalar_integers = 3;
repeated google.protobuf.Int32Value list_of_integers = 4;
// This is a duration:
google.protobuf.Duration duration = 5;
}

6 changes: 4 additions & 2 deletions internal/converter/testdata/wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ const WellKnown = `{
},
"list_of_integers": {
"items": {
"type": "integer"
"type": "integer",
"description": "Wrapper message for ` + "`int32`" + `.\n\n The JSON representation for ` + "`Int32Value`" + ` is JSON number."
},
"type": "array"
},
"duration": {
"additionalProperties": true,
"type": "string"
"type": "string",
"description": "This is a duration:"
}
},
"additionalProperties": true,
Expand Down
43 changes: 22 additions & 21 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,58 +416,58 @@ func (c *Converter) recursiveFindDuplicatedNestedMessages(curPkg *ProtoPackage,

func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msg *descriptor.DescriptorProto, pkgName string, duplicatedMessages map[*descriptor.DescriptorProto]string, ignoreDuplicatedMessages bool) (*jsonschema.Type, error) {

// Prepare a new jsonschema:
jsonSchemaType := new(jsonschema.Type)

// Generate a description from src comments (if available)
if src := c.sourceInfo.GetMessage(msg); src != nil {
jsonSchemaType.Description = formatDescription(src)
}

// Handle google's well-known types:
if msg.Name != nil && wellKnownTypes[*msg.Name] && pkgName == ".google.protobuf" {
var wellKnownSchema = new(jsonschema.Type)
switch *msg.Name {
case "DoubleValue", "FloatValue":
wellKnownSchema.Type = gojsonschema.TYPE_NUMBER
jsonSchemaType.Type = gojsonschema.TYPE_NUMBER
case "Int32Value", "UInt32Value", "Int64Value", "UInt64Value":
wellKnownSchema.Type = gojsonschema.TYPE_INTEGER
jsonSchemaType.Type = gojsonschema.TYPE_INTEGER
case "BoolValue":
wellKnownSchema.Type = gojsonschema.TYPE_BOOLEAN
jsonSchemaType.Type = gojsonschema.TYPE_BOOLEAN
case "BytesValue", "StringValue":
wellKnownSchema.Type = gojsonschema.TYPE_STRING
jsonSchemaType.Type = gojsonschema.TYPE_STRING
case "Value":
wellKnownSchema.OneOf = []*jsonschema.Type{
jsonSchemaType.OneOf = []*jsonschema.Type{
{Type: gojsonschema.TYPE_ARRAY},
{Type: gojsonschema.TYPE_BOOLEAN},
{Type: gojsonschema.TYPE_NUMBER},
{Type: gojsonschema.TYPE_OBJECT},
{Type: gojsonschema.TYPE_STRING},
}
case "Duration":
wellKnownSchema.Type = gojsonschema.TYPE_STRING
jsonSchemaType.Type = gojsonschema.TYPE_STRING
}

// If we're allowing nulls then prepare a OneOf:
if c.Flags.AllowNullValues {
wellKnownSchema.OneOf = append(wellKnownSchema.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_NULL}, &jsonschema.Type{Type: wellKnownSchema.Type})
return wellKnownSchema, nil
jsonSchemaType.OneOf = append(jsonSchemaType.OneOf, &jsonschema.Type{Type: gojsonschema.TYPE_NULL}, &jsonschema.Type{Type: jsonSchemaType.Type})
return jsonSchemaType, nil
}

// Otherwise just return this simple type:
return wellKnownSchema, nil
return jsonSchemaType, nil
}

// Set defaults:
jsonSchemaType.Properties = orderedmap.New()
jsonSchemaType.Version = jsonschema.Version

if refName, ok := duplicatedMessages[msg]; ok && !ignoreDuplicatedMessages {
return &jsonschema.Type{
Version: jsonschema.Version,
Ref: refName,
}, nil
}

// Prepare a new jsonschema:
jsonSchemaType := &jsonschema.Type{
Properties: orderedmap.New(),
Version: jsonschema.Version,
}

// Generate a description from src comments (if available)
if src := c.sourceInfo.GetMessage(msg); src != nil {
jsonSchemaType.Description = formatDescription(src)
}

// Optionally allow NULL values:
if c.Flags.AllowNullValues {
jsonSchemaType.OneOf = []*jsonschema.Type{
Expand Down Expand Up @@ -537,6 +537,7 @@ func formatDescription(sl *descriptor.SourceCodeInfo_Location) string {
if s := strings.TrimSpace(sl.GetTrailingComments()); s != "" {
lines = append(lines, s)
}

return strings.Join(lines, "\n\n")
}

Expand Down

0 comments on commit b140dad

Please sign in to comment.