Skip to content

Dart generator: Add default value for optional params #21502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

djibouleau
Copy link
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
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.

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