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

Precompiled schema not all $ref resolved #3902

Closed
4 tasks done
cwendtxealth opened this issue Oct 11, 2023 · 0 comments · Fixed by #3903
Closed
4 tasks done

Precompiled schema not all $ref resolved #3902

cwendtxealth opened this issue Oct 11, 2023 · 0 comments · Fixed by #3903
Labels
bug utils Related to @rjsf/utils

Comments

@cwendtxealth
Copy link
Contributor

Prerequisites

What theme are you using?

core

Version

5.13.1

Current Behavior

For precompiled schemas all $refs have to be resolved when creating the different schema definitions for ajv to create them. In a recent update in utils retrieveSchema.ts resolveAllReferences it will now only resolve the $ref the first time it was found and will ignore after. This cause any schema with multiple references to the same $ref to fail pre compilation.

Here is a simple example schema

{
  "anyOf": [
    {
      "title": "First method of identification",
      "properties": {
        "firstName": {
          "type": "string",
          "title": "First name"
        },
        "lastName": {
          "$ref": "#/definitions/test"
        }
      }
    },
    {
      "title": "Second method of identification",
      "properties": {
        "idCode": {
          "$ref": "#/definitions/test"
        }
      }
    }
  ],
  "definitions": {
    "test": {
      "type": "string"
    }
  }
}

and then the output from running compileSchemaValidators

if (schOrEnv === undefined) throw new MissingRefError(it.opts.uriResolver, baseId, $ref)
                                      ^
MissingRefError: can't resolve reference #/definitions/test from id -284587a1

  missingRef: '-284587a1#/definitions/test',
  missingSchema: '-284587a1'

Expected Behavior

Resolve all $refs

Steps To Reproduce

No response

Environment

- OS:
- Node:
- npm:

Anything else?

No response

@cwendtxealth cwendtxealth added bug needs triage Initial label given, to be assigned correct labels and assigned labels Oct 11, 2023
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this issue Oct 12, 2023
…f/oneOf` branches

Fixes rjsf-team#3902 by fixing the recursive `$ref` resolution when resolving `anyOf/oneOf` branches
- In `@rjsf/utils`, fixed rjsf-team#3902 as follows:
  - Updated the `package.json` to fix the path to `jest` within the `test:debug` script after NPM workspaces were used
  - Updated the `resolveAnyOrOneOfSchemas()` to no longer take a `recurseList` array and to have each branch of the `anyOf`/`oneOf` pass an empty array to the `resolveAllReferences()` function
  - Updated `parseSchema()` and `resolveDependencies()` to no longer pass `recurseList` to `resolveAnyOrOneOfSchemas()`
  - Updated the `SUPER_SCHEMA` to add the use case provided in rjsf-team#3902 to verify the issue is fixed
    - Updated the snapshot for the `schemaParser` test with the updated parsed schema
- In `@rjsf/validator-ajv8`, added an optimization to the `AJV8PrecompiledSchema` class
  - Added a new `ensureSameRootSchema()` function that only resolves the `rootSchema` with the `formData` when it does not match the given `schema` before throwing an error
  - Used this new function in `rawValidation()` and `isValid()`, replacing the duplicate existing code
  - Updated the tests to add unit tests for the new function as well as removing the need to use a resolved `rootSchema` in the tests
- Updated the `CHANGELOG.md` accordingly
heath-freenome added a commit that referenced this issue Oct 12, 2023
…f/oneOf` branches (#3903)

Fixes #3902 by fixing the recursive `$ref` resolution when resolving `anyOf/oneOf` branches
- In `@rjsf/utils`, fixed #3902 as follows:
  - Updated the `package.json` to fix the path to `jest` within the `test:debug` script after NPM workspaces were used
  - Updated the `resolveAnyOrOneOfSchemas()` to no longer take a `recurseList` array and to have each branch of the `anyOf`/`oneOf` pass an empty array to the `resolveAllReferences()` function
  - Updated `parseSchema()` and `resolveDependencies()` to no longer pass `recurseList` to `resolveAnyOrOneOfSchemas()`
  - Updated the `SUPER_SCHEMA` to add the use case provided in #3902 to verify the issue is fixed
    - Updated the snapshot for the `schemaParser` test with the updated parsed schema
- In `@rjsf/validator-ajv8`, added an optimization to the `AJV8PrecompiledSchema` class
  - Added a new `ensureSameRootSchema()` function that only resolves the `rootSchema` with the `formData` when it does not match the given `schema` before throwing an error
  - Used this new function in `rawValidation()` and `isValid()`, replacing the duplicate existing code
  - Updated the tests to add unit tests for the new function as well as removing the need to use a resolved `rootSchema` in the tests
- Updated the `CHANGELOG.md` accordingly
@heath-freenome heath-freenome added utils Related to @rjsf/utils and removed needs triage Initial label given, to be assigned correct labels and assigned labels Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug utils Related to @rjsf/utils
Projects
None yet
2 participants