Skip to content

Commit

Permalink
Allow marshaller to keep valueID while marshal the proto extension fi…
Browse files Browse the repository at this point in the history
…eld.

PiperOrigin-RevId: 681949060
  • Loading branch information
FHIR Team authored and copybara-github committed Oct 3, 2024
1 parent be4e378 commit d8c2406
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
3 changes: 1 addition & 2 deletions go/jsonformat/marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ func (m *Marshaller) marshalSingleExtensionHelper(pb protoreflect.Message) (json
}
}
return cm, err

}

func (m *Marshaller) marshalExtensionsAsURLs(decmap jsonpbhelper.JSONObject, pbs []protoreflect.Message) error {
Expand Down Expand Up @@ -739,7 +738,7 @@ func (m *Marshaller) marshalMessageToMap(pb protoreflect.Message) (jsonpbhelper.
if err != nil {
return nil, err
}
if m.jsonFormat != formatPure && !jsonpbhelper.IsResourceType(pb.Descriptor()) {
if m.jsonFormat != formatPure && !jsonpbhelper.IsResourceType(pb.Descriptor()) && !jsonpbhelper.IsChoice(pb.Descriptor()){
// Omit FHIR element ID fields for analytics json.
// See https://github.com/rbrush/sql-on-fhir/blob/master/sql-on-fhir.md#id-fields-omitted.
delete(decmap, "id")
Expand Down
104 changes: 104 additions & 0 deletions go/jsonformat/marshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3439,6 +3439,110 @@ func TestMarshalMessageForAnalyticsV2_InferredSchema(t *testing.T) {
},
},
},
{
name: "extension with valueString",
inputs: []mvr{
{
ver: fhirversion.R4,
r: &r4patientpb.Patient{
Extension: []*d4pb.Extension{
{
Url: &d4pb.Uri{Value: "http://hl7.org/extension_url"},
Value: &d4pb.Extension_ValueX{Choice: &d4pb.Extension_ValueX_StringValue{StringValue: &d4pb.String{Value: "extension_value"}}},
},
},
},
},
{
ver: fhirversion.STU3,
r: &r3pb.Patient{
Extension: []*d3pb.Extension{
{
Url: &d3pb.Uri{Value: "http://hl7.org/extension_url"},
Value: &d3pb.Extension_ValueX{Choice: &d3pb.Extension_ValueX_StringValue{StringValue: &d3pb.String{Value: "extension_value"}}},
},
},
},
},
},
want: map[string]any{
"extension_url": []any{
map[string]any{
"value": map[string]any{"string": "extension_value"},
},
},
},
},
{
name: "extension with valueId",
inputs: []mvr{
{
ver: fhirversion.R4,
r: &r4patientpb.Patient{
Extension: []*d4pb.Extension{
{
Url: &d4pb.Uri{Value: "http://hl7.org/extension_url"},
Value: &d4pb.Extension_ValueX{Choice: &d4pb.Extension_ValueX_Id{Id: &d4pb.Id{Value: "extension_value"}}},
},
},
},
},
{
ver: fhirversion.STU3,
r: &r3pb.Patient{
Extension: []*d3pb.Extension{
{
Url: &d3pb.Uri{Value: "http://hl7.org/extension_url"},
Value: &d3pb.Extension_ValueX{Choice: &d3pb.Extension_ValueX_Id{Id: &d3pb.Id{Value: "extension_value"}}},
},
},
},
},
},
want: map[string]any{
"extension_url": []any{
map[string]any{
"value": map[string]any{"id": "extension_value"},
},
},
},
},
{
name: "extension with Id and valueId", // Id should be omitted but valueId should be kept
inputs: []mvr{
{
ver: fhirversion.R4,
r: &r4patientpb.Patient{
Extension: []*d4pb.Extension{
{
Id: &d4pb.String{Value: "exampleExtension"},
Url: &d4pb.Uri{Value: "http://hl7.org/extension_url"},
Value: &d4pb.Extension_ValueX{Choice: &d4pb.Extension_ValueX_Id{Id: &d4pb.Id{Value: "extension_value"}}},
},
},
},
},
{
ver: fhirversion.STU3,
r: &r3pb.Patient{
Extension: []*d3pb.Extension{
{
Id: &d3pb.String{Value: "exampleExtension"},
Url: &d3pb.Uri{Value: "http://hl7.org/extension_url"},
Value: &d3pb.Extension_ValueX{Choice: &d3pb.Extension_ValueX_Id{Id: &d3pb.Id{Value: "extension_value"}}},
},
},
},
},
},
want: map[string]any{
"extension_url": []any{
map[string]any{
"value": map[string]any{"id": "extension_value"},
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit d8c2406

Please sign in to comment.