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

String request payload is incorrectly serialized #48

Open
ondrejpar opened this issue Oct 12, 2022 · 3 comments
Open

String request payload is incorrectly serialized #48

ondrejpar opened this issue Oct 12, 2022 · 3 comments

Comments

@ondrejpar
Copy link

Relevant part of api.yml:

      requestBody:
        content:
          application/json:
            schema:
              type: string

The generated function has correct signature: myFunc(arg: string).
However, fetcher.ts only handles objects and arrays. The string "foo" is this serialized as {"0":"f","1":o","2":"o"}

Is there any workaround atm?

@ondrejpar
Copy link
Author

Found a workaround:
call the function like this:

myFunc({ _BODY_STRING: 'real data' } as unknown as string);

add fetch middleware like this:

fetcher.configure({
  baseUrl: "...",
  init: ...
  use: [
    async (url, init, next) => {
      const body = JSON.parse(init.body);
      if (body._BODY_STRING) {
        init.body = JSON.stringify(body._BODY_STRING);
      }
      return await next(url, init)
    }
  ]
});

Ugly AF but works for now.

@studiosciences
Copy link

studiosciences commented Nov 1, 2022

I don't think your spec is correct. It doesn't return application/json. It should be text/plain`.

The fetch function will return a type of unknown, which you can cast to a string. At least, that is what I am seeing. That isn't ideal, but you don't need the middleware.

@ondrejpar
Copy link
Author

@studiosciences it's requestBody specification, not responseBody specification. Also, it's not text/plain, it really is application/json - it is actually possible, although adimttedly unusual, to JSON serialize non-objects ('42' is valid JSON containing number 42, '"foo"' is valid JSON containing string "foo").

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

2 participants