public static void main(final String... args) throws IOException, ProcessingException { final JsonNode good = Utils.loadResource("/fstab-good.json"); final JsonNode bad = Utils.loadResource("/fstab-bad.json"); final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json"); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foobar", CustomDownloader.getInstance()).freeze(); final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(cfg).freeze(); final JsonSchema schema = factory.getJsonSchema("foobar:/fstab.json#"); ProcessingReport report; report = schema.validate(good); System.out.println(report); report = schema.validate(bad); System.out.println(report); report = schema.validate(bad2); System.out.println(report); }
public static void main(final String... args) throws IOException, ProcessingException { final JsonNode fstabSchema = Utils.loadResource("/fstab-inline.json"); final JsonNode good = Utils.loadResource("/fstab-good.json"); final JsonNode bad = Utils.loadResource("/fstab-bad.json"); final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json"); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .dereferencing(Dereferencing.INLINE).freeze(); final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(cfg).freeze(); final JsonSchema schema = factory.getJsonSchema(fstabSchema); ProcessingReport report; report = schema.validate(good); System.out.println(report); report = schema.validate(bad); System.out.println(report); report = schema.validate(bad2); System.out.println(report); } }
public static void main(final String... args) throws IOException, ProcessingException { final JsonNode good = Utils.loadResource("/fstab-good.json"); final JsonNode bad = Utils.loadResource("/fstab-bad.json"); final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json"); final URITranslatorConfiguration translatorCfg = URITranslatorConfiguration.newBuilder() .addSchemaRedirect(FROM, TO).freeze(); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .setURITranslatorConfiguration(translatorCfg).freeze(); final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(cfg).freeze(); final JsonSchema schema = factory.getJsonSchema(FROM); ProcessingReport report; report = schema.validate(good); System.out.println(report); report = schema.validate(bad); System.out.println(report); report = schema.validate(bad2); System.out.println(report); } }
public static void main(final String... args) throws IOException, ProcessingException { final JsonNode good = Utils.loadResource("/fstab-good.json"); final JsonNode bad = Utils.loadResource("/fstab-bad.json"); final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json"); final URITranslatorConfiguration translatorCfg = URITranslatorConfiguration.newBuilder() .setNamespace(NAMESPACE).freeze(); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .setURITranslatorConfiguration(translatorCfg).freeze(); final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(cfg).freeze(); final JsonSchema schema = factory.getJsonSchema("fstab.json"); ProcessingReport report; report = schema.validate(good); System.out.println(report); report = schema.validate(bad); System.out.println(report); report = schema.validate(bad2); System.out.println(report); } }
= LoadingConfiguration.newBuilder();
Main(final String fakeRoot) throws IOException { final URITranslatorConfigurationBuilder builder = URITranslatorConfiguration.newBuilder() .setNamespace(getCwd()); if (fakeRoot != null) builder.addPathRedirect(fakeRoot, getCwd()); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .setURITranslatorConfiguration(builder.freeze()).freeze(); factory = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(cfg).freeze(); syntaxValidator = factory.getSyntaxValidator(); }
private LoadingConfiguration getLoadingConfiguration(Map<String, JsonNode> preloadSchemas) { LoadingConfigurationBuilder loadingConfigurationBuilder = LoadingConfiguration.newBuilder(); for (String nextSchemaUri : preloadSchemas.keySet()) { loadingConfigurationBuilder.preloadSchema(nextSchemaUri, preloadSchemas.get(nextSchemaUri)); } return loadingConfigurationBuilder.freeze(); }
private synchronized void init() { if (loader != null && factory == null) { final LoadingConfigurationBuilder loadingConfig = LoadingConfiguration.newBuilder(); final String simpleName = loader.getClass().getSimpleName(); loadingConfig.addScheme(simpleName, new LoaderUriDownloader(loader)); loadingConfig.setURITranslatorConfiguration(URITranslatorConfiguration.newBuilder().setNamespace(simpleName + ":///").freeze()); factory = JsonSchemaFactory.newBuilder().setLoadingConfiguration(loadingConfig.freeze()).freeze(); } }
@Test public void URIsAreNormalizedBehindTheScenes() throws ProcessingException { final String location = "http://toto/a/../b"; final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); final SchemaTree tree = loader.get(URI.create(location)); assertEquals(tree.getLoadingRef().toURI(), URI.create("http://toto/b#")); }
@Test public void schemasAreFetchedOnceNotTwice() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader).freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(1)).fetch(uri); }
@Test public void schemasCacheCanBeDisabled() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader) .setCacheSize(0) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(2)).fetch(uri); }
private SwaggerJsonSchemaFactory() { final URITranslatorConfiguration translatorConfiguration = URITranslatorConfiguration.newBuilder() .setNamespace(ROOT_CONTEXT) .addPathRedirect(BASE_URI, ROOT_CONTEXT) .freeze(); final LoadingConfiguration loadingConfiguration = LoadingConfiguration.newBuilder() .setURITranslatorConfiguration(translatorConfiguration) .freeze(); factory = JsonSchemaFactory.newBuilder() .setLoadingConfiguration(loadingConfiguration).freeze(); }
@Test public void schemasCacheCanBeDisabledViaCacheSize() throws ProcessingException, IOException { final URI uri = URI.create("foo:/baz#"); final URIDownloader downloader = spy(new URIDownloader() { @Override public InputStream fetch(final URI source) throws IOException { return new ByteArrayInputStream(BYTES); } }); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", downloader) .setCacheSize(0) .freeze(); final SchemaLoader loader = new SchemaLoader(cfg); loader.get(uri); loader.get(uri); verify(downloader, times(2)).fetch(uri); } }
/* package */ EntityJsonParser(boolean unrestrictedSchemaLoad) { if(unrestrictedSchemaLoad) { factory_ = JsonSchemaFactory.byDefault(); } else { JsonSchemaFactoryBuilder builder = JsonSchemaFactory.newBuilder(); LoadingConfigurationBuilder loadingCfgBuilder = LoadingConfiguration.newBuilder(); loadingCfgBuilder.addScheme("http", EntityJsonURIDownloader.getInstance()); loadingCfgBuilder.addScheme("https", EntityJsonURIDownloader.getInstance()); LoadingConfiguration loadingCfg = loadingCfgBuilder.freeze(); builder.setLoadingConfiguration(loadingCfg); factory_ = builder.freeze(); } }
@Test public void preloadedSchemasAreNotFetchedAgain() throws ProcessingException, IOException { final String location = "http://foo.bar/baz#"; final URI uri = URI.create(location); final URIDownloader mock = mock(URIDownloader.class); final LoadingConfigurationBuilder builder = LoadingConfiguration .newBuilder().addScheme("http", mock) .preloadSchema(location, JacksonUtils.nodeFactory().objectNode()); LoadingConfiguration cfg; SchemaLoader registry; cfg = builder.freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); //even if cache is disabled cfg = builder.setCacheSize(0).freeze(); registry = new SchemaLoader(cfg); registry.get(uri); verify(mock, never()).fetch(uri); }
final LoadingConfigurationBuilder loadingConfigurationBuilder = LoadingConfiguration.newBuilder() .dereferencing(dereferencing == JsonSchemaDereferencing.CANONICAL ? Dereferencing.CANONICAL
@Test public void nonJSONInputShouldBeReportedAsSuch() throws IOException { final URI uri = URI.create("foo://bar"); final InputStream sampleStream = new ByteArrayInputStream("}".getBytes()); when(mock.fetch(any(URI.class))).thenReturn(sampleStream); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", mock).freeze(); final URIManager manager = new URIManager(cfg); try { manager.getContent(uri); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("uriManager.uriNotJson", uri)) .hasTextField("parsingMessage").hasLevel(LogLevel.FATAL) .hasField("uri", uri); } }
@Test public void downloaderProblemsShouldBeReportedAsSuch() throws IOException { final URI uri = URI.create("foo://bar"); final Exception foo = new IOException("foo"); when(mock.fetch(any(URI.class))).thenThrow(foo); final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addScheme("foo", mock).freeze(); final URIManager manager = new URIManager(cfg); try { manager.getContent(uri); } catch (ProcessingException e) { assertMessage(e.getProcessingMessage()) .hasMessage(BUNDLE.printf("uriManager.uriIOError", uri)) .hasField("uri", uri).hasLevel(LogLevel.FATAL) .hasField("exceptionMessage", "foo"); } }
/** * Checks if the specified json fulfills the specified schema. * * @param schemaName The name of the schema. * @param contentObjectTree The json which is analysed. * @return TRUE if the json fulfills the specified schema, FALSE otherwise. * @throws ResourceException if an error occurred. */ private boolean validateSchema(String schemaName, JsonNode contentObjectTree) throws ResourceException { try { URITranslatorConfiguration translatorCfg = URITranslatorConfiguration.newBuilder().setNamespace(NAMESPACE).freeze(); LoadingConfiguration cfg = LoadingConfiguration.newBuilder().setURITranslatorConfiguration(translatorCfg).freeze(); JsonSchemaFactory factory = JsonSchemaFactory.newBuilder().setLoadingConfiguration(cfg).freeze(); JsonSchema schema = factory.getJsonSchema(schemaName); ProcessingReport report = schema.validate(contentObjectTree); return report.isSuccess(); } catch (ProcessingException e) { throw new ResourceException(e); } } }
@Test void managerParsesNonstandardJSON() throws IOException, ProcessingException { // get resource URIs for standard and nonstandard sources final String wellFormed = "resource:/load/standard-source.json"; final URI uri1 = JsonRef.fromString(wellFormed).getLocator(); final String illFormed = "resource:/load/nonstandard-source.json"; final URI uri2 = JsonRef.fromString(illFormed).getLocator(); // get URIManager configured to parse nonstandard sources final LoadingConfiguration cfg = LoadingConfiguration.newBuilder() .addParserFeature(JsonParser.Feature.ALLOW_COMMENTS) .addParserFeature(JsonParser.Feature.ALLOW_SINGLE_QUOTES) .addParserFeature(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES) .freeze(); final URIManager manager = new URIManager(cfg); // load JSON nodes from sources using nonstandard manager final JsonNode node1 = manager.getContent(uri1); final JsonNode node2 = manager.getContent(uri2); // validate correctness of loaded equivalent sources assertTrue(JsonNumEquals.getInstance().equivalent(node1, node2)); } }