OpenAPITools / OpenAPITools/openapi-generator

[BUG][Dart] Should use final for variables

Open
#17,222 4 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When generating Dart code, I get nullable values and this comment:

  ///
  /// Please note: This property should have been non-nullable! Since the specification file
  /// does not include a default value (using the "default:" property), however, the generated
  /// source code must fall back to having a nullable type.
  /// Consider adding a "default:" property in the specification file to hide this note.
  ///

I don't understand why a default value is required for a value that should always be specified?

openapi-generator version

master

Steps to reproduce

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i http://localhost:8080/openapi.yaml --additional-properties=serializationLibrary=json_serializable -g dart -o output

Suggest a fix

Ideally, the variables should be defined as final and could look something like this:

@JsonSerializable()
class Auth {
  final String userId;
  final String token;

  Auth(this.userId, this.token);

  static Auth fromJson(Map<String, dynamic> json) => _$AuthFromJson(json);
  Map<String, dynamic> toJson() => _$AuthToJson(this);
}

Instead, the generated code looks more like this:

class Auth {
  /// Returns a new [Auth] instance.
  Auth({
    this.userId,
    this.token,
  });

  ///
  /// Please note: This property should have been non-nullable! Since the specification file
  /// does not include a default value (using the "default:" property), however, the generated
  /// source code must fall back to having a nullable type.
  /// Consider adding a "default:" property in the specification file to hide this note.
  ///
  String? userId;

  ///
  /// Please note: This property should have been non-nullable! Since the specification file
  /// does not include a default value (using the "default:" property), however, the generated
  /// source code must fall back to having a nullable type.
  /// Consider adding a "default:" property in the specification file to hide this note.
  ///
  String? token;
  
  ...
}

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 with the documented openapi-generator-cli command and the generated Dart Auth class shown in the issue. Compare how required properties without defaults are represented and determine the generator entry point and tests covering Dart serialization. Done means the intended required-property behavior is generated consistently without the nullable warning.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.