Skip to content

Commit c7f5275

Browse files
normalize, ARRAY: When items must be unique, ensure maxItems does not exceed the maximum number of item values.
1 parent d42bd26 commit c7f5275

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

tcases-lib/src/main/java/org/cornutum/tcases/resolve/Schemas.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,27 @@ else if( schema.getEnum() != null)
125125
{
126126
case ARRAY:
127127
{
128+
schema.setItems(
129+
Optional.ofNullable( schema.getItems())
130+
.map( items -> resultFor( "items", () -> normalize( items)))
131+
.orElse( null));
132+
128133
schema.setMaxItems(
129134
Optional.ofNullable( schema.getMaxItems())
130135
.filter( max -> max >= 0)
131136
.orElse( null));
137+
138+
domainSize( schema.getItems())
139+
.filter( maxUnique -> Optional.ofNullable( schema.getUniqueItems()).orElse( false))
140+
.filter( maxUnique -> Optional.ofNullable( schema.getMaxItems()).map( max -> max > maxUnique).orElse( true))
141+
.ifPresent( maxUnique -> {
142+
143+
notifyError(
144+
"maxItems exceeds the number of unique item values",
145+
String.format("Adjusting maxItems to max unique items=%s", maxUnique));
146+
147+
schema.setMaxItems( maxUnique);
148+
});
132149

133150
schema.setMinItems(
134151
Optional.ofNullable( schema.getMinItems())

tcases-lib/src/test/java/org/cornutum/tcases/resolve/NormalizeSchemaTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void whenMinItemsAboveMax()
2929
.minItems( 10)
3030
.maxItems( 4)
3131
.uniqueItems( true)
32+
.items( SchemaBuilder.type( "number").minimum( "0.0").maximum( "1.0").build())
3233
.build();
3334

3435
Schema normalized =
@@ -151,6 +152,65 @@ public void whenMinItemsNegative()
151152
assertConditionsNone();
152153
}
153154

155+
@Test
156+
public void whenMaxItemsNoneInfeasible()
157+
{
158+
// Given...
159+
Schemas schemas = new Schemas( withConditionRecorder());
160+
161+
Schema schema =
162+
SchemaBuilder.type( "array")
163+
.uniqueItems( true)
164+
.items( SchemaBuilder.type( "string").enums( "A", "B", "C").build())
165+
.build();
166+
167+
Schema normalized =
168+
SchemaBuilder.with( schema)
169+
.build();
170+
171+
// When...
172+
normalized = schemas.normalize( normalized);
173+
174+
// Then...
175+
Schema expected =
176+
SchemaBuilder.with( schema)
177+
.maxItems( 3)
178+
.build();
179+
180+
assertThat( "Normalized", normalized, matches( new SchemaMatcher( expected)));
181+
assertErrors( "maxItems exceeds the number of unique item values. Adjusting maxItems to max unique items=3.");
182+
}
183+
184+
@Test
185+
public void whenMaxItemsInfeasible()
186+
{
187+
// Given...
188+
Schemas schemas = new Schemas( withConditionRecorder());
189+
190+
Schema schema =
191+
SchemaBuilder.type( "array")
192+
.uniqueItems( true)
193+
.maxItems( 4)
194+
.items( SchemaBuilder.type( "string").enums( "A", "B", "C").build())
195+
.build();
196+
197+
Schema normalized =
198+
SchemaBuilder.with( schema)
199+
.build();
200+
201+
// When...
202+
normalized = schemas.normalize( normalized);
203+
204+
// Then...
205+
Schema expected =
206+
SchemaBuilder.with( schema)
207+
.maxItems( 3)
208+
.build();
209+
210+
assertThat( "Normalized", normalized, matches( new SchemaMatcher( expected)));
211+
assertErrors( "maxItems exceeds the number of unique item values. Adjusting maxItems to max unique items=3.");
212+
}
213+
154214
@Test
155215
public void whenClassifierConstantEnum()
156216
{

0 commit comments

Comments
 (0)