Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Json compacter in the bidders/params endpoint #3395

Merged
merged 24 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
github.com/vrischmann/go-metrics-influxdb v0.1.1 h1:xneKFRjsS4BiVYvAKaM/rOlXYd1pGHksnES0ECCJLgo=
github.com/vrischmann/go-metrics-influxdb v0.1.1/go.mod h1:q7YC8bFETCYopXRMtUvQQdLaoVhpsEwvQS2zZEYCqg8=
Expand Down
10 changes: 7 additions & 3 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
"github.com/rs/cors"
"github.com/tidwall/pretty"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why did you choose the pretty package over the built-in json.Compact?

)

// NewJsonDirectoryServer is used to serve .json files from a directory as a single blob. For example,
Expand Down Expand Up @@ -76,12 +77,12 @@ func newJsonDirectoryServer(schemaDirectory string, validator openrtb_ext.Bidder
if !isValid {
glog.Fatalf("Schema exists for an unknown bidder: %s", bidder)
}
data[bidder] = json.RawMessage(validator.Schema(bidderName))
data[bidder] = pretty.Ugly([]byte(validator.Schema(bidderName)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling the compact method manually, please add it as an extension registered with json-iter. This will allow all code in PBS to get the benefit without needing to manually make these modifications.

}

// Add in any aliases
for aliasName, parentBidder := range yamlAliases {
data[string(aliasName)] = json.RawMessage(validator.Schema(parentBidder))
data[string(aliasName)] = pretty.Ugly([]byte(validator.Schema(parentBidder)))
}

// Add in any default aliases
Expand All @@ -93,12 +94,15 @@ func newJsonDirectoryServer(schemaDirectory string, validator openrtb_ext.Bidder
data[aliasName] = bidderData
}

response, err := json.Marshal(data)
response, err := jsonutil.Marshal(data)
if err != nil {
glog.Fatalf("Failed to marshal bidder param JSON-schema: %v", err)
}

return func(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)

w.Header().Add("Content-Type", "application/json")
w.Write(response)
}
Expand Down
17 changes: 17 additions & 0 deletions router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

"github.com/prebid/prebid-server/v2/config"
Expand Down Expand Up @@ -275,3 +276,19 @@ func TestValidateDefaultAliases(t *testing.T) {
}
}
}

func TestBidderParamsCompactedOutput(t *testing.T) {
inSchemaDirectory := "../static/bidder-params"
expected := `{"33across":{"$schema":"http://json-schema.org/draft-04/schema#","title":"33Across Adapter Params","description":"A schema which validates params accepted by the 33Across adapter","type":"object","properties":{"productId":{"type":"string","description":"Product type"},"siteId":{"type":"string","description":"Site Id"},"zoneId":{"type":"string","description":"Zone Id"}},"required":["productId","siteId"]}`
paramsValidator, err := openrtb_ext.NewBidderParamsValidator(inSchemaDirectory)
assert.NoError(t, err, "Error initialing validator")
handler := newJsonDirectoryServer(inSchemaDirectory, paramsValidator, nil, nil)
recorder := httptest.NewRecorder()

request, err := http.NewRequest("GET", "/whatever", nil)
assert.NoError(t, err, "Error creating request")

handler(recorder, request, nil)

assert.True(t, strings.HasPrefix(recorder.Body.String(), expected))
}
Loading