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 authored and Unknown committed Mar 8, 2018
1 parent 4fd04f4 commit 9c508b7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 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
14 changes: 11 additions & 3 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func (s *ServerSuite) TestEmbbeddedResourceIDsGetRetrievedCorrectly(c *C) {
c.Assert(resource["_id"], IsNil)
}

func (s *ServerSuite) TestContainedResourcesIDsAreCorrectButExtensionIsNot(c *C) {
func (s *ServerSuite) TestContainedResources(c *C) {
res, err := postFixture(s.Server.URL, "Condition", "../fixtures/condition_with_contained_patient.json")
util.CheckErr(err)

Expand All @@ -659,10 +659,18 @@ func (s *ServerSuite) TestContainedResourcesIDsAreCorrectButExtensionIsNot(c *C)
c.Assert(len(containedMap["id"].(string)), Equals, 19)
c.Assert(containedMap["_id"], IsNil)

// But sadly, the extension in the patient is not
// the extension should be without internal fields like @context
extension := containedMap["extension"].([]interface{})[0]
extensionMap := extension.(map[string]interface{})
c.Assert(extensionMap["@context"], Not(IsNil))
c.Assert(extensionMap["@context"], IsNil)
c.Assert(extensionMap["url"], Equals, "http://hl7.org/fhir/StructureDefinition/us-core-race")

// the managingOrganization reference should be without internal fields like referenceid
managingOrganizationMap := containedMap["managingOrganization"].(map[string]interface{})
c.Assert(managingOrganizationMap["reference"], Equals, "Organization/1")
c.Assert(managingOrganizationMap["referenceid"], IsNil)
c.Assert(managingOrganizationMap["type"], IsNil)
c.Assert(managingOrganizationMap["external"], IsNil)

// Delete this entry
worker := s.MasterSession.GetWorkerSession()
Expand Down

0 comments on commit 9c508b7

Please sign in to comment.