Remove toJson() and copyWith() from code generation#100
Conversation
Generated extension types already implement their representation type, making toJson() redundant. copyWith() added complexity (transformed field suppression logic) for limited value. This also removes the now-dead isTransformedSchema and isTransformedRepresentation fields that only existed to support copyWith suppression.
|
To view this pull requests documentation preview, visit the following URL: Documentation is deployed and generated using docs.page. |
There was a problem hiding this comment.
Pull request overview
This PR removes toJson() and copyWith() from @AckType-driven code generation, simplifying the generated API surface and deleting the analyzer plumbing that previously existed only to support conditional copyWith()/transformed-schema handling.
Changes:
- Stop generating
toJson()on all generated types and stop generatingcopyWith()on object/discriminated wrappers. - Remove
ModelInfo.isTransformedSchema/FieldInfo.isTransformedRepresentationand related schema AST analysis plumbing. - Update integration/example tests and user docs; delete now-obsolete dedicated
toJson/copyWithtests.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/ack_generator/test/src/test_utilities.dart | Updates test mocks to match removed FieldInfo flags. |
| packages/ack_generator/test/integration/example_folder_build_test.dart | Adjusts expectations to no longer require copyWith. |
| packages/ack_generator/test/integration/ack_type_transform_test.dart | Removes toJson/copyWith assertions and deletes a copyWith-specific test. |
| packages/ack_generator/test/integration/ack_type_to_json_test.dart | Deletes integration test for toJson() generation. |
| packages/ack_generator/test/integration/ack_type_enum_literal_fields_test.dart | Updates assertions that previously depended on copyWith-parameter typing. |
| packages/ack_generator/test/integration/ack_type_discriminated_test.dart | Removes discriminated-branch copyWith expectations and related test coverage. |
| packages/ack_generator/test/integration/ack_type_cross_file_resolution_test.dart | Renames/updates tests to focus on cross-file resolution without copyWith behavior. |
| packages/ack_generator/lib/src/models/model_info.dart | Removes isTransformedSchema from the generator model. |
| packages/ack_generator/lib/src/models/field_info.dart | Removes isTransformedRepresentation from field metadata. |
| packages/ack_generator/lib/src/generator.dart | Stops propagating the removed isTransformedSchema flag. |
| packages/ack_generator/lib/src/builders/type_builder.dart | Removes toJson()/copyWith() emission code paths from generated types. |
| packages/ack_generator/lib/src/analyzer/schema_ast_analyzer.dart | Removes transformed-representation tracking from AST analysis records and FieldInfo construction. |
| packages/ack_annotations/lib/src/ack_type.dart | Updates annotation docs to reflect the new generated API surface. |
| example/test/schema_variable_test.dart | Removes example test that used generated copyWith(). |
| example/test/extension_type_to_json_test.dart | Deletes example tests for generated toJson(). |
| example/lib/schema_types_transforms.g.dart | Updates committed example generated output to remove toJson(). |
| example/lib/schema_types_simple.g.dart | Updates committed example generated output to remove toJson()/copyWith(). |
| example/lib/schema_types_primitives.g.dart | Updates committed example generated output to remove toJson(). |
| example/lib/schema_types_edge_cases.g.dart | Updates committed example generated output to remove toJson()/copyWith(). |
| example/lib/schema_types_edge_cases.dart | Updates example expectations/comments to no longer mention toJson(). |
| example/lib/schema_types_discriminated.g.dart | Updates committed example generated output to remove toJson()/copyWith() on discriminated types. |
| example/lib/args_getter_example.g.dart | Updates committed example generated output to remove toJson()/copyWith(). |
| docs/core-concepts/typesafe-schemas.mdx | Removes doc references to toJson()/copyWith(). |
| docs/core-concepts/json-serialization.mdx | Updates doc bullets about generated outputs to remove toJson() mention. |
| refer('Map<String, Object?>'), | ||
| ]) | ||
| ..methods.addAll([ | ||
| // Override discriminator to read from _data for consistency with toJson() | ||
| // Override discriminator to read from _data. | ||
| // Factory constructors already validate the discriminator value matches | ||
| Method( | ||
| (m) => m |
There was a problem hiding this comment.
In buildDiscriminatedSubtype, there is now an unused local (nonDiscriminatorFields) left over from the removed copyWith generation. Please remove it (or otherwise use it) to avoid dead code / analyzer lints.
| /// All extension types include `parse()` and `safeParse()` factory methods, | ||
| /// and implement their validated representation type directly. |
There was a problem hiding this comment.
This PR updates the @AckType docs here, but the repo’s canonical llms.txt still documents generated copyWith for extension types. Since llms.txt is treated as the API reference, it should be updated in the same PR to remove/adjust those examples and guidance.
| @@ -41,7 +41,6 @@ typedef _SchemaTypeMapping = ({ | |||
| typedef _ListElementAnalysis = ({ | |||
| _SchemaTypeMapping mapping, | |||
| String elementRepresentationType, | |||
There was a problem hiding this comment.
_ListElementAnalysis no longer includes any transformed/representation flag, but _analyzeListElement’s doc comment still says it returns “whether the element is backed by a transformed schema.” Update that comment (or reintroduce a flag if still needed) so the type + docs stay consistent.
| String elementRepresentationType, | |
| String elementRepresentationType, | |
| bool backedByTransformedSchema, |
…ed objects Example showcases colorSchema with hex validation (.refine) and Color transform, nested profileSchema, and userWithColorSchema combining validated fields with nested types.
…iminated types, lists of discriminated types - favoriteColor: optional transformed schema generates ColorType? with null check - pet: nested discriminated schema (cat/dog) generates PetType with switch dispatch - pets: List<PetType> with .map() wrapping for discriminated elements
32 tests covering colorSchema, profileSchema, petSchema (discriminated), userWithColorSchema, optional transforms, nested discriminated types, and lists of discriminated types. All formatted and analyzed clean.
- Move catSchema, dogSchema, petSchema to example/lib/pet.dart (own generated file), verifying cross-file @AckType resolution works - Add .refine() on userWithColorSchema for cross-field validation (firstName != lastName), covering object-level refine codegen - Note: withDefault on TransformedSchema is not supported since TransformedSchema does not mix in FluentSchema
Remove unused code left over from toJson/copyWith removal: - _isBuiltInTransformedMethod in schema_ast_analyzer.dart - nonDiscriminatorFields local variable in type_builder.dart - _referenceFromDartType, _isNullable, _typeReference, _urlForElement in type_builder.dart - Unused nullability_suffix import
- Remove copyWith examples from llms.txt (lines 385, 496-502, 564-565) since copyWith is no longer generated - Fix _analyzeListElement doc comment that still referenced a removed transformed schema flag in _ListElementAnalysis
Summary
toJson()generation from all extension types (regular, sealed class, discriminated base/subtype) — redundant since types already implement their representation typecopyWith()generation from object extension types and discriminated subtypesisTransformedSchemaonModelInfo,isTransformedRepresentationonFieldInfo, and all associated analyzer/test plumbingTest plan
toJson()orcopyWith()in generated.g.dartfilesisTransformedSchemaorisTransformedRepresentationreferences