8000 Can't resolve reference that is defined on schema · Issue #452 · fastify/fast-json-stringify · GitHub
[go: up one dir, main page]

Skip to content
Can't resolve reference that is defined on schema #452
Closed
@filipomar

Description

@filipomar

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.2.0

Plugin version

No response

Node.js version

18.0.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

22.04

Description

While attempting to serialize some complex json as a response on my fastify instance, a false positive error would be thrown at the time of serialization, stating that some definitions could not be found on the schema despite them being present.

I have simplified the json a lot and narrowed the issue down to the configuration you see on the steps to reproduce.

It might be the chained anyOfs, which are actually necessary on my actual code as there are plenty of other options, and once removed cause the serialization to work as expected.

Using the JSON.stringify and adj to build a serializer solved my issues at least for now.

Steps to Reproduce

To see the error being thrown, running the code below with jest (that works just as well with ajv, causes an error to be thrown using the fast stringify.

import Ajv from 'ajv';
import { JSONSchema7 } from 'json-schema';
import fastJson, { Schema } from 'fast-json-stringify';

const response = { a: { b: { c: 'd' } } };

const schema: Schema = {
    $ref: '#/definitions/A',
    definitions: {
        A: {
            type: 'object',
            additionalProperties: false,
            properties: { a: { anyOf: [{ $ref: '#/definitions/B' }] } },
            required: ['a'],
        },
        B: {
            type: 'object',
            properties: { b: { anyOf: [{ $ref: '#/definitions/C' }] } },
            required: ['b'],
            additionalProperties: false,
        },
        C: {
            type: 'object',
            properties: { c: { type: 'string', const: 'd' } },
            required: ['c'],
            additionalProperties: false,
        },
    },
};

const schema2: JSONSchema7 = {
    ...schema,
    $schema: 'http://json-schema.org/draft-07/schema#',
};

test('fails for whatever reason', () => {
    const ajv = new Ajv();
    const validator = ajv.compile(schema2);

    expect(validator(response)).toBe(true);

    const stringify = fastJson(schema);

    console.log(stringify(response));
});

Stack trace:

    can't resolve reference #/definitions/D from id 1a64d18c-1de3-4c04-9188-782717dfd599

      43 |     const stringify = fastJson(schema);
      44 |
    > 45 |     console.log(stringify(response));
         |                 ^
      46 | });
      47 |

      at Object.code (node_modules/fast-json-stringify/node_modules/ajv/lib/vocabularies/core/ref.ts:19:39)
      at keywordCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:523:9)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:228:21
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at schemaKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:228:9)
      at typeAndKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:161:3)
      at subSchemaObjCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:147:3)
      at subschemaCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:124:7)
      at KeywordCxt.subschema (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:491:5)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/vocabularies/code.ts:147:26
          at Array.forEach (<anonymous>)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/vocabularies/code.ts:146:12
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at Object.validateUnion [as code] (node_modules/fast-json-stringify/node_modules/ajv/lib/vocabularies/code.ts:145:7)
      at keywordCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:523:9)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:265:9
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at iterateKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:262:7)
      at groupKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:248:7)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:233:38
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at schemaKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:232:7)
      at typeAndKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:161:3)
      at subSchemaObjCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:147:3)
      at subschemaCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:124:7)
      at KeywordCxt.subschema (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:491:5)
      at applyPropertySchema (node_modules/fast-json-stringify/node_modules/ajv/lib/vocabularies/applicator/properties.ts:45:11)
      at Object.code (node_modules/fast-json-stringify/node_modules/ajv/lib/vocabularies/applicator/properties.ts:32:9)
      at keywordCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:523:9)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:265:9
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at iterateKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:262:7)
      at groupKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:241:7)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:233:38
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.block (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:680:20)
      at schemaKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:232:7)
      at typeAndKeywords (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:161:3)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:100:5
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:61:45
      at CodeGen.code (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
      at CodeGen.func (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/codegen/index.ts:699:24)
      at validateFunction (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:60:9)
      at topSchemaObjCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:94:3)
      at validateFunctionCode (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/validate/index.ts:42:7)
      at Ajv.compileSchema (node_modules/fast-json-stringify/node_modules/ajv/lib/compile/index.ts:163:25)
      at Ajv._compileSchemaEnv (node_modules/fast-json-stringify/node_modules/ajv/lib/core.ts:733:24)
      at Ajv.getSchema (node_modules/fast-json-stringify/node_modules/ajv/lib/core.ts:536:34)
      at Ajv.validate (node_modules/fast-json-stringify/node_modules/ajv/lib/core.ts:358:16)
      at $main (eval at build (node_modules/fast-json-stringify/index.js:182:20), <anonymous>:176:20)
      at Object.<anonymous> (src/server/infrastructure/fastify/testing.test.ts:45:17)
      at async TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)
      at async runJest (node_modules/@jest/core/build/runJest.js:407:19)
      at async _run10000 (node_modules/@jest/core/build/cli/index.js:338:7)
      at async runCLI (node_modules/@jest/core/build/cli/index.js:190:3)

Expected Behavior

I would expect the given response to be considered valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0