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

Open Beginner friendly
#684 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
typescript
Domain
api

Research direction

The issue is in the generated client code where endpoints with a single-field body are missing the type: ContentType.Json parameter. Look at the code generator that produces these TypeScript functions, likely in a file handling request template generation. Compare how endpoints with one field vs. multiple fields are processed. The fix is to ensure the type argument is included for all JSON body requests. Test by regenerating the client for an OpenAPI spec with a single-field body endpoint and verifying the ContentType is set.

Written by the indexing model from the issue text.

Description

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
Dominant language
TypeScript
Stars
4.1k
Forks
436
Avg merge
9d 7h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from acacode/swagger-typescript-api

All issues in acacode/swagger-typescript-api

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.