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:
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:
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
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:
Observed Output: In the generated tree.g.dart, the _$BranchJsonSchema includes proportion in the required list:
Expected Behavior
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