Skip to content

Commit

Permalink
return proper paths for choice fields with validation errors
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 592007309
  • Loading branch information
Quarz0 authored and copybara-github committed Dec 18, 2023
1 parent 35b635c commit 5b850b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
18 changes: 12 additions & 6 deletions go/jsonformat/fhirvalidate/fhirvalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,22 @@ func walkMessage(msg protoreflect.Message, fd protoreflect.FieldDescriptor, json
if fd.Message() == nil {
return true
}
jsonPath := addFieldToPath(jsonPath, fd.JSONName())
newPath := addFieldToPath(jsonPath, fd.JSONName())
if jsonpbhelper.IsChoice(msg.Descriptor()) {
newPath = jsonPath + strings.Title(fd.JSONName())
}
if fd.IsList() {
l := value.List()
for i := 0; i < l.Len(); i++ {
if err := walkMessage(l.Get(i).Message(), fd, jsonpbhelper.AddIndexToPath(jsonPath, i), validators, opts...); err != nil {
if err := walkMessage(l.Get(i).Message(), fd, jsonpbhelper.AddIndexToPath(newPath, i), validators, opts...); err != nil {
if err := jsonpbhelper.AppendUnmarshalError(&errors, err); err != nil {
fatalErr = err
return false
}
}
}
} else {
err := walkMessage(value.Message(), fd, jsonPath, validators, opts...)
err := walkMessage(value.Message(), fd, newPath, validators, opts...)
if err := jsonpbhelper.AppendUnmarshalError(&errors, err); err != nil {
fatalErr = err
return false
Expand Down Expand Up @@ -303,17 +306,20 @@ func walkMessageWithErrorReporter(msg protoreflect.Message, fd protoreflect.Fiel
if fd.Message() == nil {
return true
}
jsonPath := addFieldToPath(jsonPath, fd.JSONName())
newPath := addFieldToPath(jsonPath, fd.JSONName())
if jsonpbhelper.IsChoice(msg.Descriptor()) {
newPath = jsonPath + strings.Title(fd.JSONName())
}
if fd.IsList() {
l := value.List()
for i := 0; i < l.Len(); i++ {
if err := walkMessageWithErrorReporter(l.Get(i).Message(), fd, jsonpbhelper.AddIndexToPath(jsonPath, i), validators, er); err != nil {
if err := walkMessageWithErrorReporter(l.Get(i).Message(), fd, jsonpbhelper.AddIndexToPath(newPath, i), validators, er); err != nil {
fatalErr = err
return false
}
}
} else {
if err := walkMessageWithErrorReporter(value.Message(), fd, jsonPath, validators, er); err != nil {
if err := walkMessageWithErrorReporter(value.Message(), fd, newPath, validators, er); err != nil {
fatalErr = err
return false
}
Expand Down
26 changes: 21 additions & 5 deletions go/jsonformat/unmarshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1950,21 +1950,37 @@ func TestUnmarshal_ExtendedValidation_Errors(t *testing.T) {
"Missing required field",
`
{
"resourceType": "Patient",
"link": [{}]
}`,
"resourceType": "Patient",
"link": [{}]
}`,
jsonpbhelper.UnmarshalErrorList{
{Path: "Patient.link[0]", Details: `missing required field "other"`, Type: jsonpbhelper.RequiredFieldError},
{Path: "Patient.link[0]", Details: `missing required field "type"`, Type: jsonpbhelper.RequiredFieldError},
},
allVers,
},
{
"Missing required repeated field",
"Missing required field in oneof",
`
{
"resourceType": "OperationOutcome"
"valueQuantity": {
"extension": [{"valueCode": "foo"}]
},
"resourceType": "Observation",
"status": "final",
"code": {
"text": "bar"
}
}`,
&jsonpbhelper.UnmarshalError{Path: "Observation.valueQuantity.extension[0]", Details: `missing required field "url"`, Type: jsonpbhelper.RequiredFieldError},
allVers,
},
{
"Missing required repeated field",
`
{
"resourceType": "OperationOutcome"
}`,
&jsonpbhelper.UnmarshalError{Path: "OperationOutcome", Details: `missing required field "issue"`, Type: jsonpbhelper.RequiredFieldError},
allVers,
},
Expand Down

0 comments on commit 5b850b0

Please sign in to comment.