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

Commit

Permalink
Further oneof required investigation (#80)
Browse files Browse the repository at this point in the history
* Many experiments

* Nested objects work with all-required and enforce-oneof

* Referenced objects now have required fields too

* Updated one unit test response

Co-authored-by: chris <chris@Profanity.local>
  • Loading branch information
chrusty and chris authored Sep 20, 2021
1 parent 6ac766b commit 65eca57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/converter/testdata/proto2_nested_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const Proto2NestedObject = `{
"payload": {
"required": [
"name",
"id",
"timestamp",
"id",
"rating",
"complete",
"topology"
Expand Down
15 changes: 13 additions & 2 deletions internal/converter/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,12 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.Type = gojsonschema.TYPE_ARRAY

// Build up the list of required fields:
if c.Flags.AllFieldsRequired && recursedJSONSchemaType.Properties != nil {
if c.Flags.AllFieldsRequired && len(recursedJSONSchemaType.OneOf) == 0 && recursedJSONSchemaType.Properties != nil {
for _, property := range recursedJSONSchemaType.Properties.Keys() {
jsonSchemaType.Items.Required = append(jsonSchemaType.Items.Required, property)
}
}
jsonSchemaType.Items.Required = dedupe(jsonSchemaType.Items.Required)

// Not maps, not arrays:
default:
Expand All @@ -299,7 +300,7 @@ func (c *Converter) convertField(curPkg *ProtoPackage, desc *descriptor.FieldDes
jsonSchemaType.Required = recursedJSONSchemaType.Required

// Build up the list of required fields:
if c.Flags.AllFieldsRequired && recursedJSONSchemaType.Properties != nil {
if c.Flags.AllFieldsRequired && len(recursedJSONSchemaType.OneOf) == 0 && recursedJSONSchemaType.Properties != nil {
for _, property := range recursedJSONSchemaType.Properties.Keys() {
jsonSchemaType.Required = append(jsonSchemaType.Required, property)
}
Expand Down Expand Up @@ -526,6 +527,13 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msg *descr
jsonSchemaType.Properties.Set(fieldDesc.GetName(), recursedJSONSchemaType)
}

// Enforce all_fields_required:
if c.Flags.AllFieldsRequired && len(jsonSchemaType.OneOf) == 0 && jsonSchemaType.Properties != nil {
for _, property := range jsonSchemaType.Properties.Keys() {
jsonSchemaType.Required = append(jsonSchemaType.Required, property)
}
}

// Look for required fields by the proto2 "required" flag:
if fieldDesc.GetLabel() == descriptor.FieldDescriptorProto_LABEL_REQUIRED && fieldDesc.OneofIndex == nil {
jsonSchemaType.Required = append(jsonSchemaType.Required, fieldDesc.GetName())
Expand All @@ -543,6 +551,9 @@ func (c *Converter) recursiveConvertMessageType(curPkg *ProtoPackage, msg *descr
jsonSchemaType.Properties = nil
}

// Dedupe required fields:
jsonSchemaType.Required = dedupe(jsonSchemaType.Required)

return jsonSchemaType, nil
}

Expand Down

0 comments on commit 65eca57

Please sign in to comment.