Skip to content

Commit

Permalink
Merge pull request #486 from bci-oss/bugfix/423-Example-json-Payload-…
Browse files Browse the repository at this point in the history
…generation

bugfix: Example json Payload generated as plain value but should be array
  • Loading branch information
atextor authored Dec 11, 2023
2 parents 475cb18 + 74b1f74 commit 3f8c676
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
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 ) ) ) :
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

0 comments on commit 3f8c676

Please sign in to comment.