Skip to content

Remove toJson() and copyWith() from code generation#100

Merged
leoafarias merged 7 commits intomainfrom
leoafarias/rm-tojson-copywith
Mar 27, 2026
Merged

Remove toJson() and copyWith() from code generation#100
leoafarias merged 7 commits intomainfrom
leoafarias/rm-tojson-copywith

Conversation

@leoafarias
Copy link
Copy Markdown
Collaborator

Summary

  • Removes toJson() generation from all extension types (regular, sealed class, discriminated base/subtype) — redundant since types already implement their representation type
  • Removes copyWith() generation from object extension types and discriminated subtypes
  • Removes dead code that only existed to support these features: isTransformedSchema on ModelInfo, isTransformedRepresentation on FieldInfo, and all associated analyzer/test plumbing
  • Updates annotation docs, user-facing docs, and all affected tests; deletes dedicated toJson/copyWith test files

Test plan

  • All 255 generator tests pass
  • All 101 example tests pass
  • Verified no remaining toJson() or copyWith() in generated .g.dart files
  • Verified no remaining isTransformedSchema or isTransformedRepresentation references

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.
Copilot AI review requested due to automatic review settings March 27, 2026 02:36
@docs-page
Copy link
Copy Markdown

docs-page bot commented Mar 27, 2026

To view this pull requests documentation preview, visit the following URL:

docs.page/btwld/ack~100

Documentation is deployed and generated using docs.page.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 generating copyWith() on object/discriminated wrappers.
  • Remove ModelInfo.isTransformedSchema / FieldInfo.isTransformedRepresentation and related schema AST analysis plumbing.
  • Update integration/example tests and user docs; delete now-obsolete dedicated toJson/copyWith tests.

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.

Comment on lines 294 to 300
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
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +167 to +168
/// All extension types include `parse()` and `safeParse()` factory methods,
/// and implement their validated representation type directly.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@@ -41,7 +41,6 @@ typedef _SchemaTypeMapping = ({
typedef _ListElementAnalysis = ({
_SchemaTypeMapping mapping,
String elementRepresentationType,
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_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.

Suggested change
String elementRepresentationType,
String elementRepresentationType,
bool backedByTransformedSchema,

Copilot uses AI. Check for mistakes.
…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
@leoafarias leoafarias merged commit 9cedb2b into main Mar 27, 2026
3 checks passed
@leoafarias leoafarias deleted the leoafarias/rm-tojson-copywith branch March 27, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants