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

Possible serialization bug with response schema #3749

Closed
2 tasks done
Jnig opened this issue Mar 4, 2022 · 2 comments
Closed
2 tasks done

Possible serialization bug with response schema #3749

Jnig opened this issue Mar 4, 2022 · 2 comments

Comments

@Jnig
Copy link
Contributor

Jnig commented Mar 4, 2022

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.27.2, 4.0.0-alpha.1

Node.js version

14.19

Operating system

Linux

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

Arch Linux

Description

Hi,

I'm using fastify with a response schema. In the schema I've a property called user, which can be null or a object with id and email.

When the user object has the properties id and email everything works as expected. If the object contains an additional property, the whole user object is converted to null in the response. This was somehow unexpected, but I'm not sure if this is really a bug.

Steps to Reproduce

const fastify = require("fastify")({
  logger: true,
});

const opts = {
  schema: {
    response: {
      "2xx": {
        type: "object",
        properties: {
          id: {
            type: "number",
          },
          name: {
            type: "string",
          },
          user: {
            anyOf: [
              {
                type: "null",
              },
              {
                type: "object",
                properties: {
                  id: {
                    type: "number",
                  },
                  email: {
                    type: "string",
                  },
                },
                additionalProperties: false,
              },
            ],
          },
        },
      },
    },
  },
};

// returns {"id":1,"name":"foo","user":{"id":1,"email":"foobar@local"}}
fastify.get("/", opts, function (request, reply) {
  reply.send({ id: 1, name: "foo", user: { id: 1, email: "foobar@local" } });
});

// returns {"id":1,"name":"foo","user":null}
fastify.get("/with-additional", opts, function (request, reply) {
  reply.send({
    id: 1,
    name: "foo",
    additionalProp: "foo",
    user: { id: 1, email: "foobar@local", firstName: "foobar" },
  });
});

fastify.listen(3000, function (err, address) {
  if (err) {
    fastify.log.error(err);
    process.exit(1);
  }
});

Expected Behavior

Additional properties in the user object should be removed instead of setting the object to null. This would mirror the behavior for other additional properties. additionalProp is removed from the output.

The following code

reply.send({
    id: 1,
    name: "foo",
    additionalProp: "foo",
    user: { id: 1, email: "foobar@local", firstName: "foobar" },
  });

should return

{"id":1,"name":"foo","user":{"id":1,"email":"foobar@local"}

following is returned

{"id":1,"name":"foo","user":null}
@Eomm
Copy link
Member

Eomm commented Mar 4, 2022

fast-json-stringify does not support the type: null atm

you need to use this schema:

const schema = {
  type: 'object',
  properties: {
    id: {
      type: 'number',
    },
    name: {
      type: 'string',
    },
    user: {
      nullable: true,
      type: 'object',
      properties: {
        id: {
          type: 'number',
        },
        email: {
          type: 'string',
        },
      },
      additionalProperties: false,
    },
  },
}

Closing as duplicates fastify/fast-json-stringify#388

@Eomm Eomm closed this as completed Mar 4, 2022
@Jnig
Copy link
Contributor Author

Jnig commented Mar 4, 2022

@Eomm thanks for pointing to the fast-json-stringify issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants