Skip to content

Commit

Permalink
Fix for unmarshalling of contained resources (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
eug48 committed Mar 8, 2018
1 parent 4fd04f4 commit 386de4e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
3 changes: 3 additions & 0 deletions fixtures/condition_with_contained_patient.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
}],
"gender": "male",
"birthDate": "1950-09-02",
"managingOrganization": {
"reference": "Organization/1"
},
"extension":[
{
"url":"http://hl7.org/fhir/StructureDefinition/us-core-race",
Expand Down
32 changes: 28 additions & 4 deletions models/domainresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,34 @@

package models

import (
"gopkg.in/mgo.v2/bson"
)

type ContainedResources []interface{}

type DomainResource struct {
Resource `bson:",inline"`
Text *Narrative `bson:"text,omitempty" json:"text,omitempty"`
Contained []interface{} `bson:"contained,omitempty" json:"contained,omitempty"`
Extension []Extension `bson:"extension,omitempty" json:"extension,omitempty"`
ModifierExtension []Extension `bson:"modifierExtension,omitempty" json:"modifierExtension,omitempty"`
Text *Narrative `bson:"text,omitempty" json:"text,omitempty"`
Contained ContainedResources `bson:"contained,omitempty" json:"contained,omitempty"`
Extension []Extension `bson:"extension,omitempty" json:"extension,omitempty"`
ModifierExtension []Extension `bson:"modifierExtension,omitempty" json:"modifierExtension,omitempty"`
}

// Convert contained resources from map[string]interfac{} to specific types.
// Custom marshalling methods on those types will then hide internal fields
// like @context and referenceid.
func (x *ContainedResources) SetBSON(raw bson.Raw) (err error) {

// alias type to avoid infinite loop when calling Unmarshal
type containedResources ContainedResources
x2 := (*containedResources)(x)
if err = raw.Unmarshal(x2); err == nil {
if x != nil {
for i := range *x {
(*x)[i] = BSONMapToResource((*x)[i].(bson.M), true)
}
}
}
return
}
3 changes: 0 additions & 3 deletions server/resource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,6 @@ func (u CustomJSONRenderer) Render(w http.ResponseWriter) (err error) {
data = bytes.Replace(data, []byte("\\u003e"), []byte(">"), -1)
data = bytes.Replace(data, []byte("\\u0026"), []byte("&"), -1)

// Convert "_id" to "id"
data = bytes.Replace(data, []byte("\"_id\":"), []byte("\"id\":"), -1)

writeContentType(w, fhirJSONContentType)
_, err = w.Write(data)
return
Expand Down

0 comments on commit 386de4e

Please sign in to comment.