Skip to content

Optional serialization for deep object for query parameter as a plain, instead with bracket notationΒ #2707

@ulysses-ck

Description

@ulysses-ck

Description

I have a Spring Backend app, that needs a Pageable object at the time of making a GET request. With Swagger UI, there's no problem to manage this, I can make it and works.

{
  "page": 0,
  "size": 1,
  "sort": [
    "string"
  ]
}

The problem starts when I make the codegen, I'm using the plugin @hey-api/client-fetch. It indeed uses the bracket notation and produces this:

http://localhost:8080/example?pageable[page]=0&pageable[size]=10

And my backend want this:

http://localhost:8080/example?page=0&size=10

For now, I used a custom fetch to accomplish it, that convert from bracket notation to plain query parameter.

const customFetch: typeof fetch = (input, init) => {
  let url: string;
  let requestInit: RequestInit;

  
  if (input instanceof Request) {
    url = input.url;
    requestInit = init ?? {
      method: input.method,
      headers: input.headers,
      body: input.body,
    };
  } else {
    url = input.toString();
    requestInit = init ?? {};
  }
  
  console.log("Before: ", url);

  url = url
    .replace(/pageable\[page\]=/g, "page=")
    .replace(/pageable\[size\]=/g, "size=")
    .replace(/pageable\[sort\]=/g, "sort=");

  console.log("After:", url);

  return fetch(url, requestInit);
};

I despite if it isn't a bug, because I setup the parser to take pageable as a keyword.

export default defineConfig({
  input: 'http://localhost:8080/v3/api-docs',
  output: 'src/client',
  plugins: [{
    name: '@hey-api/client-fetch',
    baseUrl: false,
    runtimeConfigPath: './src/backend-client.ts',
  }],
  parser: {
    transforms: {
      enums: 'root',
    },
    pagination: {
      keywords: [
        ...defaultPaginationKeywords,
        "pageable"
      ]
    }
  }
});

But could be useful to have this to make a plain object instead to treat bracket notation at backend, at least for Spring to work out of the box, besides be from backend or frontend side

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions