Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
888e71bd
Commit
888e71bd
authored
Jan 15, 2016
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prohibit null values in repeated and map fields in JSON
parent
1a34ac03
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletion
+38
-1
JsonParserTest.cs
csharp/src/Google.Protobuf.Test/JsonParserTest.cs
+30
-0
JsonParser.cs
csharp/src/Google.Protobuf/JsonParser.cs
+8
-1
No files found.
csharp/src/Google.Protobuf.Test/JsonParserTest.cs
View file @
888e71bd
...
@@ -170,6 +170,36 @@ namespace Google.Protobuf
...
@@ -170,6 +170,36 @@ namespace Google.Protobuf
AssertRoundtrip
(
message
);
AssertRoundtrip
(
message
);
}
}
[
Test
]
public
void
RepeatedField_NullElementProhibited
()
{
string
json
=
"{ \"repeated_foreign_message\": [null] }"
;
Assert
.
Throws
<
InvalidProtocolBufferException
>(()
=>
TestAllTypes
.
Parser
.
ParseJson
(
json
));
}
[
Test
]
public
void
RepeatedField_NullOverallValueAllowed
()
{
string
json
=
"{ \"repeated_foreign_message\": null }"
;
Assert
.
AreEqual
(
new
TestAllTypes
(),
TestAllTypes
.
Parser
.
ParseJson
(
json
));
}
[
Test
]
[
TestCase
(
"{ \"mapInt32Int32\": { \"10\": null }"
)]
[
TestCase
(
"{ \"mapStringString\": { \"abc\": null }"
)]
[
TestCase
(
"{ \"mapInt32ForeignMessage\": { \"10\": null }"
)]
public
void
MapField_NullValueProhibited
(
string
json
)
{
Assert
.
Throws
<
InvalidProtocolBufferException
>(()
=>
TestMap
.
Parser
.
ParseJson
(
json
));
}
[
Test
]
public
void
MapField_NullOverallValueAllowed
()
{
string
json
=
"{ \"mapInt32Int32\": null }"
;
Assert
.
AreEqual
(
new
TestMap
(),
TestMap
.
Parser
.
ParseJson
(
json
));
}
[
Test
]
[
Test
]
public
void
IndividualWrapperTypes
()
public
void
IndividualWrapperTypes
()
{
{
...
...
csharp/src/Google.Protobuf/JsonParser.cs
View file @
888e71bd
...
@@ -239,6 +239,10 @@ namespace Google.Protobuf
...
@@ -239,6 +239,10 @@ namespace Google.Protobuf
return
;
return
;
}
}
tokenizer
.
PushBack
(
token
);
tokenizer
.
PushBack
(
token
);
if
(
token
.
Type
==
JsonToken
.
TokenType
.
Null
)
{
throw
new
InvalidProtocolBufferException
(
"Repeated field elements cannot be null"
);
}
list
.
Add
(
ParseSingleValue
(
field
,
tokenizer
));
list
.
Add
(
ParseSingleValue
(
field
,
tokenizer
));
}
}
}
}
...
@@ -270,7 +274,10 @@ namespace Google.Protobuf
...
@@ -270,7 +274,10 @@ namespace Google.Protobuf
}
}
object
key
=
ParseMapKey
(
keyField
,
token
.
StringValue
);
object
key
=
ParseMapKey
(
keyField
,
token
.
StringValue
);
object
value
=
ParseSingleValue
(
valueField
,
tokenizer
);
object
value
=
ParseSingleValue
(
valueField
,
tokenizer
);
// TODO: Null handling
if
(
value
==
null
)
{
throw
new
InvalidProtocolBufferException
(
"Map values must not be null"
);
}
dictionary
[
key
]
=
value
;
dictionary
[
key
]
=
value
;
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment