mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 09:15:27 +00:00
Enforce @MinItems on the options a pack writes
DeclaredBounds checks it with @MinNumber and @MaxNumber, so pack load, validation and every resource decode refuse an option written with fewer items than it declares, naming the option: decorators[1].palette has 0 items; it must have at least 1. An option the pack does not write is the module's default and is not checked.
This commit is contained in:
1 parent
ce7f782561
commit
67d5128942
2 files changed
+27
-5
No files matched your search
@@ -25,6 +25,7 @@ import com.google.gson.JsonParseException;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.volmit.config.JsonForms;
|
||||
import com.volmit.config.MaxNumber;
|
||||
import com.volmit.config.MinItems;
|
||||
import com.volmit.config.MinNumber;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
@@ -35,11 +36,8 @@ import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The bounds IrisApi's {@link MinNumber} and {@link MaxNumber} declare, enforced on decoded settings. Only options the
|
||||
* JSON writes are checked: a default is the module's own choice.
|
||||
* <p>
|
||||
* {@code @MinItems} is not enforced yet: most options carrying {@code @MinItems(1)} default to an empty list, and
|
||||
* packs write {@code []} for them (OPEN_QUESTIONS 57).
|
||||
* The bounds IrisApi's {@link MinNumber}, {@link MaxNumber} and {@link MinItems} declare, enforced on decoded settings.
|
||||
* Only options the JSON writes are checked: a default is the module's own choice.
|
||||
*/
|
||||
final class DeclaredBounds {
|
||||
private static final List<String> LIBRARY_PACKAGES = List.of("java.", "javax.", "com.google.gson.");
|
||||
@@ -86,6 +84,18 @@ final class DeclaredBounds {
|
||||
if (maximum != null && value instanceof Number number && number.doubleValue() > maximum.value()) {
|
||||
throw new JsonParseException(path + " is " + number + "; it must be at most " + label(maximum.value()));
|
||||
}
|
||||
requireMinimumItems(field, value, path);
|
||||
}
|
||||
|
||||
private static void requireMinimumItems(Field field, Object value, String path) {
|
||||
MinItems minimumItems = field.getAnnotation(MinItems.class);
|
||||
if (minimumItems == null || value == null) {
|
||||
return;
|
||||
}
|
||||
int count = itemsOf(value).size();
|
||||
if (count < minimumItems.value()) {
|
||||
throw new JsonParseException(path + " has " + count + " items; it must have at least " + minimumItems.value());
|
||||
}
|
||||
}
|
||||
|
||||
private static void requireNested(Object value, JsonElement written, String path) {
|
||||
|
||||
@@ -151,6 +151,18 @@ public class DecorantSettingsDecodingTest {
|
||||
+ " not an object of options", refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aPaletteNeedsAnEntryButAnEmptyListOfNoneIsFine() {
|
||||
List<Decorator> none = decorators("{\"decorators\": [{\"whitelist\": [], \"blacklist\": [], \"topPalette\": []}]}");
|
||||
assertEquals(List.of(), none.getFirst().blacklist());
|
||||
|
||||
IllegalArgumentException refused = assertThrows(IllegalArgumentException.class,
|
||||
() -> decorators("{\"decorators\": [{\"chance\": 1}, {\"palette\": []}]}"));
|
||||
|
||||
assertEquals("Cannot read settings iris:decorant under \"decorators\" in biome meadow:"
|
||||
+ " decorators[1].palette has 0 items; it must have at least 1", refused.getMessage());
|
||||
}
|
||||
|
||||
private List<Decorator> decorators(String biomeJson) {
|
||||
return decoder.decode(Level.BIOME, new JSONObject(biomeJson), "biome meadow")
|
||||
.find(DecorantSettings.class).orElseThrow().decorators();
|
||||
|
||||
Reference in new issue
Block a user