0

How do i generate a single schema file from multiple beans in Jackson? My code currently spews out one single schema file per bean:

List<Class> klasses = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
JsonSchemaGenerator schemaGenerator = new JsonSchemaGenerator(objectMapper);
klasses.forEach(klass -> {
      try {
          JsonSchema jsonSchema = schemaGenerator.generateSchema(klass);

          String schema = JSONUtils.toPrettyJSON(jsonSchema);
          File file = new File("javascript/external/packages/types/res/" + klass.getSimpleName() + ".schema");
          Files.write(file.toPath(), schema.getBytes());
      } catch (IOException e) {
          throw new RuntimeException(e);
      }
  });

Is there a way to generate a single schema file rather than individual files for each bean? The documentation seems to be rather lacking around this area.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
  • By definition a JSON Schema describes a single data structure. What do you want the combination to look like? One option could be to introduce another class that has each of the subschemas as one of its properties. Then you generate a single schema for this wrapper type. Would that work for you? Another alternative is to go for something like OpenAPI where you put each of the types into context. – Carsten Aug 17 '20 at 07:52
  • My bad. What I meant is that, if it possible inline all the references into a `definitions ` object like so: https://github.com/natesilva/jayschema/blob/master/examples/fstab/entry-schema.json Right now, I generate one schema file per bean. – Mridang Agarwalla Aug 17 '20 at 10:56
  • The given example would match with my proposal of creating a wrapper type. You just have to then force it to create definitions for everything to avoid some elements only being defined “inline” in the main schema’s properties. – Carsten Aug 17 '20 at 11:00

0 Answers0