Skip to content

Make json_name take priority over name (fully) in C# parsing#12262

Closed
jskeet wants to merge 1 commit intoprotocolbuffers:mainfrom
jskeet:json-mapping
Closed

Make json_name take priority over name (fully) in C# parsing#12262
jskeet wants to merge 1 commit intoprotocolbuffers:mainfrom
jskeet:json-mapping

Conversation

@jskeet
Copy link
Contributor

@jskeet jskeet commented Mar 17, 2023

Fixes #11987

(testprotos.pb and UnittestIssues.pb.cs are both generated; no need to review.)

Fixes protocolbuffers#11987

(testprotos.pb and UnittestIssues.pb.cs are both generated; no need to review.)
@jskeet jskeet added bug c# 🅰️ safe for tests Mark a commit as safe to run presubmits over labels Mar 17, 2023
@jskeet jskeet requested a review from a team as a code owner March 17, 2023 14:59
@jskeet jskeet requested review from jtattermusch and removed request for a team March 17, 2023 14:59
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Mar 17, 2023
Copy link

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

LGTM, with a question

var original = new Issue11987Message { A = 10, B = 20, C = 30 };
var json = JsonFormatter.Default.Format(original);
AssertJson("{ 'b': 10, 'a': 20, 'd': 30 }", json);
var parsed = Issue11987Message.Parser.ParseJson(json);

Choose a reason for hiding this comment

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

I'm curios, what happens if you try to parse "{ 'b': 10, 'a': 20, 'd': 30, 'c': 40 }"? I'm just wondering if on the map, there should only be a single entry per field with the key being JsonName if any, else Name. Or basically just have an internal EffectiveJsonName in the field descriptor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question - will check on Monday

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right... our draft specification says that should be rejected, but currently the later field in the JSON will overwrite the earlier field. (For fields of a message type, it'll actually merge the two values.)

So the C# parser is in strict violation of the spec in that sense... as it happens, we need that non-compliant behavior for one CloudEvent parsing scenario at least. (We'll need to work out what to do about either having a "strict" parser setting or change the default in a new major version at some point.)

But I think the behavior with this PR is strictly better than before :)

Choose a reason for hiding this comment

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

Yep, that all makes sense. Thanks!

@JamesNK
Copy link
Contributor

JamesNK commented Mar 21, 2023

Reminder: #11987 effects C# and Java.

Remember to reopen the issue after this is merged, so Java has an open issue to fix it as well.

protobuf-team-bot added a commit that referenced this pull request Mar 21, 2023
@jskeet
Copy link
Contributor Author

jskeet commented Mar 21, 2023

@JamesNK: I've raised #12290 just to keep the Java and C# cases separate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C# possible incorrect implementation of CreateJsonFieldMap causes JSON data corruption during parse

4 participants