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

Allow for custom types in path params serialization (and maybe other places serialization too) #1660

Open
1 task done
JeanRemiDelteil opened this issue May 13, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-fetch Relevant to the openapi-fetch library

Comments

@JeanRemiDelteil
Copy link
Contributor

JeanRemiDelteil commented May 13, 2024

Description

In an OpenAPI spec we have some type that are annotated with a specific format:

      properties:
        id:
          type: string
          format: uuid
          description: a unique identifier

You can also imagine having other format for the same JS primitive (emails, for example).
We want to leverage this format to enforce some checks on the format, and build "safe value".

To do so:

  • In the generated TS types, the format: 'uuid' for type string is replaced by the Uuid type.
parameters: {
  path: {
    id: Uuid;
  };
};
  • The Uuid class extending String is used as constructor when deserializing.

The problem lies in the usage with the fetch api:

const petId: string = "0000-0000-0000";

api.GET('/api/pets/pet/{id}', {
  params: {
    path: {
      // TOFIX: Here the type of the 'id' key is incorrect, because if its type is "object" it will not be deserialized as a string, but as an object.
      // id: new Uuid(petId), // DOES NOT WORK even if Uuid extends String
      id: petId as unknown as Uuid, // Works, but it's a hack (string typed as Uuid)
    },
  },
})

Proposal

In the method defaultPathSerializer, the path parameters value's are tested in this order:

  • Array.isArray(value)
  • typeof value === "object"
  • default to string processing

If changed to the following it would allow custom string formats.

  • Array.isArray(value)
  • typeof value === "object" && !(value instanceof String)
  • default to string processing

Another possibility would be to allow passing a custom path serializer in the api builder, as the query and the body serializers.

Checklist

@JeanRemiDelteil JeanRemiDelteil added enhancement New feature or request help wanted Extra attention is needed openapi-fetch Relevant to the openapi-fetch library labels May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

1 participant