Skip to content

Add support for JSON:API media type #1188

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

Closed
gletournel opened this issue Apr 23, 2025 · 0 comments · Fixed by #1187
Closed

Add support for JSON:API media type #1188

gletournel opened this issue Apr 23, 2025 · 0 comments · Fixed by #1187

Comments

@gletournel
Copy link
Contributor

The generated API client does not comply with JSON:API specification.

When generating an API client for an OpenApi specification complying with JSON:API, the client ignores the JSON:API media type (application/vnd.api+json) declared in the request body definition and replaces it with the default application/json type.

This causes errors with API servers that strictly comply with the JSON:API specification. These fail to process requests with an unexpected content type, and in this specific case, they return a "415 Unsupported Media Type" error.

Let’s take the following spec example.

paths:
  /pets:
    post:
      summary: Add a new pet
      operationId: addPet
      requestBody:
        required: true
        content:
          application/vnd.api+json:
            schema:
              type: object
              properties:
                data:
                  $ref: '#/components/schemas/Pet'
      responses:
        '200':
          $ref: '#/components/responses/PetResponse'
        default:
          $ref: '#/components/responses/ErrorResponse'

The generated client looks like this.

export class Api<
  SecurityDataType extends unknown,
> extends HttpClient<SecurityDataType> {
  pets = {
    addPet: (
      data: {
        data: Pet;
      },
      params: RequestParams = {},
    ) =>
      this.request<
        {
          data: Pet;
        },
        {
          errors: Error[];
        }
      >({
        path: \`/pets\`,
        method: "POST",
        body: data,
        type: ContentType.JsonApi,
        ...params,
      }),
  };
}

In the request properties, we get type: ContentType.Json while we expect the client to send the application/vnd.api+json media type.

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

Successfully merging a pull request may close this issue.

1 participant