-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-17680 Avoid JavaType collisions with special JavaTypes for JSON/XML #11364
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
beikov
wants to merge
1
commit into
hibernate:main
Choose a base branch
from
beikov:HHH-17680
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ate-core/src/test/java/org/hibernate/orm/test/mapping/basic/JsonAndArrayMappingTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.orm.test.mapping.basic; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import org.hibernate.annotations.JdbcTypeCode; | ||
| import org.hibernate.cfg.AvailableSettings; | ||
| import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; | ||
| import org.hibernate.metamodel.spi.MappingMetamodelImplementor; | ||
| import org.hibernate.persister.entity.EntityPersister; | ||
| import org.hibernate.testing.orm.junit.DialectFeatureChecks; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.Jira; | ||
| import org.hibernate.testing.orm.junit.RequiresDialectFeature; | ||
| import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.hibernate.testing.orm.junit.Setting; | ||
| import org.hibernate.type.SqlTypes; | ||
| import org.hibernate.type.descriptor.jdbc.ArrayJdbcType; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
| import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.isA; | ||
|
|
||
| // It's vital that EntityWithJson is listed first to reproduce the bug, | ||
| // the bug being, that JsonJavaType was registered in JavaTypeRegistry under e.g. List<Integer>, | ||
| // which would then wrongly be used for EntityWithArray#listInteger as JavaType | ||
| @DomainModel(annotatedClasses = { JsonAndArrayMappingTests.EntityWithJson.class, JsonAndArrayMappingTests.EntityWithArray.class}) | ||
| @SessionFactory | ||
| @ServiceRegistry(settings = @Setting(name = AvailableSettings.JSON_FORMAT_MAPPER, value = "jackson")) | ||
| @RequiresDialectFeature(feature = DialectFeatureChecks.SupportsTypedArrays.class) | ||
| @Jira("https://hibernate.atlassian.net/browse/HHH-17680") | ||
| public class JsonAndArrayMappingTests { | ||
|
|
||
| @Test | ||
| public void verifyMappings(SessionFactoryScope scope) { | ||
| final MappingMetamodelImplementor mappingMetamodel = scope.getSessionFactory() | ||
| .getRuntimeMetamodels() | ||
| .getMappingMetamodel(); | ||
| final JdbcTypeRegistry jdbcTypeRegistry = mappingMetamodel.getTypeConfiguration().getJdbcTypeRegistry(); | ||
| final JdbcType jsonType = jdbcTypeRegistry.getDescriptor( SqlTypes.JSON ); | ||
| final EntityPersister jsonEntity = mappingMetamodel.findEntityDescriptor( EntityWithJson.class ); | ||
|
|
||
| final BasicAttributeMapping listStringJsonAttribute = (BasicAttributeMapping) jsonEntity.findAttributeMapping( | ||
| "listString" ); | ||
| final BasicAttributeMapping listIntegerJsonAttribute = (BasicAttributeMapping) jsonEntity.findAttributeMapping( | ||
| "listInteger" ); | ||
|
|
||
| assertThat( listStringJsonAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); | ||
| assertThat( listIntegerJsonAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); | ||
|
|
||
| assertThat( listStringJsonAttribute.getJdbcMapping().getJdbcType(), isA( (Class<JdbcType>) jsonType.getClass() ) ); | ||
| assertThat( listIntegerJsonAttribute.getJdbcMapping().getJdbcType(), isA( (Class<JdbcType>) jsonType.getClass() ) ); | ||
|
|
||
| final EntityPersister arrayEntity = mappingMetamodel.findEntityDescriptor( EntityWithArray.class ); | ||
|
|
||
| final BasicAttributeMapping listStringArrayAttribute = (BasicAttributeMapping) arrayEntity.findAttributeMapping( | ||
| "listString" ); | ||
| final BasicAttributeMapping listIntegerArrayAttribute = (BasicAttributeMapping) arrayEntity.findAttributeMapping( | ||
| "listInteger" ); | ||
|
|
||
| assertThat( listStringArrayAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); | ||
| assertThat( listIntegerArrayAttribute.getJavaType().getJavaTypeClass(), equalTo( List.class ) ); | ||
|
|
||
| assertThat( listStringArrayAttribute.getJdbcMapping().getJdbcType(), isA( ArrayJdbcType.class ) ); | ||
| assertThat( listIntegerArrayAttribute.getJdbcMapping().getJdbcType(), isA( ArrayJdbcType.class ) ); | ||
| } | ||
|
|
||
| @Entity(name = "EntityWithJson") | ||
| @Table(name = "EntityWithJson") | ||
| public static class EntityWithJson { | ||
| @Id | ||
| private Integer id; | ||
|
|
||
| @JdbcTypeCode( SqlTypes.JSON ) | ||
| private List<String> listString; | ||
| @JdbcTypeCode( SqlTypes.JSON ) | ||
| private List<Integer> listInteger; | ||
|
|
||
| public EntityWithJson() { | ||
| } | ||
| } | ||
|
|
||
| @Entity(name = "EntityWithArray") | ||
| @Table(name = "EntityWithArray") | ||
| public static class EntityWithArray { | ||
| @Id | ||
| private Integer id; | ||
|
|
||
| @JdbcTypeCode( SqlTypes.ARRAY ) | ||
| private List<String> listString; | ||
| @JdbcTypeCode( SqlTypes.ARRAY ) | ||
| private List<Integer> listInteger; | ||
|
|
||
| public EntityWithArray() { | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Confusing overloading of methods Note