Generated type for openapi response with a default value is marked as possibly undefined
See original GitHub issueIn 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:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@mr-bjerre just pushed a new version 0.9.1
@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.