OpenAPITools / OpenAPITools/openapi-generator

[REQ][Dart] Generator adds unnecessary null handling for non-nullable primitive responses

Open
#20,266 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description:
The Dart generator adds unnecessary null handling code for non-nullable primitive responses, making the return type nullable when it shouldn't be.
Is there an option to prevent this behavior?

Current behavior:

Future<bool?> hasUnreadNotification() async {
  final response = await hasUnreadNotificationWithHttpInfo();
  if (response.statusCode >= HttpStatus.badRequest) {
    throw ApiException(response.statusCode, await _decodeBodyBytes(response));
  }
  // When a remote server returns no body with a status of 204, we shall not decode it.
  // At the time of writing this, `dart:convert` will throw an "Unexpected end of input"
  // FormatException when trying to decode an empty string.
  if (response.body.isNotEmpty && response.statusCode != HttpStatus.noContent) {
    return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'bool',) as bool;
  }
  return null;
}

Expected behavior:

Future<bool> hasUnreadNotification() async {
  final response = await hasUnreadNotificationWithHttpInfo();
  if (response.statusCode >= HttpStatus.badRequest) {
    throw ApiException(response.statusCode, await _decodeBodyBytes(response));
  }
  return await apiClient.deserializeAsync(await _decodeBodyBytes(response), 'bool',) as bool;
}

Backend endpoint:

@GetMapping("/has-unread-notification")
fun hasUnreadNotification(): Boolean {
    return dashboardService.hasUnreadNotification()
}

Version:

  • OpenAPI Generator: 7.10.0
  • Dart SDK: 3.24.3

Additional context:
The backend endpoint always returns a non-nullable boolean, but the generator adds unnecessary null handling code and makes the return type nullable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the generated Dart client for the shown non-nullable Boolean endpoint and inspect how the Dart generator handles primitive responses. Done means the generated method returns Future, avoids unnecessary null handling for a 204 or empty body, and preserves the expected deserialization behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.