-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Skip the '@' character in the beginning of the field name in JSON #581
Merged
hjafarpour
merged 6 commits into
confluentinc:master
from
hjafarpour:JsonFieldNameCorrection
Jan 10, 2018
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f489c9
Skip the '@` character in the begining of the field name in JSON dese…
hjafarpour 1763afd
Using 'assertThat` in tests.
hjafarpour cf1cf74
Better error message for fieldname '@'.
hjafarpour dc1154a
Applied review feedback.
hjafarpour 4b12c85
Merge remote-tracking branch 'upstream/master' into JsonFieldNameCorr…
hjafarpour 85017bd
Merge remote-tracking branch 'upstream/master' into JsonFieldNameCorr…
hjafarpour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
ksql-serde/src/test/java/io/confluent/ksql/serde/json/KsqlJsonDeserializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright 2017 Confluent Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
**/ | ||
|
||
package io.confluent.ksql.serde.json; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.apache.kafka.connect.data.Schema; | ||
import org.apache.kafka.connect.data.SchemaBuilder; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import io.confluent.ksql.GenericRow; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
public class KsqlJsonDeserializerTest { | ||
|
||
Schema orderSchema; | ||
|
||
@Before | ||
public void before() { | ||
|
||
orderSchema = SchemaBuilder.struct() | ||
.field("ordertime".toUpperCase(), org.apache.kafka.connect.data.Schema.INT64_SCHEMA) | ||
.field("orderid".toUpperCase(), org.apache.kafka.connect.data.Schema.INT64_SCHEMA) | ||
.field("itemid".toUpperCase(), org.apache.kafka.connect.data.Schema.STRING_SCHEMA) | ||
.field("orderunits".toUpperCase(), org.apache.kafka.connect.data.Schema.FLOAT64_SCHEMA) | ||
.field("arraycol".toUpperCase(), SchemaBuilder.array(org.apache.kafka.connect.data.Schema.FLOAT64_SCHEMA)) | ||
.field("mapcol".toUpperCase(), SchemaBuilder.map(org.apache.kafka.connect.data.Schema.STRING_SCHEMA, org.apache.kafka.connect.data.Schema.FLOAT64_SCHEMA)) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void shouldDeserializeJsonCorrectly() throws JsonProcessingException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
Map<String, Object> orderRow = new HashMap<>(); | ||
orderRow.put("ordertime", 1511897796092L); | ||
orderRow.put("@orderid", 1L); | ||
orderRow.put("itemid", "Item_1"); | ||
orderRow.put("orderunits", 10.0); | ||
orderRow.put("arraycol", new Double[]{10.0, 20.0}); | ||
orderRow.put("mapcol", Collections.singletonMap("key1", 10.0)); | ||
|
||
byte[] jsonBytes = objectMapper.writeValueAsBytes(orderRow); | ||
|
||
KsqlJsonDeserializer ksqlJsonDeserializer = new KsqlJsonDeserializer(orderSchema); | ||
|
||
GenericRow genericRow = ksqlJsonDeserializer.deserialize("", jsonBytes); | ||
assertThat("Incorrect columns count.", genericRow.getColumns().size(), equalTo(6)); | ||
assertThat("Incorrect deserialization", (Long) genericRow.getColumns().get(0) == | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
and elsewhere in the test |
||
1511897796092L); | ||
assertThat("Incorrect deserialization", (Long) genericRow.getColumns().get(1) == 1L); | ||
assertThat("Incorrect deserialization", ((String) genericRow.getColumns().get(2)).equals("Item_1")); | ||
assertThat("Incorrect deserialization", (Double) genericRow.getColumns().get(3) == 10.0); | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is it possible that the fieldName could just be
@
? in which case the next line would failThere 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.
Field name cannot be '@' and if we have '@' as field name we should fail anyway.
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.
Maybe we should fail with a more meaningful exception? i.e.., not
IndexOutOfBoundsException
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 better error message for field name '@'.