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

bugfix: Example json Payload generated as plain value but should be array #486

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Yauhenikapl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.datatype.DatatypeFactory;
Expand Down Expand Up @@ -222,8 +223,16 @@ private Map<String, Object> transformCollectionProperty( final BasicProperty pro
final List<Object> collectionValues = getCollectionValues( property, (Collection) characteristic );
return toMap( property.getName(), collectionValues );
} else if ( isConstrainedCollection( characteristic ) ) {
return toMap( property.getName(), getCollectionValues( property, characteristic.as( Trait.class ).getBaseCharacteristic().as( Collection.class ),
(LengthConstraint) characteristic.as( Trait.class ).getConstraints().get( 0 ) ) );

final Collection collection = characteristic.as( Trait.class ).getBaseCharacteristic().as( Collection.class );

final List<Constraint> constraints = characteristic.as( Trait.class ).getConstraints().stream().filter( trait -> trait.is( LengthConstraint.class ) )
.toList();

return !constraints.isEmpty() ?
toMap( property.getName(), getCollectionValues( property, collection,
(LengthConstraint) characteristic.as( Trait.class ).getConstraints().get( 0 ) ) ) :
Yauhenikapl marked this conversation as resolved.
Show resolved Hide resolved
toMap( property.getName(), getCollectionValues( property, collection ) );
}
return ImmutableMap.of();
}
Expand All @@ -233,8 +242,7 @@ private boolean isConstrainedCollection( final Characteristic characteristic ) {
return false;
}
final Trait trait = characteristic.as( Trait.class );
return trait.getBaseCharacteristic().is( Collection.class ) && (trait.getConstraints().size() == 1) &&
trait.getConstraints().get( 0 ).is( LengthConstraint.class );
return trait.getBaseCharacteristic().is( Collection.class ) && (!trait.getConstraints().isEmpty());
}

private Map<String, Object> transformAbstractEntityProperty( final BasicProperty property, final boolean useModelExampleValue ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -150,6 +147,8 @@ public void testGenerateJsonForAspectWithCollectionOfEntities( final KnownVersio
assertThat( aspectWithEntityCollection.getTestList() ).hasSize( 1 );

final TestEntityWithSimpleTypes testEntityWithSimpleTypes = aspectWithEntityCollection.getTestList().get( 0 );

assertThat( generatedJson ).contains( "[" ).contains( "]" );
assertTestEntityWithSimpleTypes( testEntityWithSimpleTypes );
}

Expand Down Expand Up @@ -235,6 +234,7 @@ public void testGenerateJsonForAspectWithMultipleCollectionsOfSimpleType( final
final AspectWithMultipleCollectionsOfSimpleType aspectWithCollectionOfSimpleType = parseJson( generatedJson,
AspectWithMultipleCollectionsOfSimpleType.class );

assertThat( generatedJson ).contains( "[" ).contains( "]" );
assertThat( aspectWithCollectionOfSimpleType.getTestListInt() ).containsExactly( 35 );
assertThat( aspectWithCollectionOfSimpleType.getTestListString() ).containsExactly( "test string" );
}
Expand All @@ -246,6 +246,7 @@ public void testGenerateJsonForAspectWithCollectionOfSimpleType( final KnownVers

final AspectWithCollectionOfSimpleType aspectWithCollectionOfSimpleType = parseJson( generatedJson, AspectWithCollectionOfSimpleType.class );

assertThat( generatedJson ).contains( "[" ).contains( "]" );
assertThat( aspectWithCollectionOfSimpleType.getTestList() ).containsExactly( 35 );
}

Expand Down Expand Up @@ -303,6 +304,7 @@ public void testGenerateJsonForAspectWithComplextEntityCollectionEnum( final Kno
final List<AspectWithComplexEntityCollectionEnum.MyEntityTwo> myEntityTwoList = aspectWithComplexEntityCollectionEnum
.getMyPropertyOne().getValue()
.getEntityPropertyOne();
assertThat( generatedJson ).contains( "[" ).contains( "]" );
assertThat( myEntityTwoList ).hasSize( 1 );
assertThat( myEntityTwoList.get( 0 ).getEntityPropertyTwo() ).isEqualTo( "foo" );
}
Expand Down Expand Up @@ -584,6 +586,8 @@ public void testGenerateJsonForAspectWithCollectionWithAbstractEntity( final Kno
final Collection<AbstractTestEntity> testProperty = aspectWithCollectionWithAbstractEntity.getTestProperty();
assertThat( testProperty ).isNotEmpty();
final ExtendingTestEntity extendingTestEntity = (ExtendingTestEntity) testProperty.iterator().next();

assertThat( generatedJson ).contains( "[" ).contains( "]" );
assertThat( extendingTestEntity.getAbstractTestProperty() ).isNotNull();
assertThat( extendingTestEntity.getEntityProperty() ).isNotBlank();
}
Expand All @@ -604,6 +608,7 @@ public void testGenerateJsonForAspectWithAbstractSingleEntity( final KnownVersio
void testGenerateJsonForAspectWithConstrainedSetProperty( final KnownVersion metaModelVersion ) throws IOException {
final String generatedJson = generateJsonForModel( TestAspect.ASPECT_WITH_CONSTRAINED_SET, metaModelVersion );
final AspectWithConstrainedSet aspectWithConstrainedSet = parseJson( generatedJson, AspectWithConstrainedSet.class );
assertThat( generatedJson ).contains( "[" ).contains( "]" );
assertThat( aspectWithConstrainedSet.getTestProperty() ).hasSizeGreaterThan( 0 );
}

Expand Down