[Question] Mismatched default value type handling
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 333
- Avg merge
- 16h 29m
- Merged PRs (30d)
- 116
Description
## Description
When the default value of a field doesn't match the type a warning is emitted by `kiota`, but, in this case, the generated code is not going to compile for `allOf` the non-dynamic languages.
## Original issue
Faced this issue [here](https://github.com/APIs-guru/openapi-directory/blob/cbd39847f03828150e77c26b6307a111049e9999/APIs/openai.com/1.2.0/openapi.yaml#L2189) in the OpenAI spec.
## Reproducer
A minimal spec like this:
```yaml
openapi: 3.0.0
info:
title: Test
version: 1.0.0
description: something
paths:
/api/something/v1:
post:
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
max_tokens:
default: inf
type: integer
nullable: true
responses:
"200":
description: "something"
content:
application/json: {}
```
This would produce the following Warning:
```
OpenAPI warning: #/paths/~1api~1something~1v1/post/requestBody/content/application~1json/schema/properties/max_tokens/default - Data and type mismatch found.
```
But it generates code that is not going to compile:
```csharp
public int? MaxTokens { get; set; }
...
MaxTokens = "inf";
```
## Desired resolution
I think that we can improve over the current situation, as having code generated but that is going to fail to compile is suboptimal.
There are a few directions we can consider:
1. Decide that this mismatch is a fatal error (for all non-dynamic langs at least ...) and abort the generation
2. Since we are already emitting a warning, skip the assignment so that the generated code will compile
3. In this specific case the intention is pretty clear, should we handle special numeric values? ( [upstream ref](https://github.com/OAI/OpenAPI-Specification/issues/603) maybe @darrelmiller knows more about)
4. Another option would be to generate code such as: `Int32.Parse("inf")` so that the error will be moved at the user's runtime. Possibly throwing the relevant exception only in case the `default` needs to get applied.
Happy to hear more feedback!
Contributor guide
Assessment
This issue has not been assessed yet.