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.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
VincentVanclefcommented, Aug 19, 2021

@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?
5reactions
BigApple1988commented, Jul 30, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Schema allows additional properties
If you are using combining operations for primitives with no properties, additionalProperties can be false and the combining operations still work. If you...
Read more >
Swagger specification · GitBook - Goswagger.Io
The "default" section must contain any "required" properties, which is ... not show even I just use swagger generate model -f person.yaml because...
Read more >
How to configure Swashbuckle to ignore property on model
If you need to do this but without using JsonIgnore (maybe you still need to serialize/deserialize the property) then just create a custom...
Read more >
Component Inspector | Figma Community
This plugin does not generate style code. It generates code that describes component properties. Currently supporting instance and compone...
Read more >
[BUG] [typescript-angular] syntax error in *.service.ts files ...
Bug Report Checklist[x] Have you provided a full/minimal spec to reproduce the issue?[ ] Have you validated the input us...
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