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

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?
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\model\effect-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.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 17
  • Comments: 18 (2 by maintainers)

Most upvoted comments

@VincentVanclef AFAIK you need to fix your swagger definitions, I didn’t find any other way to force the properties to be non-optional

I ended up adding this scema:

    {
        public void Apply(OpenApiSchema schema, SchemaFilterContext context)
        {
            if (schema.Properties == null)
            {
                return;
            }

            var notNullableProperties = schema
                .Properties
                .Where(x => !x.Value.Nullable && !schema.Required.Contains(x.Key))
                .ToList();

            foreach (var property in notNullableProperties)
            {
                schema.Required.Add(property.Key);
            }
        }
    }`


services.AddSwaggerGen(c =>
            {
                c.DescribeAllParametersInCamelCase();

                c.SupportNonNullableReferenceTypes();
                c.SchemaFilter<RequiredNotNullableSchemaFilter>();```


would this be how you did it as well?

For many of us “nullable” mostly equals to “optional”, because it’s really exotic scenario when we want to get parameter explicitly set to “null” value. On the other hand, even more exotic scenario is non-nullable optional properties. Why would you expect to get non-nullable property set?

Maybe there can be a generator option something like ''treatNonNullablePropertiesAsOptional"? which will generate non-nullable non-optional fields for non-nullable properties and nullable-and-optional fields for nullable properties?

@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.

There’s a difference between a field being nullable (controlled by the “nullable” property in the OpenAPI spec, which pr default is false) and a field being optional (controlled inversely by the “required” property in the OpenAPI spec, which pr default is also false).

Nullable is represented like this in TypeScript:

export interface Interface1 {
   nullableField: string | null;
}

Optional is represented like this in TypeScript:

export interface Interface2 {
   optionalField?: string;
}

A field can in theory be both nullable and optional, and the OpenAPI default is that fields are optional, but not nullable. I agree with agilob, that the behavior of the generator is correct.

The linked JSON https://gist.github.com/Dunkhan/604694e5ae47c1162f784f1d3f3a78b9 is missing “required: true” for the non-optional properties.

@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.

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

And to add on top of your comment, a field can be required and nullable. Required means it needs to be specified with a value, and null is a value, as not specifying value will (optionally) use default value from spec. Gonna draw a Venn-diagram for this soon…

With that said, I think this issue can be closed now. @wing328