Openapi-generator: [BUG][typescript-angular] All properties generated with "?:" even when they are not optional

Created on 1 Oct 2020  路  3Comments  路  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
Description

All properties generated by the openapi-generator are defined with ?: e.g.

/**
* Gets or sets the angle of reflection.
*/
angle?: number;

expected:

/**
* Gets or sets the angle of reflection.
*/
angle: number;
openapi-generator version

4.3.1

OpenAPI declaration file content or url

https://gist.github.com/Dunkhan/604694e5ae47c1162f784f1d3f3a78b9

Generation Details
npx openapi-generator generate -i D:\Work\testApi.json -g typescript-angular -c ./generate.api.options.json -o dist/api

generate options:

{  
   "npmName": "my-api",  
   "ngVersion": "9.1.0",
   "fileNaming": "kebab-case",
   "apiModulePrefix": "myPrefix"
}
Steps to reproduce

Run the command
Look in the file
\dist\api\modeleffect-angle-values.ts

Related issues/PRs

none found

Suggest a fix

I am fully aware I might be doing something wrong, missing a flag, or reporting an intended behaviour. Please help me out if this is the case by explaining how I can get the generator to generate without the ?

I tried the flag nullSafeAdditionalProps but I found that this simply added "| null" to all nullable properties. This is good and I intend to keep using it but it does not solve the problem.

The reason this is an issue is that it I wish to use strict type checking and the properties being potentially undefined means that I have to fill all my code with undefined checks. These properties should never be undefined.

TypeScript Bug

Most helpful comment

@agilob I don't know how the tests look, but they are wrong. By definition nullable is false by default(https://swagger.io/specification/)! Yet not setting nullable at all treats nullable as true.

All 3 comments

馃憤 Thanks for opening this issue!
馃彿 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Your schema must be wrong. Required/optional fields are supported and tested for.
In the example schema

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/2_0/petstore.yaml

      parameters:
        - name: status
          in: query
          description: Status values that need to be considered for filter
          required: true
          type: array
          items:
            type: string
            enum:
              - available
              - pending
              - sold
            default: available

it generates correct output

image

add required: true

@agilob I don't know how the tests look, but they are wrong. By definition nullable is false by default(https://swagger.io/specification/)! Yet not setting nullable at all treats nullable as true.

Was this page helpful?
0 / 5 - 0 ratings