Skip to content

Commit 3e14db8

Browse files
committed
HBX-2996 Create valid json objects for circular relationships
1 parent fbbf90c commit 3e14db8

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

language/src/main/java/org/hibernate/tool/language/internal/JsonHelper.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,13 @@ private static void entityToString(
183183
JsonAppender appender) {
184184
final EntityIdentifierMapping identifierMapping = entityType.getIdentifierMapping();
185185
appender.trackingEntity( value, entityType, shouldProcessEntity -> {
186+
appender.append( "{\"" ).append( identifierMapping.getAttributeName() ).append( "\":" );
187+
entityIdentifierToString( value, identifierMapping, options, appender );
186188
if ( shouldProcessEntity ) {
187-
appender.append( "{\"" ).append( identifierMapping.getAttributeName() ).append( "\":" );
188-
entityIdentifierToString( value, identifierMapping, options, appender );
189+
// if it wasn't already encountered, append all properties
189190
managedTypeToString( value, entityType, options, appender, ',' );
190-
appender.append( '}' );
191-
}
192-
else {
193-
// if it was already encountered, only append the identity string
194-
appender.append( '\"' ).append( entityType.getEntityName() ).append( '#' );
195-
entityIdentifierToString( value, identifierMapping, options, appender );
196-
appender.append( '\"' );
197191
}
192+
appender.append( '}' );
198193
} );
199194
}
200195

language/src/test/java/org/hibernate/tool/language/ResultsJsonSerializerTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ public void testCompanyFetchEmployees(SessionFactoryScope scope) {
248248
employees.forEach( employee -> {
249249
assertDoesNotThrow( () -> UUID.fromString( employee.get( "uniqueIdentifier" ).asText() ) );
250250
assertThat( employee.get( "firstName" ).textValue() ).startsWith( "Ma" );
251-
assertThat( employee.get( "company" ).textValue() ).isEqualTo( Company.class.getName() + "#1" );
251+
final JsonNode company = employee.get( "company" );
252+
assertThat( company.get( "id" ).intValue() ).isEqualTo( 1 );
253+
assertThat( company.properties().stream().map( Map.Entry::getKey ) )
254+
.containsOnly( "id" ); // circular relationship
252255
} );
253256
}
254257
catch (JsonProcessingException e) {
@@ -348,8 +351,10 @@ public void testComplexInheritance(SessionFactoryScope scope) {
348351
assertThat( cat.isObject() ).isTrue();
349352
assertThat( cat.get( "id" ).intValue() ).isEqualTo( 2 );
350353
assertThat( cat.get( "description" ).textValue() ).isEqualTo( "Gatta" );
351-
assertThat( cat.get( "owner" ).isTextual() ).isTrue(); // circular relationship
352-
assertThat( cat.get( "owner" ).textValue() ).isEqualTo( Human.class.getName() + "#1" );
354+
final JsonNode owner = cat.get( "owner" );
355+
assertThat( owner.get( "id" ).intValue() ).isEqualTo( 1 );
356+
assertThat( owner.properties().stream().map( Map.Entry::getKey ) )
357+
.containsOnly( "id" ); // circular relationship
353358

354359
final JsonNode nickNames = jsonNode.get( "nickNames" );
355360
assertThat( nickNames.isArray() ).isTrue();

0 commit comments

Comments
 (0)