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

Uncaught error when body is undefined in request init #1666

Closed
1 task done
jaredLunde opened this issue May 17, 2024 · 6 comments · Fixed by #1672
Closed
1 task done

Uncaught error when body is undefined in request init #1666

jaredLunde opened this issue May 17, 2024 · 6 comments · Fixed by #1672
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@jaredLunde
Copy link
Contributor

jaredLunde commented May 17, 2024

Description

Uncaught TypeError: Cannot set property body of # which has only a getter

Screenshot 2024-05-17 at 4 53 14 PM

https://github.com/drwpow/openapi-typescript/blob/main/packages/openapi-fetch/src/index.js#L16-L20

Basically, body is a getter on the Request object and the linked code is trying to write to it. One work around could be to wrap it in a try/catch. Otherwise I think you'll have to ignore some properties in the request init

Reproduction

Add a body to the request init object in openapi-fetch@0.9.6 e.g. client.POST('/', {body:undefined}).

Happens in all major browsers AFAICT

Checklist

@jaredLunde jaredLunde added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels May 17, 2024
@FreeAoi
Copy link
Contributor

FreeAoi commented May 19, 2024

Hello! I can't reproduce the bug with the information provided, could you please create a repository where we can test it or let me know if there is another detail when doing the reproduction?

@jaredLunde
Copy link
Contributor Author

jaredLunde commented May 19, 2024

Perhaps it is related to Bun, Astro, or Vite in some way. I'm not 100% sure where the Request global is being set, but I had assumed it was the browser's. Unfortunately I can't figure out how to install Bun on StackBlitz to dig further into a repro. Regardless, there's clearly some incompatibility in the vast JS/TS ecosystem as a result of overwriting Request properties that are native to the Request object Finding some other means of adding them would probably be beneficial for that reason.

For example:

const nativeRequest = new Request()

class CustomRequest extends Request {
  constructor(input, init) {
    super(input, init);

    // add custom parameters
    for (const key in init) {
      if (!(key in nativeRequest)) {
        this[key] = init[key];
      }
    }
  }
}

Or

class CustomRequest extends Request {
  constructor(input, init) {
    super(input, init);

    // add custom parameters
    for (const key in init) {
      if (!(key in this)) {
        this[key] = init[key];
      }
    }
  }
}

@jaredLunde
Copy link
Contributor Author

Since body can be undefined, I'm guessing the naive check agains !this["body"] in the CustomRequest object is returning a false positive, while key in this would not return a false positive.

@FreeAoi
Copy link
Contributor

FreeAoi commented May 19, 2024

I will take a look on this and if its necessary I will make a PR to fix it

@jaredLunde
Copy link
Contributor Author

jaredLunde commented May 19, 2024

Oh wow, I'm so sorry. I have a repro: https://stackblitz.com/edit/vitejs-vite-3tdy1b?file=src%2FApp.tsx

If body is undefined you can reproduce the issue easily in the browser. client.POST('/', {body:undefined})

@jaredLunde jaredLunde changed the title Unable to set a body property in the request init Uncaught error when body is undefined in request init May 19, 2024
@FreeAoi
Copy link
Contributor

FreeAoi commented May 19, 2024

Alr makes sense https://github.com/drwpow/openapi-typescript/blob/main/packages/openapi-fetch/src/index.js#L17 return a false positive, you could do the PR fixing this by changing that conditional to !(key in this)

jaredLunde added a commit to jaredLunde/openapi-typescript that referenced this issue May 19, 2024
drwpow pushed a commit that referenced this issue May 19, 2024
)

* fix: only add custom properties that aren't in the request object

Closes #1666

* add changeset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants