Skip to content

Add support for JSON:API media type #1188

Open
@gletournel

Description

@gletournel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions