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

Default request additional properties vs default response additional properties #1055

Closed
Zeds112 opened this issue Sep 8, 2024 · 3 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@Zeds112
Copy link

Zeds112 commented Sep 8, 2024

💬 Question here

By default, fastify allows the request body to have additional properties but by default the response has any additional properties removed. I could not find some documentation on this.

Is there a simple way to allow additional properties on all responses similar to the default for request bodies? Thanks.

const Fastify = require('fastify');

const fOpts = {};

const fastify = Fastify(fOpts);

const schema = {
  description: 'test',
  body: {
    type: 'object',
    properties: {
      test1: { type: 'string' },
    },
  },
  response: {
    200: {
      type: 'object',
      properties: {
        test3: { type: 'string' },
      },
    },
  },
};

fastify.post('/test', { schema }, async (req: any, res: any) => {
  console.log({
    body: req.body,
  })

  // curl -X POST http://localhost:3003/test \
  //   -H "Content-Type: application/json" \
  //   -d '{
  //     "test1": "test1",
  //     "test2": "test2"
  //   }'

  // { body: { test1: 'test1', test2: 'test2' } }

  return res.status(200).send({
    test3: 'test3',
    test4: 'test4',
  });

  // client receives: {"test3":"test3"}

});


const run = async () => {
  await fastify.listen({
    host: 'localhost',
    port: 3003,
  });

};

run();


export { };

Your Environment

  • node version: 18
  • fastify version: 4.28.1
  • os: Windows
  • any other relevant information
@Zeds112 Zeds112 added the help wanted Extra attention is needed label Sep 8, 2024
@dosubot dosubot bot added the question Further information is requested label Sep 8, 2024
@mcollina
Copy link
Member

You can just add the additionalProperties: true to the object.

@Zeds112
Copy link
Author

Zeds112 commented Sep 13, 2024

Hi thanks I understand that but I'm specifically wondering if there's a default option so that you don't have to add additionalProperties: true to everything manually.

@mcollina
Copy link
Member

Unfortunately no.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants