• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.networknt.schema;
17 
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 
20 import java.util.Set;
21 
22 import org.junit.jupiter.api.Test;
23 
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.networknt.schema.SpecVersion.VersionFlag;
27 import com.networknt.schema.serialization.JsonMapperFactory;
28 
29 /**
30  * Test that the code isn't confused by an anchor in the id.
31  */
32 public class Issue927Test {
33     @Test
test()34     void test() throws JsonMappingException, JsonProcessingException {
35         String schema = "{\r\n"
36                 + "  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\r\n"
37                 + "  \"$id\": \"id\",\r\n"
38                 + "  \"type\": \"object\",\r\n"
39                 + "  \"title\": \"title\",\r\n"
40                 + "  \"anyOf\": [\r\n"
41                 + "    {\r\n"
42                 + "      \"required\": [\r\n"
43                 + "        \"id\",\r\n"
44                 + "        \"type\",\r\n"
45                 + "        \"genericSubmission\"\r\n"
46                 + "      ]\r\n"
47                 + "    }\r\n"
48                 + "  ],\r\n"
49                 + "  \"properties\": {\r\n"
50                 + "    \"id\": {\r\n"
51                 + "      \"type\": \"string\",\r\n"
52                 + "      \"title\": \"title\"\r\n"
53                 + "    },\r\n"
54                 + "    \"type\": {\r\n"
55                 + "      \"type\": \"string\",\r\n"
56                 + "      \"title\": \"title\"\r\n"
57                 + "    },\r\n"
58                 + "    \"genericSubmission\": {\r\n"
59                 + "      \"$ref\": \"#/definitions/genericSubmission\"\r\n"
60                 + "    }\r\n"
61                 + "  },\r\n"
62                 + "  \"definitions\": {\r\n"
63                 + "    \"genericSubmission\": {\r\n"
64                 + "      \"$id\": \"#/definitions/genericSubmission\",\r\n"
65                 + "      \"type\": \"object\",\r\n"
66                 + "      \"title\": \"title\",\r\n"
67                 + "      \"required\": [\r\n"
68                 + "        \"transactionReference\",\r\n"
69                 + "        \"title\"\r\n"
70                 + "      ],\r\n"
71                 + "      \"properties\": {\r\n"
72                 + "        \"transactionReference\": {\r\n"
73                 + "          \"type\": \"string\",\r\n"
74                 + "          \"title\": \"title\",\r\n"
75                 + "          \"description\": \"description\"\r\n"
76                 + "        },\r\n"
77                 + "        \"title\": {\r\n"
78                 + "          \"type\": \"array\",\r\n"
79                 + "          \"minItems\": 1,\r\n"
80                 + "          \"items\": {\r\n"
81                 + "            \"type\": \"object\",\r\n"
82                 + "            \"required\": [\r\n"
83                 + "              \"value\",\r\n"
84                 + "              \"locale\"\r\n"
85                 + "            ],\r\n"
86                 + "            \"properties\": {\r\n"
87                 + "              \"value\": {\r\n"
88                 + "                \"$ref\": \"#/definitions/value\"\r\n"
89                 + "              },\r\n"
90                 + "              \"locale\": {\r\n"
91                 + "                \"$ref\": \"#/definitions/locale\"\r\n"
92                 + "              }\r\n"
93                 + "            }\r\n"
94                 + "          }\r\n"
95                 + "        }\r\n"
96                 + "      }\r\n"
97                 + "    },\r\n"
98                 + "    \"value\": {\r\n"
99                 + "      \"$id\": \"#/definitions/value\",\r\n"
100                 + "      \"type\": \"string\"\r\n"
101                 + "    },\r\n"
102                 + "    \"locale\": {\r\n"
103                 + "      \"$id\": \"#/definitions/locale\",\r\n"
104                 + "      \"type\": \"string\",\r\n"
105                 + "      \"default\": \"fr\"\r\n"
106                 + "    }\r\n"
107                 + "  }\r\n"
108                 + "}";
109         JsonSchema jsonSchema = JsonSchemaFactory.getInstance(VersionFlag.V7)
110                 .getSchema(SchemaLocation.of("http://www.example.org"), JsonMapperFactory.getInstance().readTree(schema));
111 
112         String input = "{\r\n"
113                 + "  \"$schema\": \"./mySchema.json\",\r\n"
114                 + "  \"_comment\": \"comment\",\r\n"
115                 + "  \"id\": \"b34024c4-6103-478c-bad6-83b26d98a892\",\r\n"
116                 + "  \"type\": \"genericSubmission\",\r\n"
117                 + "  \"genericSubmission\": {\r\n"
118                 + "    \"transactionReference\": \"123456\",\r\n"
119                 + "    \"title\": [\r\n"
120                 + "      {\r\n"
121                 + "        \"value\": \"[DE]...\",\r\n"
122                 + "        \"locale\": \"de\"\r\n"
123                 + "      },\r\n"
124                 + "      {\r\n"
125                 + "        \"value\": \"[EN]...\",\r\n"
126                 + "        \"locale\": \"en\"\r\n"
127                 + "      }\r\n"
128                 + "    ]\r\n"
129                 + "  }\r\n"
130                 + "}";
131         Set<ValidationMessage> messages = jsonSchema.validate(input, InputFormat.JSON);
132         assertEquals(0, messages.size());
133     }
134 
135 }
136