RFC7950 specifies that string length is counted in unicode
characters. String.length() returns the length in code units of
UTF-16, which are not the same thing.
Use String.codePointCount() to get correct results for strings
containing characters from outside of Unicode BMP.
JIRA: YANGTOOLS-1224
Change-Id: I6ff9557d61449625be975eaca00ad235bf429155
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit
cbadacd601e11552b749edfaaa19b64b9804e55f)
void validate(final String str) {
if (lengthConstraint != null) {
final RangeSet<Integer> ranges = lengthConstraint.getAllowedRanges();
void validate(final String str) {
if (lengthConstraint != null) {
final RangeSet<Integer> ranges = lengthConstraint.getAllowedRanges();
- if (!ranges.contains(str.length())) {
+ if (!ranges.contains(str.codePointCount(0, str.length()))) {
throw new YangInvalidValueException(ErrorType.PROTOCOL, lengthConstraint,
"String " + str + " does not match allowed lengths " + ranges);
}
throw new YangInvalidValueException(ErrorType.PROTOCOL, lengthConstraint,
"String " + str + " does not match allowed lengths " + ranges);
}
package org.opendaylight.yangtools.yang.data.impl.codec;
import static org.junit.Assert.assertEquals;
package org.opendaylight.yangtools.yang.data.impl.codec;
import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
+import org.opendaylight.yangtools.yang.model.api.ConstraintMetaDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
+import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.InvalidLengthConstraintException;
+import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
+import org.opendaylight.yangtools.yang.model.util.type.StringTypeBuilder;
/**
* Unit tests for StringCodecString.
/**
* Unit tests for StringCodecString.
* @author Thomas Pantelis
*/
public class StringCodecStringTest {
* @author Thomas Pantelis
*/
public class StringCodecStringTest {
-
- @SuppressWarnings("unchecked")
@Test
public void testSerialize() {
StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.stringType(),
@Test
public void testSerialize() {
StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.stringType(),
assertEquals("serialize", "", codec.serialize(""));
}
assertEquals("serialize", "", codec.serialize(""));
}
- @SuppressWarnings("unchecked")
@Test
public void testDeserialize() {
StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.stringType(),
@Test
public void testDeserialize() {
StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(BaseTypes.stringType(),
assertEquals("deserialize", "bar", codec.deserialize("bar"));
assertEquals("deserialize", "", codec.deserialize(""));
}
assertEquals("deserialize", "bar", codec.deserialize("bar"));
assertEquals("deserialize", "", codec.deserialize(""));
}
+
+ @Test
+ public void testDeserializeUnicode() throws InvalidLengthConstraintException {
+ final StringTypeBuilder builder = RestrictedTypes.newStringBuilder(BaseTypes.stringType(),
+ SchemaPath.create(true, QName.create("foo", "foo")));
+ builder.setLengthConstraint(mock(ConstraintMetaDefinition.class), List.of(ValueRange.of(1)));
+ final StringTypeDefinition type = builder.build();
+
+ StringCodec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(type, StringCodec.class);
+
+ assertEquals("🌞", codec.deserialize("🌞"));
+ }