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

conditionals with jackson subtype resolution #153

Open
abhinavcode opened this issue Nov 25, 2020 · 2 comments
Open

conditionals with jackson subtype resolution #153

abhinavcode opened this issue Nov 25, 2020 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@abhinavcode
Copy link

abhinavcode commented Nov 25, 2020

I have a requirement where I want to make if/else conditions for subtypes (annotated via jackson's JsonSubType.class). It could be either EXTERNAL_PROPERTY or WRAPPER_OBJECT which decides the conditionals block.

public class Animal {
  String name;
  String type;
  AnimalData animalData;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Human.class, name = "human"),
        @JsonSubTypes.Type(value = Frog.class, name = "frog"), })
public interface AnimalData{}

I want someway to get if/then blocks without traversing the final JsonNode.
My expected output is:

{
   "$schema":"http://json-schema.org/draft-07/schema#",
   "type":"object",
   "properties":{
      "name":{
         "type":"string"
      },
      "type":{
         "type":"string"
      },
      "allOf":[
         {
            "if":{
               "properties":{
                  "type":{
                     "const":"human"
                  }
               }
            },
            "then":{
               "properties":{
                  "animalData":{
                     // human json 
                  }
               }
            }
         },
         {
            "if":{
               "properties":{
                  "type":{
                     "const":"frog"
                  }
               }
            },
            "then":{
               "properties":{
                  "animalData":{
                     // frog json
                  }
               }
            }
         }
      ]
   }
}

Is it possible to generate this using the jsonschema generator, without traversing the final JsonNode?

@abhinavcode abhinavcode changed the title if/else with jackson subtype resolution conditionals with jackson subtype resolution Nov 25, 2020
@abhinavcode
Copy link
Author

abhinavcode commented Nov 26, 2020

I read #130 but it is adding constants to conditionals.
I wanted some control to resolve subtype in an if condition.
I tried advanced configurations but couldn't figure out a way.

@CarstenWickner
Copy link
Member

CarstenWickner commented Nov 26, 2020

Hi @abhinavcode,

EXTERNAL_PROPERTY is a tricky one to handle generically, that's why it's not yet supported by the JacksonModule out-of-the-box. I'm open for pull requests though. 😉

However, it seems you're handling it yourself already and the main question is about not having to manually build the nested schemas once you're inside of a CustomDefinition.
I've tried to document the most important methods here: https://victools.github.io/jsonschema-generator/#custom-type-definitions
For this particular use-case, it sounds like createDefinitionReference()/createStandardDefinitionReference() would be an approriate choice.

As an additional reference, you could look into the JacksonModule implementation (or its JsonSubTypesResolver in particular), where the WRAPPER_OBJECT alongside some other Jackson subtype strategies are being handled.

@CarstenWickner CarstenWickner added the help wanted Extra attention is needed label Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Development

No branches or pull requests

2 participants