Json Schema | Getters are incorrectly included in properties and required lists
Type: bug
_First of all, thank you for the incredible work on this package and for adding the JSON Schema generation feature. It's a huge time-saver that previously had to be done manually, and it's very much appreciated!_
When using the new _createJsonSchema: true_ experimental feature, read-only getters are being included in the generated JSON Schema. Furthermore, these getters are being marked as required, even when they are not part of the class constructor and cannot be passed during deserialization.
Currently, there are the fields _includeFromJson_ and _includeToJson_, but the field **_includeSchema_** is missing. These are currently ignored for the schema.
**To Reproduce** Given the following data structure:
```dart
import 'package:json_annotation/json_annotation.dart';
part 'tree.g.dart';
@JsonSerializable(createJsonSchema: true)
final class Tree {
final List<Branch> branches;
final List<Root> roots;
@JsonKey(unknownEnumValue: TreeType.tree)
final TreeType type;
final String? customName;
final int insideHollows;
const Tree({
required this.branches,
required this.roots,
required this.type,
required this.customName,
required this.insideHollows,
});
}
@JsonSerializable(createJsonSchema: true)
final class Branch {
final double length;
final List<Branch> branch;
const Branch({required this.length, required this.branch});
factory Branch.fromJson(Map<String, dynamic> json) => _$BranchFromJson(json);
// This getter should NOT be in the schema, especially as required
@JsonKey(includeFromJson: false, includeToJson: false, required: false)
double get proportion => length / branch.length;
}
@JsonEnum(alwaysCreate: true)
enum TreeType { pine, birch, cherry_tree, tree }
```
**Observed Output**: In the generated tree.g.dart, the _$BranchJsonSchema includes proportion in the required list:
```dart
const _$BranchJsonSchema = {
r'$schema': 'https://json-schema.org/draft/2020-12/schema',
'type': 'object',
'properties': {
'length': {'type': 'number'},
'branch': {
'type': 'array',
'items': {r'$ref': r'#/$defs/Branch'},
},
'proportion': {'type': 'number'}, // Should not be here if it's a getter
},
'required': ['length', 'branch', 'proportion'], // 'proportion' is incorrectly marked as required
};
```
**Expected Behavior**
- **Getters Exclusion:** Fields that are not parameters in the primary constructor (like getters) should not be included in the JSON Schema properties by default, or at the very least, should not be in the required list.
- **Annotation Respect:** The schema generator should respect @JsonKey(includeFromJson: false)
- **Feature Request:** It would be beneficial to have a specific flag like includeJsonSchema: bool in @JsonKey for granular control.
JSON Schema is often used to validate input before calling fromJson, including read-only getters as required makes the schema validation fail for valid payloads that only contain constructor-required data
关闭于 2026-02-05 6 条评论