Skip to content

Commit

Permalink
Merge pull request #2 from michaelsauter/fix/json-html-escaping
Browse files Browse the repository at this point in the history
Do not escape HTML characters inside JSON quoted strings
  • Loading branch information
michaelsauter authored Aug 11, 2022
2 parents 29241d0 + b45358a commit 1adec48
Show file tree
Hide file tree
Showing 378 changed files with 2,724 additions and 531 deletions.
6 changes: 5 additions & 1 deletion fhir-models-gen/cmd/genResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,17 @@ func generateResourceOrType(resources ResourceMap, requiredTypes map[string]bool
file.Commentf("MarshalJSON marshals the given %s as JSON into a byte slice", definition.Name)
file.Func().Params(jen.Id("r").Id(definition.Name)).Id("MarshalJSON").Params().
Params(jen.Op("[]").Byte(), jen.Error()).Block(
jen.Return().Qual("encoding/json", "Marshal").Call(jen.Struct(
jen.Id("buffer").Op(":=").Qual("bytes", "Buffer").Block(),
jen.Id("enc").Op(":=").Qual("encoding/json", "NewEncoder").Call(jen.Id("&buffer")),
jen.Id("enc").Op(".").Id("SetEscapeHTML").Call(jen.False()),
jen.Id("err").Op(":=").Id("enc").Op(".").Id("Encode").Call(jen.Struct(
jen.Id("Other"+definition.Name),
jen.Id("ResourceType").String().Tag(map[string]string{"json": "resourceType"}),
).Values(jen.Dict{
jen.Id("Other" + definition.Name): jen.Id("Other" + definition.Name).Call(jen.Id("r")),
jen.Id("ResourceType"): jen.Lit(definition.Name),
})),
jen.Return(jen.Id("buffer").Op(".").Id("Bytes").Call(), jen.Id("err")),
)
}

Expand Down
9 changes: 7 additions & 2 deletions fhir-models-gen/cmd/valueSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cmd
import (
"errors"
"fmt"
"strings"

"github.com/dave/jennifer/jen"
"github.com/lifebox-healthcare/golang-fhir-models/fhir-models-gen/fhir"
"strings"
)

func generateValueSet(resources ResourceMap, valueSet fhir.ValueSet) (*jen.File, error) {
Expand Down Expand Up @@ -57,7 +58,11 @@ func generateValueSet(resources ResourceMap, valueSet fhir.ValueSet) (*jen.File,
Params().
Params(jen.Op("[]").Byte(), jen.Error()).
Block(
jen.Return(jen.Qual("encoding/json", "Marshal").Call(jen.Id("code").Op(".").Id("Code").Call())),
jen.Id("buffer").Op(":=").Qual("bytes", "Buffer").Block(),
jen.Id("enc").Op(":=").Qual("encoding/json", "NewEncoder").Call(jen.Id("&buffer")),
jen.Id("enc").Op(".").Id("SetEscapeHTML").Call(jen.False()),
jen.Id("err").Op(":=").Id("enc").Op(".").Id("Encode").Call(jen.Id("code").Op(".").Id("Code").Call()),
jen.Return(jen.Id("buffer").Op(".").Id("Bytes").Call(), jen.Id("err")),
)

// UnmarshalJSON function
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/addressType.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -33,7 +34,11 @@ const (
)

func (code AddressType) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *AddressType) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/addressUse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -35,7 +36,11 @@ const (
)

func (code AddressUse) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *AddressUse) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/aggregationMode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -33,7 +34,11 @@ const (
)

func (code AggregationMode) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *AggregationMode) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/bindingStrength.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -34,7 +35,11 @@ const (
)

func (code BindingStrength) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *BindingStrength) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
11 changes: 9 additions & 2 deletions fhir-models-gen/fhir/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package fhir

import "encoding/json"
import (
"bytes"
"encoding/json"
)

// THIS FILE IS GENERATED BY https://github.com/lifebox-healthcare/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND
Expand Down Expand Up @@ -83,13 +86,17 @@ type OtherBundle Bundle

// MarshalJSON marshals the given Bundle as JSON into a byte slice
func (r Bundle) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(struct {
OtherBundle
ResourceType string `json:"resourceType"`
}{
OtherBundle: OtherBundle(r),
ResourceType: "Bundle",
})
return buffer.Bytes(), err
}

// UnmarshalBundle unmarshals a Bundle.
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/bundleType.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -39,7 +40,11 @@ const (
)

func (code BundleType) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *BundleType) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
11 changes: 9 additions & 2 deletions fhir-models-gen/fhir/codeSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package fhir

import "encoding/json"
import (
"bytes"
"encoding/json"
)

// THIS FILE IS GENERATED BY https://github.com/lifebox-healthcare/golang-fhir-models
// PLEASE DO NOT EDIT BY HAND
Expand Down Expand Up @@ -109,13 +112,17 @@ type OtherCodeSystem CodeSystem

// MarshalJSON marshals the given CodeSystem as JSON into a byte slice
func (r CodeSystem) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(struct {
OtherCodeSystem
ResourceType string `json:"resourceType"`
}{
OtherCodeSystem: OtherCodeSystem(r),
ResourceType: "CodeSystem",
})
return buffer.Bytes(), err
}

// UnmarshalCodeSystem unmarshals a CodeSystem.
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/codeSystemContentMode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -35,7 +36,11 @@ const (
)

func (code CodeSystemContentMode) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *CodeSystemContentMode) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/codeSystemHierarchyMeaning.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -34,7 +35,11 @@ const (
)

func (code CodeSystemHierarchyMeaning) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *CodeSystemHierarchyMeaning) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/constraintSeverity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -32,7 +33,11 @@ const (
)

func (code ConstraintSeverity) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *ConstraintSeverity) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/contactPointSystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -37,7 +38,11 @@ const (
)

func (code ContactPointSystem) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *ContactPointSystem) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/contactPointUse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -35,7 +36,11 @@ const (
)

func (code ContactPointUse) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *ContactPointUse) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/contributorType.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -34,7 +35,11 @@ const (
)

func (code ContributorType) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *ContributorType) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/daysOfWeek.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -37,7 +38,11 @@ const (
)

func (code DaysOfWeek) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *DaysOfWeek) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/discriminatorType.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -35,7 +36,11 @@ const (
)

func (code DiscriminatorType) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *DiscriminatorType) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
7 changes: 6 additions & 1 deletion fhir-models-gen/fhir/extensionContextType.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package fhir

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand All @@ -33,7 +34,11 @@ const (
)

func (code ExtensionContextType) MarshalJSON() ([]byte, error) {
return json.Marshal(code.Code())
buffer := bytes.Buffer{}
enc := json.NewEncoder(&buffer)
enc.SetEscapeHTML(false)
err := enc.Encode(code.Code())
return buffer.Bytes(), err
}
func (code *ExtensionContextType) UnmarshalJSON(json []byte) error {
s := strings.Trim(string(json), "\"")
Expand Down
Loading

0 comments on commit 1adec48

Please sign in to comment.