|
| 1 | +package com.networknt.schema; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import java.io.InputStream; |
| 5 | +import java.util.Set; |
| 6 | +import org.junit.jupiter.api.Assertions; |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 9 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 10 | +import com.fasterxml.jackson.databind.JsonNode; |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 13 | +import com.fasterxml.jackson.databind.node.BinaryNode; |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * created at 07.02.2023 |
| 18 | + * |
| 19 | + * @author k-oliver |
| 20 | + * @since 1.0.77 |
| 21 | + */ |
| 22 | +public class Issue650Test { |
| 23 | + |
| 24 | + /** |
| 25 | + * Test using a Java model with a byte[] property which jackson converts to a BASE64 encoded string automatically. Then convert into |
| 26 | + * a jackson tree. The resulting node is of type {@link BinaryNode}. This test checks if validation handles the {@link BinaryNode} as string |
| 27 | + * when validating. |
| 28 | + * |
| 29 | + * @throws Exception |
| 30 | + * @since 1.0.77 |
| 31 | + */ |
| 32 | + @Test |
| 33 | + public void testBinaryNode() throws Exception { |
| 34 | + final ObjectMapper mapper = new ObjectMapper(); |
| 35 | + mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); |
| 36 | + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 37 | + mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
| 38 | + |
| 39 | + // schema with data property of type string: |
| 40 | + InputStream schemaInputStream = getClass().getResourceAsStream("/draft7/issue650.json"); |
| 41 | + JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaInputStream); |
| 42 | + |
| 43 | + // create model first: |
| 44 | + Issue650Test.Model model = new Issue650Test.Model(); |
| 45 | + model.setData("content".getBytes("UTF-8")); |
| 46 | + // now convert to tree. The resulting type of the data property is BinaryNode now: |
| 47 | + JsonNode node = mapper.valueToTree(model); |
| 48 | + |
| 49 | + // validate: |
| 50 | + Set<ValidationMessage> errors = schema.validate(node); |
| 51 | + |
| 52 | + // check result: |
| 53 | + Assertions.assertTrue(errors.isEmpty()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * created at 07.02.2023 |
| 58 | + * |
| 59 | + * @author Oliver Kelling |
| 60 | + * @since 1.0.77 |
| 61 | + */ |
| 62 | + private static class Model { |
| 63 | + private byte[] data; |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * @return the data |
| 68 | + * @since 1.0.77 |
| 69 | + */ |
| 70 | + public byte[] getData() { |
| 71 | + return this.data; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * @param data the data to set |
| 77 | + * @since 1.0.77 |
| 78 | + */ |
| 79 | + public void setData(byte[] data) { |
| 80 | + this.data = data; |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | +} |
0 commit comments