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

fix: protojson to use DiscardUnknown option for backwards compatibility #1453

Merged
merged 2 commits into from
Mar 31, 2023

Conversation

yquansah
Copy link
Contributor

The current http sdk client fails with requests against Flipt instances that are namespace aware (according to the proto definitions). We start to see errors like the following:

$ go run main.go
This is the error:  proto: (line 1:167): unknown field "namespaceKey"

This is because the http client does not know what to do with unknown fields when it goes to decode the output back out to the user. This change introduces a DiscardUnknown Unmarshal option, so the client can know what to do with unknown fields relative to the protobuf definition.

@yquansah yquansah requested a review from a team as a code owner March 31, 2023 14:05
Copy link
Contributor

@GeorgeMac GeorgeMac left a comment

Choose a reason for hiding this comment

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

One small request, otherwise, this is great. Thank you 🙏

@@ -289,7 +289,7 @@ func generateHTTPMethod(g *protogen.GeneratedFile, m mappings, method *protogen.
return
}

g.P("if err := ", protojson("Unmarshal"), "(respData, &output); err != nil { return nil, err }")
g.P("if err := ", "(protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal", "(respData, &output); err != nil { return nil, err }")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
g.P("if err := ", "(protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal", "(respData, &output); err != nil { return nil, err }")
g.P("if err := (", protojson("UnmarshalOptions"), "{DiscardUnknown: true}).Unmarshal", "(respData, &output); err != nil { return nil, err }")

Copy link
Contributor

@GeorgeMac GeorgeMac Mar 31, 2023

Choose a reason for hiding this comment

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

This might seem a little weird. However, it will just ensure that protojson is always imported appropriately at the top of the file.

It is being imported elsewhere at the moment, which is why this isn't breaking.
If that were to change though then this could break. So we make sure to use the function here when referencing the type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should have spent more time trying to figure out how to do it with the protojson import 😓 haha thanks. Will make that change right now.

Copy link
Contributor

Choose a reason for hiding this comment

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

No worries :D It is a weird thing protogen. Takes some getting used to.

@codecov
Copy link

codecov bot commented Mar 31, 2023

Codecov Report

Merging #1453 (ea27755) into main (a036d5a) will not change coverage.
The diff coverage is n/a.

❗ Current head ea27755 differs from pull request most recent head cc25436. Consider uploading reports for the commit cc25436 to get more accurate results

@@           Coverage Diff           @@
##             main    #1453   +/-   ##
=======================================
  Coverage   77.98%   77.98%           
=======================================
  Files          43       43           
  Lines        3239     3239           
=======================================
  Hits         2526     2526           
  Misses        569      569           
  Partials      144      144           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@yquansah yquansah requested a review from GeorgeMac March 31, 2023 14:15
Copy link
Contributor

@GeorgeMac GeorgeMac left a comment

Choose a reason for hiding this comment

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

Nice one!

@yquansah yquansah added the automerge Used by Kodiak bot to automerge PRs label Mar 31, 2023
@kodiakhq kodiakhq bot merged commit 8b1e12b into main Mar 31, 2023
@kodiakhq kodiakhq bot deleted the yq-fix-protojson branch March 31, 2023 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Used by Kodiak bot to automerge PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants