OpenAPITools / OpenAPITools/openapi-generator

[REQ][DART-DIO] copyWith extension for json_serializable as the serializationLibrary

Open
#21,665 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

The default 'built_value' provides a builder pattern to 'rebuild' an object with modified properties. The 'json_serializable' option for the dart-dio generator generates completely frozen classes and in doing so, one has to mutate objects in a cumbersome way like so: final model2 = Model(model1.elem1, model1.elem2 + 1).

Describe the solution you'd like

Dart already leverages a common copyWith pattern to maintain immutability.
copyWithExtension package exists to generate a copyWith in the same part file. So the model classes can be annotated with the @CopyWith() annotation, in addition to the @JsonSerializable() annotation. The generated model/dtos would then have a generated copyWith() method in the same part file as the json_serializable's boilerplate. This would then more closely match the functionality given by default 'built_value' serialization library.

Describe alternatives you've considered

Post generation, I have run the following script to:

  1. Add the dependencies to pubspec.yaml
  2. Add the @CopyWith() to each class in the models directory
  3. Rerun the build_runner
# Annotate every class with `CopyWith`
find lib/src/model -type f -name '*.dart' | while read -r file; do
# Insert import if missing
grep -q "copy_with_extension" "$file" || sed -i '' "/package:json_annotation/a\\
import 'package:copy_with_extension/copy_with_extension.dart';
" "$file"

# Insert @CopyWith() if missing
grep -q "@CopyWith" "$file" || sed -i '' "/@JsonSerializable/i\\
@CopyWith()
" "$file"
done

Additional context

The default 'built_value' already provide a safe way to maintain immutability and this other implementation should atleast match in terms of it

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the dart-dio generator's json_serializable model generation and the build_runner setup described in the issue, then inspect how generated model part files and pubspec dependencies are handled. Done means generated models can use copy_with_extension alongside json_serializable, producing copyWith methods while preserving immutable model behavior; add coverage for the generated output.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.