-
Notifications
You must be signed in to change notification settings - Fork 769
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
Conversation
What are your thoughts on using |
router/router.go
Outdated
@@ -44,6 +44,7 @@ import ( | |||
"github.com/julienschmidt/httprouter" | |||
_ "github.com/lib/pq" | |||
"github.com/rs/cors" | |||
"github.com/tidwall/pretty" |
There was a problem hiding this comment.
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
?
router/router.go
Outdated
@@ -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))) |
There was a problem hiding this comment.
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.
endpoints/openrtb2/auction_test.go
Outdated
@@ -122,6 +123,7 @@ func TestJsonSampleRequests(t *testing.T) { | |||
}, | |||
} | |||
|
|||
jsoniter.RegisterExtension(&jsonutil.RawMessageExtension{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Need to move this line (and all instances of it) to the start of the app. Might be a good use of the func init()
, but that doesn't work for tests so we'll need to do something different there.
endpoints/openrtb2/auction_test.go
Outdated
func TestSingleJSONTest(t *testing.T) { | ||
jsoniter.RegisterExtension(&jsonutil.RawMessageExtension{}) | ||
runJsonBasedTest(t, "sample-requests/valid-whole/exemplary/simple.json", "") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Remove dev test.
util/jsonutil/jsonutil.go
Outdated
func init() { | ||
jsonConfigValidationOff.RegisterExtension(&RawMessageExtension{}) | ||
jsonConfigValidationOn.RegisterExtension(&RawMessageExtension{}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My untested attempt at an init func. Doesn't work for tests, but I think that's what TestMain is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a TestMain
per every *_test.go
file that needs to register the extension
util/jsonutil/jsonutil.go
Outdated
dst := bytes.NewBuffer(make([]byte, 0, len(jsonRawMsg))) | ||
if err := json.Compact(dst, jsonRawMsg); err != nil { | ||
stream.Error = err | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Add a test for the error path.
dcf5e27
to
b6b8ba5
Compare
@@ -21,6 +23,7 @@ import ( | |||
|
|||
func init() { | |||
rand.Seed(time.Now().UnixNano()) | |||
jsoniter.RegisterExtension(&jsonutil.RawMessageExtension{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to register it for all of the jsoniter library, or can/should it be scoped to the jsonConfigValidationOn
and jsonConfigValidationOff
configs?
util/jsonutil/jsonutil.go
Outdated
@@ -211,3 +213,44 @@ func tryExtractErrorMessage(err error) string { | |||
func isLikelyDetailedErrorMessage(msg string) bool { | |||
return !strings.HasPrefix(msg, "request.") | |||
} | |||
|
|||
// RawMessageExtension will call json.Compact() on every json.RawMessage field when getting marshalled. | |||
// All other types will be marshalled as usual |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend removing the "All other types will be marshalled as usual" part. Doesn't seem necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
util/jsonutil/jsonutil.go
Outdated
return nil | ||
} | ||
|
||
// jsonRawMessageType is declared here so we don't have to call TypeOfPtr(&json.RawMessage{}).Elem() everytime we encode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO I don't think this comment is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
util/jsonutil/jsonutil_test.go
Outdated
}, | ||
}, | ||
{ | ||
desc: "json.RawMessage is passed to Marshal(), expect inner JSON blob to be line-break-free, tab-free, spaces only found inside strings, and compacted into one line", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test names should be simple, short, and contain no spaces. Characters like - and _ are fine. Please use comments to add more description as needed. Same for other tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
util/jsonutil/jsonutil_test.go
Outdated
test: func(t *testing.T) { | ||
jsoniter.RegisterExtension(&RawMessageExtension{}) | ||
out, err := Marshal(json.RawMessage(formatted)) | ||
assert.NoError(t, err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error check should be require.NoError
so we don't try to. compare a potentially invalid out
in the following assertion. Same for the other tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test was modified, no longer asserts for error
util/jsonutil/jsonutil.go
Outdated
jsonRawMsg := *(*[]byte)(ptr) | ||
|
||
dst := bytes.NewBuffer(make([]byte, 0, len(jsonRawMsg))) | ||
json.Compact(dst, jsonRawMsg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please handle the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Corrected
router/router_test.go
Outdated
@@ -275,3 +282,22 @@ func TestValidateDefaultAliases(t *testing.T) { | |||
} | |||
} | |||
} | |||
|
|||
func TestBidderParamsCompactedOutput(t *testing.T) { | |||
expectedPrefix := `{"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"]}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this rely on the real 33across static file? Will this test fail if that bidder makes an update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I forgot to swap the real file for a proper JSON test file. Modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick with the json test file. Otherwise looks great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be a real bidder name? Could we use foo instead? The file name is "appnexus" and integer param is for AAX.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed in latest commit
To not go back to using the default
json
library in thebidders/params
endpoint, this pull requests modifies the handler function to use of the tidwall/pretty library instead.