Skip to content

Feature Request | Version switch to generate multiple schemas from one class definition #545

@ExLeonem

Description

@ExLeonem

Hey there,

I wanted to ask if there is any possibility to include a switch/feature flag or something like that to generate multiple JSON schemas from a single class.

Use- case

The company I'm working for would like to use this library to generate JSON schemas that we can publish. Which ultimately represent, the format of objects some of our REST API Endpoints expect.

However, these class definitions in part include properties, which we want to split into multiple JSON schemas. For example in a JSON schema v1.0 and v2.0 . The main class definition is rather large and dependent on a lot of other class defintions.

Splitting this class into multiple class definitions is not really an option for us, because we would have to re-write a lot of our current logic.

Example

What we would like to do is the following. We want to generate for following class

class A {
  private int attribute1; // should be visible in json schema for v1.0 and v2.0
  private String attribute2; // should only be visible in json schema for v1.0
  private String attribute3; // should only be visible in json schema for v2.0
  private String attribute4; // required for v1.0, not required for version v2.0
}

the following two definitions...

Definition a-schema-v1.json.

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
   "type": "object",
   "required": ["attribute4"],
   "properties": {
     "attribute1": {
       "type": "number"
     },
     "attribute2": {
        "type": "string"
     },
     "attribute4": {
        "type": "string"
     }
   }
}

Definition a-schema-v2.json:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
   "type": "object",
   "properties": {
     "attribute1": {
       "type": "number"
     },
     "attribute3": {
        "type": "string"
     },
     "attribute4": {
        "type": "string"
     }
   }
}

Without splitting the class definition, say for example into Av1 and Av2.

Implementation Idea

Our idea is to create a module which allows versioning of class attributes. Which in turn writes these into separate schema definitions.

Something like this:

class A {
  @JsonSchema(version={SchemaVersion.V1, SchemaVersion.V2})
  private int attribute1; // should only be visible in json schema for v1.0 and v2.0

  @JsonSchema(version=SchemaVersion.V1)
  private String attribute2; // should only be visible in json schema for v1.0

  @JsonSchema(version=SchemaVersion.V2)
  private String attribute3; // should only be visible in json schema for v2.0

  @JsonSchema(version=SchemaVersion.V1, required=true)
  @JsonSchema(version=SchemaVersion.V2)
  private String attribute4; // required for v1.0, not required for version v2.0
}

I checked the code and didnt see any possible to only achieve this via a module. I assume we need to extend classes in *-plugin and *-generator as well to achieve this?

Do you think this is somehow feasible? Would this something we could add as a feature of this library? If so we could provide a pull request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions