Generated toJson and safeToJson skip model-level refinements

未关闭
#263 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
42/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
dart

调研方向

Start by tracing how @Schemable() generates WindowSchema, the loader, and toJson/safeToJson; compare that path with Window.schema.safeEncode and its model-level refinement. Identify a generator-supported registration point that avoids a recursive Window.schema reference. Done means the generated loader and encode helpers enforce the refinement, with WindowSchema.schema still usable as a base.

由索引模型根据 Issue 内容生成。

描述

Reproduction

Verified on Ack and ack_generator 2.0.0-beta.3 from main at b8eb8eb with Dart 3.14.0. Put this in lib/encode_repro.dart in a package with ack, ack_generator, and build_runner, then run dart run build_runner build and dart run lib/encode_repro.dart.

import 'package:ack/ack.dart';

part 'encode_repro.g.dart';

@Schemable()
final class const Window({required final int start, required final int end})
    with _$WindowAck {
  static final schema = WindowSchema.schema.refine(
    (window) => window.start <= window.end,
    message: 'start must not exceed end',
  );
}

void main() {
  const invalid = Window(start: 2, end: 1);
  print(invalid.toJson());
  print(invalid.safeToJson().isOk);
  final checked = Window.schema.safeEncode(invalid);
  print(checked.isOk);
  print(checked.getError().runtimeType);
}
Actual

toJson() returns {start: 2, end: 1} and safeToJson().isOk is true. The same instance sent through Window.schema.safeEncode fails with SchemaValidationError. The generated helpers go through WindowSchema, which knows the field rules but not the cross-field refinement on Window.schema.

Suggested change

Provide an explicit generator-supported way to register a model-level refinement so it is part of the schema used by both the generated loader and the generated encode helpers. Keep WindowSchema.schema usable as a base without introducing a recursive reference to Window.schema. Until then, callers can use Window.schema.safeParse and Window.schema.safeEncode explicitly.

主要语言
Dart
星标
64
派生
3
平均合并
17 小时 32 分钟
30 天内合并 PR
33

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

btwld/ack 的其他 Issue

查看 btwld/ack 的全部 Issue

相似的 Issue

更多 Dart Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。