swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

application/json content type is not set for body that only has one field

Open benknab opened this issue 1 year ago • 1 comments

this is one of our endpoints with a body that may accept two fields:

  /**
   * No description
   *
   * @tags Custodians
   * @name PostSecuredCustodiansCreate
   * @request POST:/v1/secured/custodians/create
   */
  postSecuredCustodiansCreate = (
    body: {
      name: string;
      notes?: string;
    },
    params: RequestParams = {}
  ) =>
    this.request<
      {
        status: 'ok';
        data: {
          id: string;
          /** @format date-time */
          createdAt: string;
          /** @format date-time */
          updatedAt: string;
          name: string;
          notes?: string;
        };
      },
      any
    >({
      path: `/v1/secured/custodians/create`,
      method: 'POST',
      body: body,
      type: ContentType.Json,
      format: 'json',
      ...params,
    });

and in the same file, any endpoint that requires one field has missing type in request options:

  /**
   * No description
   *
   * @tags Custodians
   * @name PostSecuredCustodiansGet
   * @request POST:/v1/secured/custodians/get
   */
  postSecuredCustodiansGet = (
    body: {
      id: string;
    },
    params: RequestParams = {}
  ) =>
    this.request<
      {
        status: 'ok';
        data: {
          id: string;
          /** @format date-time */
          createdAt: string;
          /** @format date-time */
          updatedAt: string;
          name: string;
          notes?: string;
        };
      },
      any
    >({
      path: `/v1/secured/custodians/get`,
      method: 'POST',
      body: body,
      format: 'json',
      ...params,
    }); // `type` arg is missing from here, client tries to send the body as text

benknab avatar Feb 16 '24 15:02 benknab

I found the following api definition to be the minimal example that reproduces the issue:

{
  "swagger": "2.0",
  "paths": {
    "/route-1": {
      "post": {
        "parameters": [
          {
            "in": "body",
            "schema": {
              "type": "object"
            }
          }
        ]
      }
    },
    "/route-2": {
      "post": {
        "parameters": [
          {
            "in": "body",
            "schema": {
              "type": "object"
            }
          }
        ]
      }
    }
  }
}

Update: This has to do with the handling of OpenAPI v2 schemas, but I was not able to find where the root cause was. Upgrading the spec to use OpenApi v3 fixes the issue, which is of course only possible when having access to the provider of the spec.

maxstevens-nl avatar May 21 '24 19:05 maxstevens-nl