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

fix: take optional request body into account when creating form data #1416

Merged
merged 3 commits into from
Jun 7, 2024

Conversation

AffeJonsson
Copy link
Contributor

@AffeJonsson AffeJonsson commented May 29, 2024

Status

READY

Description

Fixes #1392
This makes sure to check for undefined in case the request body is optional. This also contains changes to not cast to any when handling subtypes.

Before:

export const createPets = (
  pet?: Pet,
  version: number = 1,
  options?: AxiosRequestConfig,
): Promise<AxiosResponse<Pet>> => {
  const formData = new FormData();
  if ((pet as any).name !== undefined) {
    formData.append('name', (pet as any).name);
  }
  if ((pet as any).tag !== undefined) {
    formData.append('tag', (pet as any).tag);
  }

  if ((pet as any).id !== undefined) {
    formData.append('id', (pet as any).id.toString());
  }
  if ((pet as any).name !== undefined) {
    formData.append('name', (pet as any).name);
  }
  if ((pet as any).tag !== undefined) {
    formData.append('tag', (pet as any).tag);
  }
  if (pet['@id'] !== undefined) {
    formData.append('@id', pet['@id']);
  }
  if (pet.email !== undefined) {
    formData.append('email', pet.email);
  }
  if (pet.callingCode !== undefined) {
    formData.append('callingCode', pet.callingCode);
  }
  if (pet.country !== undefined) {
    formData.append('country', pet.country);
  }

  return axios.post(`/v${version}/pets`, formData, options);
};

After:

export const createPets = (
  pet?: Pet,
  version: number = 1,
  options?: AxiosRequestConfig,
): Promise<AxiosResponse<Pet>> => {
  const formData = new FormData();
  const petPetBase = pet as PetBase | undefined;
  if (petPetBase?.name !== undefined) {
    formData.append('name', petPetBase.name);
  }
  if (petPetBase?.tag !== undefined) {
    formData.append('tag', petPetBase.tag);
  }

  const petPetExtended = pet as PetExtended | undefined;
  if (petPetExtended?.id !== undefined) {
    formData.append('id', petPetExtended.id.toString());
  }
  if (petPetExtended?.name !== undefined) {
    formData.append('name', petPetExtended.name);
  }
  if (petPetExtended?.tag !== undefined) {
    formData.append('tag', petPetExtended.tag);
  }
  if (pet?.['@id'] !== undefined) {
    formData.append('@id', pet['@id']);
  }
  if (pet?.email !== undefined) {
    formData.append('email', pet.email);
  }
  if (pet?.callingCode !== undefined) {
    formData.append('callingCode', pet.callingCode);
  }
  if (pet?.country !== undefined) {
    formData.append('country', pet.country);
  }

  return axios.post(`/v${version}/pets`, formData, options);
};

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

Steps to Test or Reproduce

generate:react-query
generate:swr

@melloware melloware added this to the 6.30.0 milestone May 29, 2024
@melloware melloware added the bug Something isn't working label May 29, 2024
melloware
melloware previously approved these changes May 29, 2024
@melloware melloware requested a review from anymaniax May 29, 2024 11:50
@AffeJonsson
Copy link
Contributor Author

Noticed the cast to subtype only worked if mocks were generated, I'll fix a commit where they are imported properly

melloware
melloware previously approved these changes May 31, 2024
Copy link
Collaborator

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AffeJonsson
I have consulted with you about how to extend the function, so please check it out.

packages/core/src/getters/res-req-types.ts Outdated Show resolved Hide resolved
@soartec-lab soartec-lab self-assigned this Jun 6, 2024
Copy link
Collaborator

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks !

@soartec-lab soartec-lab merged commit 9c44e9e into orval-labs:master Jun 7, 2024
2 checks passed
@AffeJonsson AffeJonsson deleted the fix/optional-form-data branch June 7, 2024 09:37
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 this pull request may close these issues.

Non-requred requestBody results in TS error
3 participants