openapi-generator
openapi-generator copied to clipboard
[BUG] [CSharp] optional properties should use boxed type
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)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
As far as I could see, most other generators use boxed types instead of their primitive counterpart. In the CSharp generators, boxed types are only used, when nullable is set to true. Given an optional property that is not nullable, there is no way to see if it is provided in the request, or if it is not and the default value for the type was used. The primitive datatype should only be used for required, non-nullable properties. In other cases, the boxed type should be used to allow properties to be null if they are not set as it is also the case for most other languages.
openapi-generator version
7.8.0
OpenAPI declaration file content or url
openapi: 3.0.1
info:
title: title
version: 0.0.1
paths:
/test:
get:
parameters:
- name: deepObject
in: query
schema:
$ref: '#/components/schemas/DeepObject'
explode: true
style: deepObject
responses:
204:
description: no content
components:
schemas:
DeepObject:
type: object
required:
- requiredProperty
properties:
requiredProperty:
type: integer
format: int32
optionalProperty:
type: integer
format: int32
Generation Details
This will generate:
public DeepObject(int requiredProperty = default(int), int optionalProperty = default(int))
{
this.RequiredProperty = requiredProperty;
this.OptionalProperty = optionalProperty;
}
but expected is
public DeepObject(int requiredProperty = default(int), int? optionalProperty = default(int?))
{
this.RequiredProperty = requiredProperty;
this.OptionalProperty = optionalProperty;
}
Steps to reproduce
npm install @openapitools/openapi-generator-cli -g
openapi-generator-cli version-manager set 7.8.0
npx @openapitools/openapi-generator-cli generate -i ./openapi.yaml -g csharp
Related issues/PRs
couldn't find any
Suggest a fix
#19463
Optional properties should be defined as Option<T?> so null and omitted can be tracked separately.
@devhl-labs that would be even better I admit, however, that is not how its is currently handled for most other languages. To do that, we should extend the function getTypeDeclaration(Schema schema) in DefaultCodegen.java and all inheriting classes, that is used across all generators, so it takes an additional boolean isRequired. Otherwise there is no way to know, when to use the optional type. So this certainly means a bigger amount of effort to adapt all generators.
have you tried enabling the normalizer rule SET_PRIMITIVE_TYPES_TO_NULLABLE?
this will add nullable:true to the primitive type set in the rule and I think the output should meet your requirement
https://github.com/openapitools/openapi-generator/blob/master/docs/customization.md#openapi-normalizer
@wing328 thank you for the hint, I was not aware of this normalizer rule. It does indeed seem like this could meet our requirement.