fast-json-stringify icon indicating copy to clipboard operation
fast-json-stringify copied to clipboard

allOf does not support $ref

Open joelmukuthu opened this issue 6 years ago • 5 comments

🐛 Bug Report

With allOf, a schema that is referenced via $ref gets stripped out of the stringified output.

To Reproduce

Steps to reproduce the behavior:

const fastJson = require('fast-json-stringify');
const stringify = fastJson({
  type: 'object',
  definitions: {
    names: {
      type: 'object',
      properties: {
        firstName: { type: 'string' },
        lastName: { type: 'string' },
      },
    },
  },
  properties: {
    user: {
      type: 'object',
      allOf: [{ $ref: '#/definitions/names' }],
    },
  },
});

console.log(stringify({ user: { firstName: 'john', lastName: 'doe' } })); // {"user":{}}

Expected behavior

That firstName and lastName would be included in the output. Compare with the following where $ref is not used:

const fastJson = require('fast-json-stringify');
const stringify = fastJson({
  type: 'object',
  definitions: {
    names: {
      type: 'object',
      properties: {
        firstName: { type: 'string' },
        lastName: { type: 'string' },
      },
    },
  },
  properties: {
    user: {
      type: 'object',
      allOf: [
        {
          type: 'object',
          properties: {
            firstName: { type: 'string' },
            lastName: { type: 'string' },
          },
        },
      ],
    },
  },
});

console.log(stringify({ user: { firstName: 'john', lastName: 'doe' } })); 
// {"user":{"firstName":"john","lastName":"doe"}}

Your Environment

  • node version: 12.6.1
  • fast-json-stringify: 2.0.0
  • os: Linux

joelmukuthu avatar May 14 '20 17:05 joelmukuthu

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar May 14 '20 18:05 mcollina

Sure thing. Would you mind jump starting me in the right direction, the codebase looked quite cryptic at first glance earlier today.

On Thu, 14 May 2020, 8:50 pm Matteo Collina, [email protected] wrote:

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/fastify/fast-json-stringify/issues/223#issuecomment-628823119, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC3O3UF7KAVIYAAVKWEGF3RRQ4OTANCNFSM4NA37GNA .

joelmukuthu avatar May 14 '20 22:05 joelmukuthu

We should do something similar to https://github.com/fastify/fast-json-stringify/blob/c4fbf833cacf4a97a18f2d3b2217d9a4188dc030/index.js#L632 in https://github.com/fastify/fast-json-stringify/blob/c4fbf833cacf4a97a18f2d3b2217d9a4188dc030/index.js#L736.

I think we should pass through the definitions.

mcollina avatar May 15 '20 06:05 mcollina

I'm experiencing a similar issue that is probably related:

In my schema's "response":

    200: {
      type: 'array',
      items: {
        anyOf: [
          { $ref: 'someref1' },
          { $ref: 'someref2' }
        ]
      }
    }

This does not work.

ahgentil avatar May 18 '20 20:05 ahgentil

I've submitted a PR for this.

@ahgentil, I haven't tested it with actual code but it looks like $ref in anyOf does work. This test is already on master and passing: https://github.com/fastify/fast-json-stringify/blob/c4fbf833cacf4a97a18f2d3b2217d9a4188dc030/test/anyof.test.js#L252-L295

Consider creating a new issue with some code that can fully reproduce your case.

joelmukuthu avatar Jun 06 '20 11:06 joelmukuthu