Skip to content
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

Support internal non-pointer references #26

Open
madbence opened this issue Aug 3, 2021 · 3 comments · May be fixed by #27
Open

Support internal non-pointer references #26

madbence opened this issue Aug 3, 2021 · 3 comments · May be fixed by #27
Labels
bug Something isn't working enhancement New feature or request

Comments

@madbence
Copy link

madbence commented Aug 3, 2021

According to 8.3.2, $ref might not be a JSON pointer, eg:

   {
       "$id": "http://example.net/root.json",
       "items": {
           "type": "array",
           "items": { "$ref": "#item" }
       },
       "definitions": {
           "single": {
               "$id": "#item",
               "type": "object",
               "additionalProperties": { "$ref": "other.json" }
           }
       }
   }

Currently, the schema viewer errors with Invalid JSON pointer. (it's trying to resolve #item as a JSON pointer, while in fact it's just an internal reference, pointing to http://example.net/root.json#item)

@robertmassaioli
Copy link
Collaborator

Hi @madbence ,

Two quick things:

  1. There is no definition for #item only for #items (missing an s)
  2. The other.json reference does not look valid.

This tool should support any fully-internal references. 😄 Let me know if that still does not work.

@madbence
Copy link
Author

madbence commented Aug 4, 2021

@robertmassaioli I took the example from the spec, so I think it's valid (sure, the other.json reference is external, which isn't supported, and that is fine)

The #item reference in #/items/items/$ref is a valid reference (to #/definitions/single), as described in the spec:

   When an implementation encounters the <#/definitions/single> schema,
   it resolves the "$id" URI reference against the current base URI to
   form <http://example.net/root.json#item>.

   When an implementation then looks inside the <#/items> schema, it
   encounters the <#item> reference, and resolves this to
   <http://example.net/root.json#item>, which it has seen defined in
   this same document and can therefore use automatically.

   When an implementation encounters the reference to "other.json", it
   resolves this to <http://example.net/other.json>, which is not
   defined in this document.  If a schema with that identifier has
   otherwise been supplied to the implementation, it can also be used
   automatically.

A more complex example that I was trying to examine: https://raw.githubusercontent.com/usnistgov/OSCAL/main/json/schema/oscal_assessment-results_schema.json

The following change kinda resolved the issue (yes, it's an ugly hack, and doesn't care about the base URI, or nested definitions):

--- a/src/lookup/index.ts
+++ b/src/lookup/index.ts
@@ -62,7 +62,10 @@ export class InternalLookup implements Lookup {
       return undefined;
     }
 
-    const result = pointerGet(this.schema, ref.slice(1));
+    if (ref === '#') return this.getSchema(this.schema);
+
+    const result = ref.includes('/') ? pointerGet(this.schema, ref.slice(1)) : Object.values((this.schema as any).definitions).find(a => a['$id'] == ref) as any;
 
     if (result === undefined) {
       return undefined;

@robertmassaioli
Copy link
Collaborator

Okay, I've had a sit down and read through the schema. Essentially, the "$id": "#<identifier>" format overrides standard JSON Pointer logic and becomes the first lookup for within the file.

That's fairly annoying but not impossible to replicate, we would just need to generate a map when we first load the schema for usage in follow-up references.

@robertmassaioli robertmassaioli added bug Something isn't working enhancement New feature or request labels Aug 7, 2021
@madbence madbence linked a pull request Aug 9, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants