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

Regression when updating from 6.21 to 6.22.0 #1247

Closed
mattiasnordqvist opened this issue Mar 3, 2024 · 2 comments · Fixed by #1251
Closed

Regression when updating from 6.21 to 6.22.0 #1247

mattiasnordqvist opened this issue Mar 3, 2024 · 2 comments · Fixed by #1251
Labels
bug Something isn't working
Milestone

Comments

@mattiasnordqvist
Copy link

mattiasnordqvist commented Mar 3, 2024

What are the steps to reproduce this issue?

  1. Using this open-api spec:
openapi: 3.0.1
info:
  title: Api
  version: '1.0'
paths:
  /queries/inventory:
    post:
      tags:
        - Query
      operationId: GetInventory
      responses:
        '200':
          description: Success
components:
  schemas:
    GetInventoryQuery:
      type: object
      properties:
        inventoryId:
          type: string
  1. And this configuration:
import { defineConfig } from 'orval';

export default defineConfig({
  'api-spec': {
    input: {
      target: './swagger.yaml',
    },
    output: {
      mode: 'split',
      target: './src/api/generated/api.ts',
      schemas: './src/api/generated/model',
      clean: true,
      prettier: true,
      client: 'react-query',
      // https://github.com/anymaniax/orval/issues/730
      override: {
        query: {
          useQuery: false,
          useMutation: false,
        },
        transformer: (verbOptions) => {
          if(verbOptions.operationId == 'CreateFileDownloadKeys')
          {
            verbOptions.override.operations[verbOptions.operationId] = { 
              ...verbOptions.override.operations[verbOptions.operationId], 
              query: {
                ...verbOptions.override.operations[verbOptions.operationId]?.query,
                useMutation: true,
                useQuery: false,
              },
            };
          } else {
            verbOptions.override.operations[verbOptions.operationId] = {
              ...verbOptions.override.operations[verbOptions.operationId],
              query: {
                ...verbOptions.override.operations[verbOptions.operationId]?.query,
                useMutation: verbOptions.verb == 'post' && verbOptions.tags.includes('Command'),
                useQuery: verbOptions.verb == 'get' || verbOptions.tags.includes('Query'),
              },
            };
          }
          return verbOptions;
        },
        operations: {
          GetDataCollections: {
            query: {
              useQuery: true,
            },
          },
        },
      },
    },
  },
});
  1. and then run the orval command

What happens?

This code is generated

/**
 * Generated by orval v6.22.0 🍺
 * Do not edit manually.
 * Api
 * OpenAPI spec version: 1.0
 */
import type { QueryKey } from '@tanstack/react-query';
import axios from 'axios';
import type { AxiosRequestConfig, AxiosResponse } from 'axios';

export const getInventory = (options?: AxiosRequestConfig): Promise<AxiosResponse<void>> => {
  return axios.post(`/queries/inventory`, undefined, options);
};

export const getGetInventoryQueryKey = () => {
  return [`/queries/inventory`] as const;
};

What were you expecting to happen?

In 6.21.0, this code was generated:

/**
 * Generated by orval v6.21.0 🍺
 * Do not edit manually.
 * Api
 * OpenAPI spec version: 1.0
 */
import { useQuery } from '@tanstack/react-query';
import type { QueryFunction, QueryKey, UseQueryOptions, UseQueryResult } from '@tanstack/react-query';
import axios from 'axios';
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';

export const getInventory = (options?: AxiosRequestConfig): Promise<AxiosResponse<void>> => {
  return axios.post(`/queries/inventory`, undefined, options);
};

export const getGetInventoryQueryKey = () => {
  return [`/queries/inventory`] as const;
};

export const getGetInventoryQueryOptions = <
  TData = Awaited<ReturnType<typeof getInventory>>,
  TError = AxiosError<unknown>,
>(options?: {
  query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInventory>>, TError, TData>>;
  axios?: AxiosRequestConfig;
}) => {
  const { query: queryOptions, axios: axiosOptions } = options ?? {};

  const queryKey = queryOptions?.queryKey ?? getGetInventoryQueryKey();

  const queryFn: QueryFunction<Awaited<ReturnType<typeof getInventory>>> = ({ signal }) =>
    getInventory({ signal, ...axiosOptions });

  return { queryKey, queryFn, ...queryOptions } as UseQueryOptions<
    Awaited<ReturnType<typeof getInventory>>,
    TError,
    TData
  > & { queryKey: QueryKey };
};

export type GetInventoryQueryResult = NonNullable<Awaited<ReturnType<typeof getInventory>>>;
export type GetInventoryQueryError = AxiosError<unknown>;

export const useGetInventory = <
  TData = Awaited<ReturnType<typeof getInventory>>,
  TError = AxiosError<unknown>,
>(options?: {
  query?: Partial<UseQueryOptions<Awaited<ReturnType<typeof getInventory>>, TError, TData>>;
  axios?: AxiosRequestConfig;
}): UseQueryResult<TData, TError> & { queryKey: QueryKey } => {
  const queryOptions = getGetInventoryQueryOptions(options);

  const query = useQuery(queryOptions) as UseQueryResult<TData, TError> & { queryKey: QueryKey };

  query.queryKey = queryOptions.queryKey;

  return query;
};

Any logs, error output, etc?

nope

Any other comments?

In our Api, we have multiple quering-endpoints using the POST-http verb. We want the generated code to behave as if they were GET-requests. We did this by tagging these endpoints with the Query, which we then look for in the transformer supplied in the orval.config
From orval 6.22.0 and on, this method of generating query-like operations in orval for POST-endpoints, doesn't work anymore.

What versions are you using?

Operating System: Windows 11
Package Version: 6.21.0 (working) and 6.22.0 and above (not working)
Browser Version: Not applicable

@mattiasnordqvist
Copy link
Author

mattiasnordqvist commented Mar 3, 2024

The transformer defined in the config above is a workaround for an issue discussed here: #730
This issue is now solved, however, upgrading to 6.25 does not solve the original issue, nor this one.

@melloware
Copy link
Collaborator

Did you look through the 6.22 commits to see what possible causes this?

@melloware melloware added this to the 6.26.0 milestone Mar 5, 2024
@melloware melloware added the bug Something isn't working label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants