|
| 1 | +package io.github.simbo1905.json.schema; |
| 2 | + |
| 3 | +import jdk.sandbox.java.util.json.Json; |
| 4 | +import jdk.sandbox.java.util.json.JsonValue; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.net.URI; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.Set; |
| 11 | + |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 14 | + |
| 15 | +/// Compile-only posture: deny all remote fetches to reveal which fragments |
| 16 | +/// compile locally. This is a unit-level gate prior to the full OpenRPC IT. |
| 17 | +class OpenRPCCompileOnlyTest extends JsonSchemaLoggingConfig { |
| 18 | + |
| 19 | + @Test |
| 20 | + void compile_local_fragment_succeeds_with_remote_denied() { |
| 21 | + final var fragment = "{" + |
| 22 | + "\"$defs\":{\"X\":{\"type\":\"integer\"}}," + |
| 23 | + "\"$ref\":\"#/$defs/X\"" + |
| 24 | + "}"; |
| 25 | + |
| 26 | + final var fetcher = new MapRemoteFetcher(Map.of()); |
| 27 | + final var policy = JsonSchema.FetchPolicy.defaults().withAllowedSchemes(Set.of("file")); |
| 28 | + final var options = JsonSchema.CompileOptions.remoteDefaults(fetcher).withFetchPolicy(policy); |
| 29 | + |
| 30 | + final var schema = JsonSchema.compile(Json.parse(fragment), JsonSchema.Options.DEFAULT, options); |
| 31 | + assertThat(schema.validate(Json.parse("1")).valid()).isTrue(); |
| 32 | + assertThat(schema.validate(Json.parse("\"x\""))).extracting("valid").isEqualTo(false); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void compile_remote_ref_is_denied_by_policy() { |
| 37 | + final var fragment = "{" + |
| 38 | + "\"$ref\":\"http://example.com/openrpc.json#/$defs/X\"" + |
| 39 | + "}"; |
| 40 | + |
| 41 | + final var fetcher = new MapRemoteFetcher(Map.of()); |
| 42 | + final var policy = JsonSchema.FetchPolicy.defaults().withAllowedSchemes(Set.of("file")); |
| 43 | + final var options = JsonSchema.CompileOptions.remoteDefaults(fetcher).withFetchPolicy(policy); |
| 44 | + |
| 45 | + assertThatThrownBy(() -> JsonSchema.compile(Json.parse(fragment), JsonSchema.Options.DEFAULT, options)) |
| 46 | + .isInstanceOf(JsonSchema.RemoteResolutionException.class) |
| 47 | + .hasFieldOrPropertyWithValue("reason", JsonSchema.RemoteResolutionException.Reason.POLICY_DENIED) |
| 48 | + .hasMessageContaining("http://example.com/openrpc.json"); |
| 49 | + } |
| 50 | + |
| 51 | + private static final class MapRemoteFetcher implements JsonSchema.RemoteFetcher { |
| 52 | + private final Map<URI, JsonValue> documents; |
| 53 | + private MapRemoteFetcher(Map<URI, JsonValue> documents) { this.documents = Map.copyOf(documents); } |
| 54 | + @Override public FetchResult fetch(URI uri, JsonSchema.FetchPolicy policy) { |
| 55 | + throw new JsonSchema.RemoteResolutionException(uri, |
| 56 | + JsonSchema.RemoteResolutionException.Reason.NOT_FOUND, |
| 57 | + "No remote document registered for " + uri); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments