JsonSchemaGenerator { public Collection<GeneratedFile> generate() throws IOException { SchemaGeneratorConfigBuilder builder = new SchemaGeneratorConfigBuilder(schemaVersion, OptionPreset.PLAIN_JSON); builder.forTypesInGeneral().withStringFormatResolver(target -> target.getSimpleTypeDescription().equals("Date") ? "date-time" : null); builder.forFields().withIgnoreCheck(JsonSchemaGenerator::isNotUserTaskParam).withCustomDefinitionProvider(this::getInputOutput); SchemaGenerator generator = new SchemaGenerator(builder.build()); ObjectWriter writer = new ObjectMapper().writer(); Collection<GeneratedFile> files = new ArrayList<>(); for (Map.Entry<String, List<Class<?>>> entry : map.entrySet()) { ObjectNode merged = null; for (Class<?> c : entry.getValue()) { ObjectNode read = generator.generateSchema(c); if (merged == null) { merged = read; } else { JsonUtils.merge(read, merged); } } try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { writer.writeValue(outputStream, merged); files.add(new GeneratedFile(Type.JSON_SCHEMA, JsonSchemaUtil.getFileName(entry.getKey()), outputStream.toByteArray())); } } return files; } private  JsonSchemaGenerator(Map<String, List<Class<?>>> map, SchemaVersion schemaVersion); Collection<GeneratedFile> generate(); static final Logger logger; static final SchemaVersion DEFAULT_SCHEMA_VERSION; }
@Test public void testSimpleSchemaGenerator() throws IOException { Collection<GeneratedFile> files = new JsonSchemaGenerator.SimpleBuilder(Thread.currentThread().getContextClassLoader()) .addSchemaName(PersonInputParams.class.getName(), "org.jbpm.test", "test") .addSchemaName(PersonOutputParams.class.getName(), "org.jbpm.test", "test") .build() .generate(); assertEquals(1, files.size()); GeneratedFile file = files.iterator().next(); assertEquals("org#jbpm#test_test.json", file.relativePath()); assertSchema(file, SchemaVersion.DRAFT_7); Collection<GeneratedFile> filesFromClasses = new JsonSchemaGenerator.ClassBuilder( Stream.of(PersonInputParams.class, PersonOutputParams.class, IgnoredClass.class)) .build().generate(); assertEquals(1, filesFromClasses.size()); GeneratedFile fileFromClasses = filesFromClasses.iterator().next(); assertEquals(fileFromClasses.relativePath(), file.relativePath(), "must have the same path of a class-based generator"); assertArrayEquals(fileFromClasses.contents(), file.contents(), "must have the same contents of a class-based generator"); }
@Test public void testJsonSchemaGeneratorNonExistingDraft() throws IOException { assertThrows(IllegalArgumentException.class, () -> { JsonSchemaGenerator.ClassBuilder builder = new JsonSchemaGenerator.ClassBuilder( Stream.of(PersonInputParams.class, PersonOutputParams.class, IgnoredClass.class)) .withSchemaNameFunction(c -> "pepe") .withSchemaVersion("NON_EXISTING_DRAFT"); builder.build().generate(); }); }
@Test public void testJsonSchemaGeneratorDraft2019() throws IOException { Collection<GeneratedFile> files = new JsonSchemaGenerator.ClassBuilder( Stream.of(PersonInputParams.class, PersonOutputParams.class, IgnoredClass.class)) .withSchemaVersion("DRAFT_2019_09").build().generate(); assertEquals(1, files.size()); GeneratedFile file = files.iterator().next(); assertEquals("org#jbpm#test_test.json", file.relativePath()); assertSchema(file, SchemaVersion.DRAFT_2019_09); }
@Test public void testJsonSchemaGeneratorInputOutput() throws IOException { Collection<GeneratedFile> files = new JsonSchemaGenerator.ClassBuilder(Stream.of(PersonInputOutputParams.class)).build().generate(); assertEquals(1, files.size()); GeneratedFile file = files.iterator().next(); assertEquals("InputOutput_test.json", file.relativePath()); assertSchema(file, SchemaVersion.DRAFT_7); }
@Test public void testNothingToDo() throws IOException { Collection<GeneratedFile> files = new JsonSchemaGenerator.ClassBuilder(Stream.of(IgnoredClass.class)).build().generate(); assertTrue(files.isEmpty()); }