question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$ref resolution process change in v7 (bug in v6)

See original GitHub issue

Apologies for the fairly long JSON schema. It is a stripped down version of a quite large schema which is being bundled using json-schema-ref-parser. The problem occurs with the $ref values. The ajv-cli version 3.3.0 compiles the schema and claims it is valid. Newer versions of AJV can’t compile as it cannot resolve the reference.

The problem seems to be that the $ref for property ContainedWithin is pointing to ShipperBookingLineParent which in turn points to ShipperBookingLine.

  • I know it’s a bit convoluted, but is AJV supposed to be able to resolve that kind of reference (apparently in older version it was ok) or is it not supported anymore?

It is possible to update the $ref by removing parts of it (ShipperBookingLineParent/allOf/5/properties/) however the non-stripped down schema has many $ref which are created by json-schema-ref-parser so I am not able to post process them in a meaningful manner as some would require removal and others replacement of paths. Perhaps also worth mentioning that the tool Stoplight Studio has no problem resolving the reference.

What version of Ajv are you using? Does the issue happen if you use the latest version? ajv-cli 5.0.0 ajv 8.0.5 (Have a Javascript running with this version, same result)

Ajv options object Not used - running with defaults

JSON Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "/models/01-MaerskInformationModel-Bundled/Booking/ShipperBooking/ShipperBooking-bundled.v1-stripped.json",
  "type": "object",
  "properties": {
    "ShipperBookingLines": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "DangerousCargoInformation": {
            "type": "object",
            "properties": {
              "HazardousClassification": {
                "type": "object",
                "properties": {
                  "RequiredDangerousCommodityPackageType": {
                    "type": "object",
                    "properties": {
                      "AwesomeProperty": {
                        "type": "string"
                      },
                      "ContainedWithin": {
                        "$ref": "#/properties/ShipperBookingLines/items/properties/ShipperBookingLineParent/properties/DangerousCargoInformation/properties/HazardousClassification/properties/RequiredDangerousCommodityPackageType"
                      }
                    }
                  }
                }
              }
            }
          },
          "ShipperBookingLineParent": {
            "$ref": "#/properties/ShipperBookingLines/items"
          }
        }
      }
    }
  }
}

Sample data

Not applicable, schema compilation fails

Your code

npx ajv-cli compile -s ShipperBooking-bundled.v1-stripped.json

Validation result, data AFTER validation, error messages ajv-cli:

schema ShipperBooking-bundled.v1-stripped.json is invalid
error: can't resolve reference #/properties/ShipperBookingLines/items/properties/ShipperBookingLineParent/properties/DangerousCargoInformation/properties/HazardousClassification/properties/RequiredDangerousCommodityPackageType from id /models/01-MaerskInformationModel-Bundled/Booking/ShipperBooking/ShipperBooking-bundled.v1-stripped.json

ajv:

MissingRefError: can't resolve reference #/properties/ShipperBookingLines/items/properties/ShipperBookingLineParent/properties/DangerousCargoInformation/properties/HazardousClassification/properties/RequiredDangerousCommodityPackageType from id /models/01-MaerskInformationModel-Bundled/Booking/ShipperBooking/ShipperBooking-bundled.v1-stripped.json
    at Object.code (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\vocabularies\core\ref.js:21:19)
    at keywordCode (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:451:13)
    at C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:185:25
    at CodeGen.code (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\codegen\index.js:439:13)
    at CodeGen.block (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\codegen\index.js:568:18)
    at schemaKeywords (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:185:13)
    at typeAndKeywords (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:129:5)
    at subSchemaObjCode (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:116:5)
    at subschemaCode (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:92:13)
    at KeywordCxt.subschema (C:\dev\workspace2\JSON-schema-test\node_modules\ajv\dist\compile\validate\index.js:425:9) {
  missingRef: '/models/01-MaerskInformationModel-Bundled/Booking/ShipperBooking/ShipperBooking-bundled.v1-stripped.json#/properties/ShipperBookingLines/items/properties/ShipperBookingLineParent/properties/DangerousCargoInformation/properties/HazardousClassification/properties/RequiredDangerousCommodityPackageType',
  missingSchema: '/models/01-MaerskInformationModel-Bundled/Booking/ShipperBooking/ShipperBooking-bundled.v1-stripped.json'
}

What results did you expect? That the schema compiles as it does with ajv-cli 3.3.0

Are you going to resolve the issue? I am not familiar with the AJV source code, so I can’t.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Relequestualcommented, Jul 22, 2021

The part of the specification where this is defined is found at https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-9.3

I’ve written 99% of a blog post which explains it a little, and it’s pending publication.

Part of the problem with how json-schema-ref-parser works, and what changed in 2019-09 made things even worse (for the library). You can read about key comments here: https://github.com/APIDevTools/json-schema-ref-parser/issues/145

If you’re allergic to reading RFC spec documents, you could join the JSON Schema slack and watch out for updates on the announcements channel for the blog posting on bundling.

There wasn’t a defined bundling process before, and it’s somewhat easier to define with the changes we made in 2019-09 (outlined in that link above).

1reaction
philsturgeoncommented, May 21, 2021

I see, looks like the trouble is coming from:

                    "ContainedWithin": {
                        "$ref": "#/properties/ShipperBookingLines/items/properties/ShipperBookingLineParent/properties/Dangerous...

When ShipperBookingLineParent only has a $ref and no properties.

      "ShipperBookingLineParent": {
            "$ref": "#/properties/ShipperBookingLines/items"
      }

This is likely a problem with json-schema-ref-parser then. The Stoplight crew help maintain that package so we can try to take a look, but if anyone else has time feel free to take a look.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - $ref resolution process change in v7 (bug in v6) -
It is a stripped down version of a quite large schema which is being bundled using json-schema-ref-parser . The problem occurs with the...
Read more >
Cisco Firepower Release Notes, Version 6.6 - Open and ...
Resolved Bugs in Version 6.6.7 ; CSCvz35669. KP-2110 Standby disabled upgrade 6.6.4-64 to 7.0.1-30 "CD App Sync error is App Config Apply Failed"....
Read more >
Bug listing with status RESOLVED with resolution INVALID as ...
Bug listing with status RESOLVED with resolution INVALID as at 2022/12/23 17:46:44.
Read more >
Breaking changes in 7.0 | Elasticsearch Guide [7.17] | Elastic
Breaking changes in 7.0edit. This section discusses the changes that you need to be aware of when migrating your application to Elasticsearch 7.0....
Read more >
storybook/CHANGELOG.v6.md at next - GitHub
Webpack: Fix for process fallback using require.resolve (#17249) ... Core: Fix build-storybook sort bug in v6-mode (#16724); Addon-docs/Angular: fix ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found