Skip to content

prevent mapper generation from type with generic super bound to type with generic extends bound#3994

Open
hduelme wants to merge 1 commit intomapstruct:mainfrom
hduelme:prevent-mapper-generation-from-super-to-extends-bound
Open

prevent mapper generation from type with generic super bound to type with generic extends bound#3994
hduelme wants to merge 1 commit intomapstruct:mainfrom
hduelme:prevent-mapper-generation-from-super-to-extends-bound

Conversation

@hduelme
Copy link
Contributor

@hduelme hduelme commented Feb 11, 2026

Currently, MapStruct generates code that attempts to implicitly cast a Collection<? super Type> to Type inside the for loop. This is not valid, since the elements of the collection are not guaranteed to be instances of Type, which results in a compilation error.
The same issue applies to Stream.

I changed the behavior so that mappings from Collection<? super Type> to Collection<? extends Type> and from Stream<? super Type> to Stream<? extends Type> now result in PROPERTYMAPPING_MAPPING_NOT_FOUND.

Additionally, I added a test to verify that mapping from Collection<? extends Type> to Collection<? super Type> works correctly. I did not add a corresponding test for Stream, since it currently generates invalid code (it tries to directly assign Collection<? extends Type> to Collection<Type>).

I am unsure what the correct behavior should be in this case, Currently, MapStruct generates code that attempts to implicitly cast a Collection<? super Type> to Type inside the for loop. This is not valid, since the elements of the collection are not guaranteed to be instances of Type, which results in a compilation error.
The same issue applies to Stream.

I changed the behavior so that mappings from Collection<? super Type> to Collection<? extends Type> and from Stream<? super Type> to Stream<? extends Type> now result in PROPERTYMAPPING_MAPPING_NOT_FOUND.

Additionally, I added a test to verify that mapping from Collection<? extends Type> to Collection<? super Type> works correctly. I did not add a corresponding test for Stream, since it currently generates invalid code (it tries to directly assign Collection<? extends Type> to Collection<Type>).

I am unsure what the correct behavior should be in this case, as MapStruct currently doesn't fully supports type bonds:

  • Should a dedicated mapping method be generated?
  • Should a cast be used?
  • Or should this kind of mapping be rejected entirely?

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.

1 participant