Skip to content
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

How to actually resolve the copyWith deprecation warnings? #503

Closed
camerow opened this issue Apr 20, 2021 · 4 comments · Fixed by #907
Closed

How to actually resolve the copyWith deprecation warnings? #503

camerow opened this issue Apr 20, 2021 · 4 comments · Fixed by #907

Comments

@camerow
Copy link

camerow commented Apr 20, 2021

Heeding the warning from my linter, I've upgraded to using libprotoc 3.15.8 and protoc_plugin 20.0.0 to generate my foo.pb.dart files in the hopes that copyWith will no longer be used in my generated files and I can future proof my application, as well as hopefully reduce bundle size.

However generating my files simply adds deprecation annotations, but doesn't use the recommended method GeneratedMessageGenericExtensions.deepCopy. Am I missing something obvious to fix this deprecated method usage? AFAIK the plugin should be doing this for me when I generate these files.

My current Dart/Flutter:

Flutter 2.0.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision adc687823a (4 days ago) • 2021-04-16 09:40:20 -0700
Engine • revision b09f014e96
Tools • Dart 2.12.3

Thanks in advance for your guidance! I must add that up until now this has been working amazingly and I'm super thankful for this package.

@yang-lile
Copy link

yang-lile commented May 10, 2021

I can't found any good way to fix update. So I only copy my test code to you for reference.

void main(List<String> args) {
  var ruler = StaticDataPool.rulers[0];
  var any = Any(value: ruler.data.value, typeUrl: ruler.data.typeUrl);
  print(any.canUnpackInto(RulerId.getDefault()));
  var unpackInto = any.unpackInto(RulerId());

  // older copy
  var copyWith = unpackInto.clone();
  copyWith = unpackInto.copyWith((a) {
    a.ruleName = '.scdae';
  });

  // new copy
  var newData =
      GeneratedMessageGenericExtensions<RulerId>(unpackInto).deepCopy();
  newData = GeneratedMessageGenericExtensions<RulerId>(unpackInto)
      .rebuild((unpackInto) {
    unpackInto.ruleName = 'cdsce';
  });
}

@osa1
Copy link
Member

osa1 commented May 9, 2022

However generating my files simply adds deprecation annotations, but doesn't use the recommended method GeneratedMessageGenericExtensions.deepCopy

I'm guessing you meant rebuild not deepCopy, as that's what the deprecation message suggests.

I don't know why generated copyWith methods use the deprecated GeneratedMessage.copyWith, but the commit that deprecated GeneratedMessage.copyWith (6ed458c) also started generating // ignore: deprecated_member_uses next to uses of this method in generated code. So unless you're using copyWith in your code you shouldn't get any warnings.

Are you sure you're not using copyWith in your code?

@MuZhou233
Copy link

I recently facing to the same problem and confused about the warning. After a deep dive, I believe the point is the document and/or warning message didn't mention I need to import protobuf to use that extention (see https://github.com/google/protobuf.dart/pull/317/files#r848295229). protobuf is even not an dependency in my project before (I generate code in another place).

One major gotcha is that if you import your proto files with a prefix these
methods will not appear on your instances.
Alternatively you can add

import 'package:protobuf/protobuf.dart' show GeneratedMessageGenericExtensions;

@osa1 sorry for ping. I think it can helps a lot if you could provide more guidance here/or somewhere else.

@osa1
Copy link
Member

osa1 commented Dec 23, 2023

@MuZhou233 I think we should export the extension in the generated files. I've implemented this in #907.

The PR (and a release) may have to wait until the next year as some of the devs are on vacation, and we may also want to finish and include #905 in the release as well.

@osa1 osa1 closed this as completed in #907 Jan 6, 2024
osa1 added a commit that referenced this issue Jan 6, 2024
Currently when a user calls deprecated methods `clone` or `copyWith`,
the deprecation message points to `GeneratedMessageGenericExtensions`
`deepCopy` and `rebuild` methods.

However we can't just replace `clone` with `deepCopy` as currently the
extension is not exported by the generated files. Instead we need to
import the library explicitly in the use site.

We could mention this in the deprecation message ("Use rebuild from
protobuf library instead"), but it's more convenient to just export the
extension in the generated message files.

Closes #503.
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 a pull request may close this issue.

4 participants