Skip to content

Feature Request "Expected" Annotation #1490

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
YukiAttano opened this issue Apr 24, 2025 · 0 comments
Open

Feature Request "Expected" Annotation #1490

YukiAttano opened this issue Apr 24, 2025 · 0 comments

Comments

@YukiAttano
Copy link

I would like to throw in a feature to discuss.

An annotation like JsonKey(expected: true) or @Expected/@optional that is purely for printing warnings if a member is missing while using .fromJson().

The idea comes from the scenario using JsonSerializable for generating data classes as REST communication objects.

An example of an user object:

{
  "id": 0,
  "firstName": "String",
  "lastName": "String",
  "age": 40,
  "address": {}
}

I would implement this class like:

@JsonSerializable()
class User {
  final int id;
  final String firstName;
  final String lastName;
  final int? age;
  final Address? address; 
  
  const User({
    required this.id,
    this.firstName = "",
    this.lastName = "",
    this.age,
    this.address,
  })
}

All fields that are not crucial for operation of the app are not "required" to allow the app to run with as minimal information necessary. But i would always "expect" to have a first name and last name from my backend.
As long as i am in a developer environment, i would like to get informed if one of my expected fields are missing.

The example may not be the best, but you'll get the idea.

I've experienced in my projects that sometimes field names tend to be misspelled (either in frontend or backend but as they could be optional this is often overlooked during implementation), or in bigger classes data might be "optional" in a release environment that is always "expected" in a dev environment.

I do understand that this is better be caught and tested in test files, but the real world often misses the time to write those, thats why i came up with this idea.

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

No branches or pull requests

1 participant