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

Router should accept arrays or primitives a body payloads #54268

Closed
pgayvallet opened this issue Jan 8, 2020 · 1 comment · Fixed by #54331
Closed

Router should accept arrays or primitives a body payloads #54268

pgayvallet opened this issue Jan 8, 2020 · 1 comment · Fixed by #54331
Assignees
Labels
bug Fixes for quality problems that affect the customer experience Feature:New Platform Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc

Comments

@pgayvallet
Copy link
Contributor

pgayvallet commented Jan 8, 2020

The validation changes in #51919 seems to have broken the actual data pre-validation, and wrongly coerce anything to objects, causing any primitive or array send as post payload to convert to an empty object :

Sample test that fails:

  it('accept to receive an array payload', async () => {
    const { server: innerServer, createRouter } = await server.setup(setupDeps);
    const router = createRouter('/');

    let body: any = null;
    router.post(
      {
        path: '/',
        validate: {
          body: schema.arrayOf(schema.object({ foo: schema.string() })),
        },
      },
      (context, req, res) => {
        body = req.body;
        return res.ok({ body: 'ok' });
      }
    );
    await server.start();

    await supertest(innerServer.listener)
      .post('/')
      .send([{ foo: 'bar' }, { foo: 'dolly' }])
      .expect(200);

    expect(body).toEqual([{ foo: 'bar' }, { foo: 'dolly' }]);
  });
expected 200 "OK", got 400 "Bad Request"

with the actual error message being

[request body.0]: expected a plain object value, but found [Array] instead

Culprit seems to be:

private preValidateSchema(data: any) {
if (Buffer.isBuffer(data)) {
// if options.body.parse !== true
return schema.buffer();
} else if (data instanceof Stream) {
// if options.body.output === 'stream'
return schema.stream();
} else {
return schema.maybe(schema.nullable(schema.object({}, { allowUnknowns: true })));
}
}

@pgayvallet pgayvallet added bug Fixes for quality problems that affect the customer experience Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc Feature:New Platform labels Jan 8, 2020
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes for quality problems that affect the customer experience Feature:New Platform Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants