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

Improved resolution for object array input values #246

Merged
merged 4 commits into from
Oct 19, 2022
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
2 changes: 1 addition & 1 deletion tcases-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<dependency>
<groupId>io.swagger.parser.v3</groupId>
<artifactId>swagger-parser</artifactId>
<version>2.1.3</version>
<version>2.1.5</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,34 +193,45 @@ protected DataValue<List<DataValue<T>>> dataValueOf( List<DataValue<T>> value)
private List<DataValue<T>> newArray( ResolverContext context)
{
List<DataValue<T>> items = new ArrayList<DataValue<T>>();
boolean itemsUnique = isItemsUnique();
DataValue<T> nextItem;

// Select array items, observing any uniqueness constraint
ValueDomain<T> nextItemValues = getItemValues();
ValueDomain<T> otherItemValues = Optional.ofNullable( getOtherItemValues()).orElse( nextItemValues);
for( int itemCount = getItemCount().selectValue( context);

items.size() < itemCount;

items.add( nextItem),
nextItemValues = otherItemValues)
int itemCount = getItemCount().selectValue( context);
if( itemCount > 0)
{
nextItem = getNextItem( context, nextItemValues, itemCount, items, itemsUnique);
}
// Select array items, observing any uniqueness constraint
ValueDomain<T> nextItemValues = getItemValues();
ValueDomain<T> otherItemValues = Optional.ofNullable( getOtherItemValues()).orElse( nextItemValues);

boolean itemsUnique =
Optional.of( isItemsUnique())
.filter( unique -> unique)
.map( unique -> {
boolean otherItemsNull = Optional.ofNullable( otherItemValues.getType()).map( type -> type == Type.NULL).orElse( false);
if( otherItemsNull)
{
context.warn( "Unable to generate unique non-null item objects -- all array items must be null");
}
return !otherItemsNull;
})
.orElse( false);

if( !itemsUnique && items.size() > 1)
{
// The first item, which satisifies required item assertions, must remain unchanged.
// But given a different random target item ...
int target = context.getRandom().nextInt( items.size() - 1) + 1;
while( items.size() < itemCount)
{
items.add( getNextItem( context, nextItemValues, itemCount, items, itemsUnique));
nextItemValues = otherItemValues;
}

if( !itemsUnique && items.size() > 1)
{
// The first item, which satisfies required item assertions, must remain unchanged.
// But given a different random target item ...
int target = context.getRandom().nextInt( items.size() - 1) + 1;

// ...and a different source item...
int source;
while( (source = context.getRandom().nextInt( items.size())) == target);
// ...and a different source item...
int source;
while( (source = context.getRandom().nextInt( items.size())) == target);

// ...ensure target item is a duplicate of the source item.
items.set( target, items.get( source));
// ...ensure target item is a duplicate of the source item.
items.set( target, items.get( source));
}
}

return items;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,6 @@ public static ValueDomain<?> toItemsDomain( Map<String,Object> propertyValues, C
types[0] == Type.NUMBER?
toNumberItemsDomain( Type.NUMBER, valueProperties) :

types[0] == Type.OBJECT?
new ObjectDomain( chars) :

types[0] == Type.STRING?
toStringItemsDomain( valueProperties, chars) :

Expand Down Expand Up @@ -440,21 +437,10 @@ public static ValueDomain<?> toArrayDomain( Map<String,Object> propertyValues, C
}
else
{
Map<String,Object> itemValueProperties = expectPropertyValues( items, "Contains");
ValueDomain<?> otherItemValues = toItemsDomain( itemValueProperties, chars);
ValueDomain<?> itemValues = toValueDomain( itemValueProperties, chars);
ValueDomain<?> itemDomain = itemValues == null? otherItemValues : itemValues;
arrayDomain = itemDomain.arrayOf();
arrayDomain.setOtherItemValues( otherItemValues);
arrayDomain = toArrayValues( items, chars);

VarBinding size = expectVarBinding( items, "Size");
arrayDomain.setItemCount( Range.of( size));

arrayDomain.setItemsUnique(
Optional.ofNullable( getVarBinding( items, "Unique"))
.filter( u -> !u.isValueNA())
.map( u -> "Yes".equals( u.getValue()))
.orElse( size.getAnnotation( "itemsUnique") != null));
}

domain = arrayDomain;
Expand All @@ -463,6 +449,37 @@ public static ValueDomain<?> toArrayDomain( Map<String,Object> propertyValues, C
return domain;
}

/**
* Returns the array domain specified by the given properties.
*/
private static ArrayDomain<?> toArrayValues( Map<String,Object> items, Characters chars)
{
Map<String,Object> itemValueProperties = expectPropertyValues( items, "Contains");
ValueDomain<?> otherItemValues = toItemsDomain( itemValueProperties, chars);
ValueDomain<?> itemValues = toValueDomain( itemValueProperties, chars);

ValueDomain<?> itemDomain =
itemValues != null?
itemValues :

otherItemValues != null?
otherItemValues :

new NullDomain();

ArrayDomain<?> arrayDomain = itemDomain.arrayOf();
arrayDomain.setOtherItemValues( otherItemValues == null? itemDomain : otherItemValues);

VarBinding size = expectVarBinding( items, "Size");
arrayDomain.setItemsUnique(
Optional.ofNullable( getVarBinding( items, "Unique"))
.filter( u -> !u.isValueNA())
.map( u -> "Yes".equals( u.getValue()))
.orElse( size.getAnnotation( "itemsUnique") != null));

return arrayDomain;
}

/**
* Returns the boolean domain specified by the given properties.
*/
Expand Down Expand Up @@ -791,11 +808,7 @@ public static ValueDomain<?> toArrayItemsDomain( Map<String,Object> propertyValu
}
else
{
Map<String,Object> itemValueProperties = expectPropertyValues( items, "Contains");
ValueDomain<?> otherItemValues = toItemsDomain( itemValueProperties, chars);
ValueDomain<?> itemValues = toValueDomain( itemValueProperties, chars);
ValueDomain<?> itemDomain = itemValues == null? otherItemValues : itemValues;
domain = itemDomain.arrayOf();
domain = toArrayValues( items, chars);

VarBinding size = expectVarBinding( items, "Size");
domain.setItemCount(
Expand Down
Loading