Skip to content

Dart generator: Add default value for optional params#21502

Closed
djibouleau wants to merge 1 commit intoOpenAPITools:masterfrom
djibouleau:dart_generator_optional_params
Closed

Dart generator: Add default value for optional params#21502
djibouleau wants to merge 1 commit intoOpenAPITools:masterfrom
djibouleau:dart_generator_optional_params

Conversation

@djibouleau
Copy link
Copy Markdown
Contributor

@djibouleau djibouleau commented Jun 30, 2025

When the spec mentions a default value for a parameter, it is recorded as an optional param. I updated the template to include the default value. Tested with FastAPI :

async def get_api_version(
    x_api_version: Annotated[str, Header(alias="X-API-Version")] = "1.2.3"
) -> str:
    return x_api_version

app = FastAPI(
    dependencies=[Depends(get_api_version)]
)

Generated code :

  /// Search
  ///
  /// Parameters:
  ///
  /// * [String] q (required):
  ///
  /// * [String] xAPIVersion:
  Future<List<Product>?> search(String q, { String? xAPIVersion = '1.2.3', }) async {
  ...

@Mattias-Sehlstedt
Copy link
Copy Markdown
Contributor

This looks to be generated client code? If that is the case, then I believe it is technically incorrect to submit the default value as a client with a call. See section Default Parameter Values here.

The default value is the value that the server will use if the client does not send a value for that field. So if the client is fine with the default, then it is best to omit sending a value. If the generated code mirrors the default, then one might misalign with the server if they were to change their default.

@djibouleau djibouleau closed this Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants