Skip to content

The schema location is not always computed correctly for ReferenceSchema #233

@octavianN

Description

@octavianN

Hello,

  1. I made a simple test that assets the schema location for a JSON Schema that has a reference to an external "definitions.json" schema.
    Please note that the schema location for the "city" schema is "#/properties/billing_address/properties/city". But this location does not exists.
    Maybe the location should be something like this: "#definitions.json/definitions/address/properties/city".
@Test
    public void testGetSchemaLocation() throws Exception {
      String definitions = "{\n" + 
          "  \"definitions\": {\n" + 
          "    \"address\": {\n" + 
          "      \"type\": \"object\",\n" + 
          "      \"properties\": {\n" + 
          "        \"city\":           { \"type\": \"string\" },\n" + 
          "        \"state\":          { \"type\": \"string\" }\n" + 
          "      }\n" +
          "    }\n" + 
          "  }\n" + 
          "}";
      
      Reader schema = new StringReader("{\n" + 
          "  \"properties\": {\n" + 
          "    \"billing_address\": { \"$ref\": \"definitions.json#/definitions/address\" }\n" + 
          "  }\n" + 
          "}");
      JSONObject schemaJson = new JSONObject(new JSONTokener(schema));
      
      SchemaClient httpClient = new SchemaClient() {
        @Override
        public InputStream get(String url) {
          if ("definitions.json".equals(url)) {
            return new ByteArrayInputStream(definitions.getBytes());
          }
          return null;
        }
      };
      ObjectSchema compiledSchema = (ObjectSchema) SchemaLoader.load(schemaJson, httpClient);
      
      assertEquals("#", compiledSchema.getSchemaLocation());
      
      ReferenceSchema billing_addresSchema = (ReferenceSchema) compiledSchema.getPropertySchemas().get("billing_address");
      assertEquals(null, billing_addresSchema.getSchemaLocation());
      ObjectSchema referredSchema = (ObjectSchema) billing_addresSchema.getReferredSchema();
      assertEquals("#/properties/billing_address", referredSchema.getSchemaLocation());
      
      Schema citySchema = referredSchema.getPropertySchemas().get("city");
      assertEquals("#/properties/billing_address/properties/city", citySchema.getSchemaLocation());
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions