Skip to content

Commit

Permalink
fix: required properties of message type are required in OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
gostajonasson committed Oct 1, 2022
1 parent 6b2cf62 commit d3eab36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 12 additions & 6 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,12 @@ func renderMessageAsDefinition(msg *descriptor.Message, reg *descriptor.Registry
fieldSchema.Required = nil
}

if fieldSchema.Ref != "" {
// Per the JSON Reference syntax: Any members other than "$ref" in a JSON Reference object SHALL be ignored.
// https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3
fieldSchema = openapiSchemaObject{schemaCore: schemaCore{Ref: fieldSchema.Ref}}
}

kv := keyVal{Value: fieldSchema}
kv.Key = reg.FieldName(f)
if schema.Properties == nil {
Expand Down Expand Up @@ -1232,6 +1238,12 @@ func renderServices(services []*descriptor.Service, paths openapiPathsObject, re
} else {
desc = fieldProtoComments(reg, bodyField.Target.Message, bodyField.Target)
}
if schema.Ref != "" {
// Per the JSON Reference syntax: Any members other than "$ref" in a JSON Reference object SHALL be ignored.
// https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3
schema = openapiSchemaObject{schemaCore: schemaCore{Ref: schema.Ref}}
}

}

if meth.GetClientStreaming() {
Expand Down Expand Up @@ -2652,12 +2664,6 @@ func updateswaggerObjectFromJSONSchema(s *openapiSchemaObject, j *openapi_option
}

func updateSwaggerObjectFromFieldBehavior(s *openapiSchemaObject, j []annotations.FieldBehavior, reg *descriptor.Registry, field *descriptor.Field) {
// Per the JSON Reference syntax: Any members other than "$ref" in a JSON Reference object SHALL be ignored.
// https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03#section-3
if s.Ref != "" {
return
}

for _, fb := range j {
switch fb {
case annotations.FieldBehavior_REQUIRED:
Expand Down
3 changes: 2 additions & 1 deletion protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4329,7 +4329,7 @@ func TestSchemaOfField(t *testing.T) {
{
field: &descriptor.Field{
FieldDescriptorProto: &descriptorpb.FieldDescriptorProto{
Name: proto.String("message_field"),
Name: proto.String("required_message_field"),
TypeName: proto.String(".example.Message"),
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE.Enum(),
Options: requiredFieldOptions,
Expand All @@ -4340,6 +4340,7 @@ func TestSchemaOfField(t *testing.T) {
schemaCore: schemaCore{
Ref: "#/definitions/exampleMessage",
},
Required: []string{"required_message_field"},
},
},
{
Expand Down

0 comments on commit d3eab36

Please sign in to comment.