KalleOlaviNiemitalo commented on code in PR #1748:
URL: https://github.com/apache/avro/pull/1748#discussion_r911725171
##########
lang/java/avro/src/main/java/org/apache/avro/Schema.java:
##########
@@ -1454,19 +1456,37 @@ public Schema parse(String s, String... more) {
*/
public Schema parse(String s) {
try {
- return parse(FACTORY.createParser(s));
+ return parse(FACTORY.createParser(s), false);
} catch (IOException e) {
throw new SchemaParseException(e);
}
}
- private Schema parse(JsonParser parser) throws IOException {
+ private Schema parse(JsonParser parser, boolean allowDanglingContent)
throws IOException {
boolean saved = validateNames.get();
boolean savedValidateDefaults = VALIDATE_DEFAULTS.get();
try {
validateNames.set(validate);
VALIDATE_DEFAULTS.set(validateDefaults);
- return Schema.parse(MAPPER.readTree(parser), names);
+ JsonNode jsonNode = MAPPER.readTree(parser);
+ Schema schema = Schema.parse(jsonNode, names);
+ if (!allowDanglingContent) {
+ StringWriter danglingWriter = new StringWriter();
+ parser.releaseBuffered(danglingWriter);
+ String dangling = danglingWriter.toString().trim();
+ if (dangling.isEmpty()) {
+ // releaseBuffered(Writer) above works in case the source was
character-based
+ // (ex. a String). releaseBuffered(OutputStream) covers the case
that the
+ // source was byte-based (ex. a File)
+ ByteArrayOutputStream danglingOutputStream = new
ByteArrayOutputStream();
+ parser.releaseBuffered(danglingOutputStream);
+ dangling = new String(danglingOutputStream.toByteArray(),
StandardCharsets.UTF_8);
Review Comment:
Should you trim this too?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]