OpenAPIから自動生成したモデルでError: Undefined name 'truefalse'.
未分類
Lastmod: 2023-10-09

問題

Flutterで開発している時にopenapi-generatorを使用してOpenAPI Schemaからモデルファイルを生成してビルドすると下記のエラーが発生した。

openapi/generated/client/lib/src/model/user_profile.dart:90:20: Error: Undefined name 'truefalse'.
    includeIfNull: truefalse
                   ^^^^^^^^^
openapi/generated/client/lib/src/model/user_profile_screen_response_data.dart:95:20: Error: Undefined name 'truefalse'.
    includeIfNull: truefalse
                   ^^^^^^^^^

実際に生成されたコードは以下のようなものになっている。

  @JsonKey(
    
    name: r'birthday',
    required: true,
    includeIfNull: truefalse
  )

原因と解決法

以下のように、requiredかつnullableである場合に発生した。

type: object
required:
  - birthday
properties:
  birthday:
    type: string
    format: date-time
    nullable: true

どちらかを削除すればtruefalseがtrueかfalseになる。

{
  "birthday": null
}

でbirthdayは必須、のような実装をしたつもりであった。