Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to disable definition for ENUMS #123

Closed
pahujatarun25 opened this issue Jul 4, 2020 · 2 comments
Closed

How to disable definition for ENUMS #123

pahujatarun25 opened this issue Jul 4, 2020 · 2 comments
Labels
question Further information is requested

Comments

@pahujatarun25
Copy link

pahujatarun25 commented Jul 4, 2020

Hi,

I am configuring schema generator with following option "with(Option.DEFINITIONS_FOR_ALL_OBJECTS)" but its adding definition for ENUMS as well. For e.g.:

{
   "$schema":"https://json-schema.org/draft/2019-09/schema",
   "$defs":{
      "Department":{
         "type":"object",
         "properties":{
            "departmentName":{
               "type":"string",
               "default":"Science"
            }
         },
         "additionalProperties":false
      },
      "StudentUniform":{
         "type":"string",
         "enum":[
            "GREY",
            "WHITE"
         ]
      }
   },
   "type":"object",
   "properties":{
     
      "department123":{
         "$ref":"#/$defs/Department"
      },

      "uniform":{
         "$ref":"#/$defs/StudentUniform"
      }
   },
   "additionalProperties":false
}

Could you please tell how could I disable it for ENUM. Could you also please tell if I can change the order so that actual object definition appears at the end.

@CarstenWickner
Copy link
Member

HI Tarun,

to inline all enums you can use configBuilder.forTypesInGeneral().withCustomDefinitionProvider(new InlineAllEnumsDefinitionProvider()) with an instance of this custom definition provider:

class InlineAllEnumsDefinitionProvider implements CustomDefinitionProviderV2 {
    @Override
    public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) {
        if (javaType.isInstanceOf(Enum.class)) {
            ObjectNode standardDefinition = context.createStandardDefinition(javaType, this);
            return new CustomDefinition(standardDefinition, true);
        }
        return null;
    }
}

If you want the "$defs" property to be at the bottom, you could simply remove and re-add it after it has been generated:

    JsonNode jsonSchema = generator.generateSchema(Student.class);
    if (jsonSchema instanceof ObjectNode) {
        String definitionsKey = config.getKeyword(SchemaKeyword.TAG_DEFINITIONS);
        JsonNode definitionsNode = ((ObjectNode) jsonSchema).remove(definitionsKey);
        if (definitionsNode != null) {
            ((ObjectNode) jsonSchema).set(definitionsKey, definitionsNode);
        }
    }

@CarstenWickner CarstenWickner added the question Further information is requested label Jul 4, 2020
@pahujatarun25
Copy link
Author

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

2 participants