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

Integrate with qs library #716

Open
2 tasks done
conduongtong opened this issue Mar 28, 2023 · 14 comments
Open
2 tasks done

Integrate with qs library #716

conduongtong opened this issue Mar 28, 2023 · 14 comments

Comments

@conduongtong
Copy link

conduongtong commented Mar 28, 2023

Prerequisites

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

🚀 Feature Proposal

Can we have a querystring parser for swagger?
I have a problem with array in params request url
image

Because swagger ui stringify params different than qs so I receive different object.
Expected:

filters[roles][$or][0][name]=Super Admin&filters[roles][$or][1][permissions][name]=user:findOne

Thank you!

Example

const qs = require("qs");
const fastify = require("fastify");
const fastifySwagger = require("@fastify/swagger");
const fastifySwaggerUi = require("@fastify/swagger-ui");

const test = async () => {
  const app = fastify({ logger: true, querystringParser: qs.parse });

  await app.register(fastifySwagger, {
    openapi: {
      info: {
        title: "Test",
      },
    },
    hideUntagged: true,
  });
  await app.register(fastifySwaggerUi, {});

  app.get(
    "/users",
    {
      schema: {
        tags: ["User"],
        querystring: {
          type: "object",
          properties: { filters: { type: "object" } },
        },
      },
    },
    async (request) => {
      return request.query;
    }
  );
  app.listen({ host: "127.0.0.1", port: 3000 });
};

test();

Query:

{
  "filters": {
    "roles": {
      "$or": [{"name": "Super Admin"}, { "permissions": { "name": "user:findOne" }}]
    }
  }
}
@mcollina
Copy link
Member

I'm sorry but I don't understand those snippets. Could you include a complete example?

@conduongtong
Copy link
Author

I'm sorry but I don't understand those snippets. Could you include a complete example?

I have a query params like this:
Screenshot 2023-03-29 at 2 52 11 PM
It return: '/users?filters[roles][$or][name]=Super Admin&filters[roles][$or][permissions][name]=user:findOne'
Because swagger stringify that object to above string.
Using qs library it would parse $or to object instead of array.
It should be stringify like this: '/users?filters[roles][$or][0][name]=Super Admin&filters[roles][$or][1][permissions][name]=user:findOne'

So I need swagger UI stringify with qs instead of querystring.
Sorry for my bad english, btw.

@mcollina
Copy link
Member

So is this a bug in Swagger UI?

@conduongtong
Copy link
Author

conduongtong commented Mar 29, 2023

So is this a bug in Swagger UI?

It's not because swagger maybe use querystring to stringify, I want to use qs library instead. With fastify I can change to qs by using querystringParser option.

@mcollina
Copy link
Member

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

@conduongtong
Copy link
Author

Can you provide steps to reproduce? We often need a reproducible example, e.g. some code that allows someone else to recreate your problem by just copying and pasting it. If it involves more than a couple of different file, create a new repository on GitHub and add a link to that.

const qs = require("qs");
const fastify = require("fastify");
const fastifySwagger = require("@fastify/swagger");
const fastifySwaggerUi = require("@fastify/swagger-ui");

const test = async () => {
  const app = fastify({ logger: true, querystringParser: qs.parse });

  await app.register(fastifySwagger, {
    openapi: {
      info: {
        title: "Test",
      },
    },
    hideUntagged: true,
  });
  await app.register(fastifySwaggerUi, {});

  app.get(
    "/users",
    {
      schema: {
        tags: ["User"],
        querystring: {
          type: "object",
          properties: { filters: { type: "object" } },
        },
      },
    },
    async (request) => {
      return request.query;
    }
  );
  app.listen({ host: "127.0.0.1", port: 3000 });
};

test();

Here is it.
You will see response different with my input

@mcollina
Copy link
Member

How are you sending your input? Can you pass a curl string to send it or a Node script?

@conduongtong
Copy link
Author

How are you sending your input? Can you pass a curl string to send it or a Node script?

I pass it to parameter like an example. schema -> quyerstring -> filters

@mcollina
Copy link
Member

Are you using Swagger UI to generate the querystring? Is it generating a querystring that the server cannot interpret correctly?

I asked the same question in #716 (comment).

@conduongtong
Copy link
Author

conduongtong commented Mar 30, 2023

Are you using Swagger UI to generate the querystring? Is it generating a querystring that the server cannot interpret correctly?

I asked the same question in #716 (comment).

Yes but it's not a bug in Swagger UI. I use different library to parse

@Uzlopak
Copy link
Contributor

Uzlopak commented Mar 30, 2023

Please read the part regarding query serialization in

https://swagger.io/docs/specification/serialization/

@conduongtong
Copy link
Author

Please read the part regarding query serialization in

https://swagger.io/docs/specification/serialization/

I read it before, I also try { type: 'object', style: 'deepObject', explode: true } but it's not working, unknown keyword 'style'

@Uzlopak
Copy link
Contributor

Uzlopak commented Mar 30, 2023

Where is this error thrown? Typescript? Runtime at fastify start? When shown in the ui?

@conduongtong
Copy link
Author

Where is this error thrown? Typescript? Runtime at fastify start? When shown in the ui?

It's strict mode schema throw error

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

3 participants