mbknor / mbknor-jackson-jsonSchema

Generate JSON Schema with Polymorphism using Jackson annotations
MIT License
234 stars 79 forks source link

Do not inline enum class #144

Open huehnerlady opened 3 years ago

huehnerlady commented 3 years ago

Hi,

I am trying to generate a schema from my classes. so far I do like the feel of this library, but I did run into a problem which would prevent me actually using it: The enum classes get inlined instead of separated. I saw that this was already asked in https://github.com/mbknor/mbknor-jackson-jsonSchema/issues/11, but seemed to have been closed without fixing it?

It was asked for a Valid json: I would expect, that the following Java class

public class Foo {

  public FooEnum value1;
  public FooEnum value2;
}

public enum FooEnum {
  FIRST_VALUE,
  SECOND_VALUE,
  THIRD_VALUE;
}

would generate the following schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Foo",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "value1": {
      "type": "string",
      "$ref": "#/definitions/FooEnum"
    },
    "value2": {
      "type": "string",
      "$ref": "#/definitions/FooEnum"
    }
  },
  "definitions": {
    "FooEnum": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    }
  }
}

but it generates:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Foo",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "value1": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    },
    "value2": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    }
  }
}

Is there the possibility to maybe fix that?

stellingsimon commented 2 years ago

This is causing problems for our use cases, too. So far we've been able to revert the undesired behaviour in a wrapper for the generator, but now we stumbled across a use-case which cannot be fixed externally.

Consider the following example:

public class Foo {

  public FooEnum value1;
  public AnotherFooEnum value2;
}

public enum FooEnum {
  FIRST_VALUE,
  SECOND_VALUE,
  THIRD_VALUE;
}

public enum AnotherFooEnum {
  FIRST_VALUE,
  SECOND_VALUE,
  THIRD_VALUE;
}

should generate

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Foo",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "value1": {
      "type": "string",
      "$ref": "#/definitions/FooEnum"
    },
    "value2": {
      "type": "string",
      "$ref": "#/definitions/AnotherFooEnum"
    }
  },
  "definitions": {
    "FooEnum": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    }
    Another"FooEnum": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    }
  }
}

but generates

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Foo",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "value1": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    },
    "value2": {
      "type": "string",
      "enum": [
        "FIRST_VALUE",
        "SECOND_VALUE",
        "THIRD_VALUE"
      ]
    }
  }
}