OpenAPITools / OpenAPITools/openapi-generator
[BUG] typescript-angular api.service does not respect nullable: true returnType
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Adding nullable: true to a return type of a service is not respected in the generated client code.
openapi-generator version
in package.json
"@openapitools/openapi-generator-cli": "^2.7.0",
and in openapitools.json
"generator-cli": {
"version": "7.0.1"
}
OpenAPI declaration file content or url
openapi: 3.0.0
paths:
/api/employer/getForUser:
get:
operationId: EmployerController_getForUser
parameters: []
responses:
'200':
description: ''
content:
application/json:
schema:
allOf:
- $ref: '#/components/schemas/ApiResponseEmployerAppModel'
nullable: true
tags:
- employer
security:
- bearer: []
info:
title: API
description: API Documentation
version: 1.0.0
contact: {}
tags: []
servers: []
components:
securitySchemes:
bearer:
scheme: bearer
bearerFormat: JWT
type: http
schemas:
ApiResponseEmployerAppModel:
type: object
properties:
name:
type: string
Generation Details
Steps to reproduce
using editor.swagger.io
- use editor.swagger.io to generate typescript-angular client code for the provided spec
- return type of service method is not nullable
using the openapi-generator tool
openapi-generator-cli generate -i PATH_TO_SPEC -g typescript-angular -o ./src/app/api --additional-properties=ngVersion=15.2.1,supportsES6=true,withInterfaces=true,stringEnums=true --skip-validate-spec --reserved-words-mappings long=lon
Related issues/PRs
Suggest a fix
I suspect that a check for the isNullable property can be added in the mustache template:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache#L176C687-L176C687
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache, especially the referenced line around 176, and reproduce the issue with the provided OpenAPI schema and typescript-angular generation command. Compare the generated service return type with the nullable response declaration; done means the generated client preserves nullable: true in that return type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100