question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated type for openapi response with a default value is marked as possibly undefined

See original GitHub issue

In my front-end I’m consuming a backend endpoint /test. The post needs a body of type Model where the a property is optional with a default value. The response from this endpoint is a Model where I know that a will contain an integer since a default value is given.

I.e. here is the openapi.json spec

{
  openapi: "3.0.2",
  info: {
    title: "FastAPI",
    version: "0.1.0",
  },
  paths: {
    "/test": {
      get: {
        summary: "Foo",
        operationId: "foo_test_get",
        requestBody: {
          content: {
            "application/json": {
              schema: {
                $ref: "#/components/schemas/Model",
              },
            },
          },
          required: true,
        },
        responses: {
          "200": {
            description: "Successful Response",
            content: {
              "application/json": {
                schema: {
                  $ref: "#/components/schemas/Model",
                },
              },
            },
          },
        },
      },
    },
  },
  components: {
    schemas: {
      Model: {
        title: "Model",
        required: ["b"],
        type: "object",
        properties: {
          a: {
            title: "A",
            type: "integer",
            default: 5,
          },
          b: {
            title: "B",
            type: "integer",
          },
        },
      },
    },
  },
}

Running the following command

npx openapi-typescript-codegen --input http://localhost:8000/openapi.json -o dist

produces the following type alias

export type Model = {
    'a'?: number;
    'b': number;
}

and service

import type { Model } from "../models/Model";
import { request as __request } from "../core/request";

export class Service {
  /**
   * Foo
   * @param requestBody
   * @returns Model Successful Response
   * @throws ApiError
   */
  public static async fooTestPost(requestBody: Model): Promise<Model> {
    const result = await __request({
      method: "POST",
      path: `/test`,
      body: requestBody,
    });
    return result.body;
  }
}

It looks pretty good but since a default value is given for a then I know the response from /test is actually going to be Promise<Required<Model>>:

{
    'a': number;
    'b': number;
}

I hope it make sense and I realise that maybe I need to fix it in openapi.json. I’m going back and forth on this matter and can’t decide. Whats you take on it?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ferdikoomencommented, Mar 12, 2021

@mr-bjerre just pushed a new version 0.9.1

2reactions
ferdikoomencommented, Feb 26, 2021

@mr-bjerre i see the issue indeed. I created this PR: https://github.com/ferdikoomen/openapi-typescript-codegen/pull/592 Will test the changes. Officially the required props need to be in the array, but the JAVA generator also marks properties with default values as non-optional, so will do some regression testing and then we could merge this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nest Swagger generates routes with undefined as response ...
According to the OAS3 spec, the response code must be a valid HTTP status code or "default", and that value can only be...
Read more >
OpenAPI Specification - Version 3.0.3 - Swagger
The OpenAPI Specification (OAS) defines a standard, language-agnostic interface to RESTful APIs which allows both humans and computers to discover and ...
Read more >
Object is possibly 'undefined' error in TypeScript | bobbyhadz
The "Object is possibly 'undefined'" error occurs when we try to access a property on an object that may be undefined (e.g. marked...
Read more >
TypeScript Generator Maven Plugin
Generates TypeScript declaration file from specified java classes. ... Function can return undefined if default name should be used.
Read more >
OpenAPI Specification
This is valid only for either query or formData parameters and allows you to send a parameter with a name only or an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found