Skip to content

[Question] Mismatched default value type handling #3398

@andreaTP

Description

@andreaTP

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 in the OpenAI spec.

Reproducer

A minimal spec like this:

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:

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 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!

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestgeneratorIssues or improvements relater to generation capabilities.

Type

No type

Projects

Status

New📃

Relationships

None yet

Development

No branches or pull requests

Issue actions