diff --git a/example/lib/class_first_models.g.dart b/example/lib/class_first_models.g.dart index 977d3783..bb2c30a0 100644 --- a/example/lib/class_first_models.g.dart +++ b/example/lib/class_first_models.g.dart @@ -394,11 +394,11 @@ abstract final class AccountSchema { }) => Account.schema.safeEncode(value, debugName: debugName); /// The JSON Schema that describes [Account]. - static Map toJsonSchema() => _accountSchema.toJsonSchema(); + static Map toJsonSchema() => Account.schema.toJsonSchema(); /// The schema model that describes [Account]. static AckSchemaModel toSchemaModel() => - AckSchemaModelExtension(_accountSchema).toSchemaModel(); + AckSchemaModelExtension(Account.schema).toSchemaModel(); } Account _$AccountFromRuntime(Map $ackValue) => Account( diff --git a/packages/ack_generator/CHANGELOG.md b/packages/ack_generator/CHANGELOG.md index 9d10f7ca..57a5f3be 100644 --- a/packages/ack_generator/CHANGELOG.md +++ b/packages/ack_generator/CHANGELOG.md @@ -71,6 +71,15 @@ ### Fixed +* Generated `toJsonSchema()` and `toSchemaModel()` on a class-first facade + export the class-owned `schema`, the same schema `parse` and `encode` use. + A `.describe(...)` or an exportable constraint on that schema now appears in + the export. A class-owned `schema` initializer that reads the facade's + `toJsonSchema()` or `toSchemaModel()` now recurses; read + `XSchema.schema.toJsonSchema()` or `XSchema.schema.toSchemaModel()` there + instead. Keep a class-owned `schema` non-nullable: the facade's typed `parse` + never returns `null`. + * Report a build error when `normalize_unicode: true` is enabled and `unorm_dart` is missing from the package resolution. diff --git a/packages/ack_generator/lib/src/builders/class_model_emitter.dart b/packages/ack_generator/lib/src/builders/class_model_emitter.dart index 5478387c..bd7fabb5 100644 --- a/packages/ack_generator/lib/src/builders/class_model_emitter.dart +++ b/packages/ack_generator/lib/src/builders/class_model_emitter.dart @@ -232,11 +232,11 @@ abstract final class ${metadata.facadeName} { /// The JSON Schema that describes [$className]. static Map toJsonSchema() => - ${metadata.backingName}.toJsonSchema(); + $validationSchema.toJsonSchema(); /// The schema model that describes [$className]. static ${_ack('AckSchemaModel')} toSchemaModel() => - ${_ack('AckSchemaModelExtension')}(${metadata.backingName}).toSchemaModel(); + ${_ack('AckSchemaModelExtension')}($validationSchema).toSchemaModel(); }'''; } diff --git a/packages/ack_generator/test/integration/ack_infer_runtime_build_test.dart b/packages/ack_generator/test/integration/ack_infer_runtime_build_test.dart index d94bc380..90d07eb2 100644 --- a/packages/ack_generator/test/integration/ack_infer_runtime_build_test.dart +++ b/packages/ack_generator/test/integration/ack_infer_runtime_build_test.dart @@ -256,6 +256,16 @@ final class RefinedWindow with _$RefinedWindowAck { ); } +@Schemable() +final class DescribedWindow with _$DescribedWindowAck { + const DescribedWindow({required this.start, required this.end}); + final int start; + final int end; + static final schema = DescribedWindowSchema.schema + .refine((window) => window.start <= window.end) + .describe('A time window'); +} + @Schemable() final class RefinedBooking with _$RefinedBookingAck { const RefinedBooking({required this.window}); @@ -658,6 +668,12 @@ void main() { expect(RefinedBookingSchema.safeParse({'window': json}).isFail, isTrue); }); + test('facade exports describe the class-owned schema', () { + expect(DescribedWindowSchema.toJsonSchema()['description'], 'A time window'); + expect(DescribedWindowSchema.toSchemaModel().description, 'A time window'); + expect(DescribedWindowSchema.schema.description, isNull); + }); + test('union branches inherit the base style and honor explicit overrides', () { expect(const StyleCat(livesLeft: 9).toJson(), {'kind': 'cat', 'lives_left': 9}); expect(