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

A lot of operators don't check they actually have a Scannable for scanUnsafe(Attr<Scannable>) #895

Closed
simonbasle opened this issue Oct 6, 2017 · 1 comment
Assignees
Labels
type/bug A general bug type/enhancement A general enhancement
Milestone

Comments

@simonbasle
Copy link
Member

Expected behavior

All scanUnsafe implementations should return a Scannable when called with PARENT or ACTUAL.

Actual behavior

A lot of operators don't necessarily check that this is the case. They will happily return e.g. a CoreSubscriber, which happens to have mostly Scannable implementations (including the ones used in tests). But if you create an operator with an actual bare CoreSubsriber, scanning it for ACTUAL will fail with a ClassCastException.

Steps to reproduce

@Test
public void scanFail() {
    Scannable scannable = new FluxReplay<>(subscriber -> { }, 2, 2, null);
    Scannable parent = scannable.scan(Scannable.Attr.PARENT); // <-- ClassCastException

    assertThat(parent).isNotNull();
    assertThat(parent.isScanAvailable()).isFalse();
}

Reactor Core version

3.1.0.RELEASE

@simonbasle simonbasle added the type/bug A general bug label Oct 6, 2017
@simonbasle simonbasle added this to the 3.1.1.RELEASE milestone Oct 6, 2017
@simonbasle simonbasle self-assigned this Oct 6, 2017
@simonbasle simonbasle added the type/enhancement A general enhancement label Oct 6, 2017
@simonbasle
Copy link
Member Author

Backward-compatible solution design
We could work around the issue without having to go through the whole lot of operators, and provide better future-proof safety, by using Scannable.from.

When encountering these Attr<Scannable>, Scannable#scan could perform a safe conversion of the element from scanUnsafe with Scannable#from.

This can be generalized in Attr by adding a tryConvert(Object) method to the attributes. By default it would just cast as before, but scan would delegate that behavior to the Attr. For attributes that have a better strategy (like applying Scannable#from and getting a Null Object), said strategy could be defined at the attribute level.

In order to still be able to get the raw value from operators for these cases, a new RawAttr extends Attr<Object> could be introduced. They would wrap a typed Attr original and Scannable#scan would detect these and directly return the result of scanUnsafe(original).

simonbasle added a commit that referenced this issue Oct 6, 2017
This commit makes scanning of ACTUAL and PARENT safer by introducing a
safe converter in the Attr class. By default, no converter is defined
and the old behavior of force-casting is still used.

For the two attributes above however, the value from `scanUnsafe` goes
through `Scannable#from` first when calling `scan` or `scanOrDefaul`.
That way, even though a lot of operators blindly return something that
is Scannable _in most case but not necessarily_ (e.g. a Publisher),
the outer scan will not fail.

Added companion `RawAttr` for ACTUAL and PARENT which are `Attr<Object>`
always returning the raw value from the operator (downcasted to Object).
simonbasle added a commit that referenced this issue Oct 17, 2017
This commit makes scanning of ACTUAL and PARENT safer by introducing a
safe converter in the Attr class. By default, no converter is defined
and the old behavior of force-casting is still used.

For the two attributes above however, the value from `scanUnsafe` goes
through `Scannable#from` first when calling `scan` or `scanOrDefaul`.
That way, even though a lot of operators blindly return something that
is Scannable _in most case but not necessarily_ (e.g. a Publisher),
the outer scan will not fail.

Added companion `RawAttr` for ACTUAL and PARENT which are `Attr<Object>`
always returning the raw value from the operator (downcasted to Object).
simonbasle added a commit that referenced this issue Oct 17, 2017
This commit makes scanning of ACTUAL and PARENT safer by introducing a
safe converter in the Attr class. By default, no converter is defined
and the old behavior of force-casting is still used.

For the two attributes above however, the value from `scanUnsafe` goes
through `Scannable#from` first when calling `scan` or `scanOrDefaul`.
That way, even though a lot of operators blindly return something that
is Scannable _in most case but not necessarily_ (e.g. a Publisher),
the outer scan will not fail.

Added companion `RawAttr` for ACTUAL and PARENT which are `Attr<Object>`
always returning the raw value from the operator (downcasted to Object).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug A general bug type/enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant