Regression in 5.2: binary records with covariant interface accessors expose bridge types as bean properties
Issue description
After upgrading Micronaut Core from 5.1.x to 5.2.x, Micronaut Data fails to process an entity record when that record is already compiled and present on the annotation processor classpath.
The record component has a concrete type annotated with @Embeddable, but the
record also implements an interface that declares the accessor with a covariant
supertype. Java emits a synthetic bridge accessor for that interface method.
With Core 5.2, ClassElement.getBeanProperties() appears to expose the bridge
method's interface type instead of the concrete record-component type.
Micronaut Data then rejects the property because it sees ResourceView, which
is not annotated with @Embeddable, rather than ResourceDetails, which is.
This worked with Micronaut Core 5.1.x. It fails after moving to 5.2.x (observed with a build based on Core 5.2.4 and Micronaut Data 5.1.4).
Expected behaviour
For a binary/classpath record, the resource bean property should retain the
record component's declared type, ResourceDetails. The repository should
compile successfully.
Actual behaviour
Micronaut Data sees ResourceView for resource and compilation fails with:
io.micronaut.data.exceptions.MappingException: Type [example.BinaryRecordEntity$ResourceView]
of property [resource] of entity [example.BinaryRecordEntity] is missing
@Embeddable annotation. @Embedded fields can only be applied to types annotated
with @EmbeddableMinimal reproducer
Use two Gradle/Maven modules. Compile model first, then place it on the
repository module's compile and annotation-processor classpaths. The separate
modules are important: the failure occurs when the entity is read as a compiled
classpath type, not when source is processed in the same compilation round.
model/src/main/java/example/BinaryRecordEntity.java:
package example;
import io.micronaut.data.annotation.Embeddable;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
import jakarta.persistence.Embedded;
@MappedEntity
public record BinaryRecordEntity(
@Id String id,
@Embedded ResourceDetails resource) implements Identified<String>, HasResource {
public interface ResourceView {
}
@Embeddable
public record ResourceDetails(String value) implements ResourceView {
}
}
interface Identified<I> {
I id();
}
interface HasResource {
BinaryRecordEntity.ResourceView resource();
}repository/src/main/java/example/BinaryRecordRepository.java:
package example;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
@Repository
interface BinaryRecordRepository extends CrudRepository<BinaryRecordEntity, String> {
}Compile the repository module with micronaut-data-processor enabled. Core
5.1.x accepts the repository; Core 5.2.x produces the exception above.
Bytecode evidence
javap -p -v BinaryRecordEntity.class shows both accessors:
public example.BinaryRecordEntity$ResourceDetails resource();
flags: ACC_PUBLIC
public example.BinaryRecordEntity$ResourceView resource();
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETICThe id accessor has the same shape (String id() plus synthetic Object id()), so this can also corrupt identifier-type metadata.
Why this appears to be a Core issue
Micronaut Data consumes ClassElement.getBeanProperties() when constructing a
SourcePersistentEntity; it then validates the type supplied for an
@Embedded property. The incorrect property type is already present before
Data applies its mapping rules. The likely fix is in Micronaut Core's Java
classpath/binary-record property resolution: it should prefer the declared
record component/concrete accessor and ignore synthetic bridge accessors when
deriving a property type.
Workaround
There is no safe application-level workaround that preserves correct metadata.
Annotating the interface with @Embeddable only suppresses the initial error;
the property remains typed as the interface and nested concrete properties are
then lost. Processing the entity from source avoids the failure, but does not
fix processing it as a normal dependency binary.
Source: micronaut-projects/micronaut-core