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

Kotlin: Generating 'required' schema attribute? #466

Closed
yahorbarkouski opened this issue Aug 7, 2024 · 2 comments
Closed

Kotlin: Generating 'required' schema attribute? #466

yahorbarkouski opened this issue Aug 7, 2024 · 2 comments
Labels
duplicate This issue or pull request already exists

Comments

@yahorbarkouski
Copy link

yahorbarkouski commented Aug 7, 2024

Hey! Wondering if it's possible to generate a schema with 'required' attribute like in the following output:

"schema": {
  "type": "object",
  "properties": {
    "steps": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "explanation": {
            "type": "string"
          },
          "output": {
            "type": "string"
          }
        },
        "required": ["explanation", "output"], <- THIS LINE
        "additionalProperties": false
      }
    },
    "final_answer": {
      "type": "string"
    }
  },
  "required": ["steps", "final_answer"], <- THIS LINE
  "additionalProperties": false
}

If so, what option/setting should I use?

I was trying to do so by using withRequiredCheck (even added NotNull JetBrains annotations:)), but no luck:

val configBuilder = SchemaGeneratorConfigBuilder(DRAFT_2020_12, PLAIN_JSON)
      .with(FORBIDDEN_ADDITIONAL_PROPERTIES_BY_DEFAULT)
     
configBuilder.forFields()
      .withRequiredCheck(FieldRequiredCheck { field: Field -> field.isAnnotationPresent(NotNull::class.java) })
@CarstenWickner
Copy link
Member

HI @yahorbarkouski,

Generally, yes: the withRequiredCheck() is the correct place to configure this.
The example in the documentation is very similar to yours: https://victools.github.io/jsonschema-generator/#quot-required-quot-keyword

configBuilder.forFields()
    .withRequiredCheck(field -> field.getAnnotationConsideringFieldAndGetter(Nullable.class) == null);

However from your code snippet, I assume you are using Kotlin.
If that is the case, you are most likely affected by the same curious Kotlin behaviour as described on #358
If you annotate a field just with:

@NotNull
val field: String

The compiled Java code (that is being analysed by this jsonschema-generator library here), will treat the annotation as being present in a different place.
Instead, you would have to add an explicit prefix to indicate where the annotation shall be added in the compiled Java code, e.g.

@field:NotNull
val field: String

I would expect your configuration to work as expected then.

@yahorbarkouski
Copy link
Author

@CarstenWickner @field:NotNull doesn't work, unfortunately, but I get the direction, thank you so much!

@CarstenWickner CarstenWickner changed the title Generating 'required' schema attribute? Kotlin: Generating 'required' schema attribute? Aug 7, 2024
@CarstenWickner CarstenWickner added the duplicate This issue or pull request already exists label Sep 28, 2024
@CarstenWickner CarstenWickner closed this as not planned Won't fix, can't repro, duplicate, stale Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Development

No branches or pull requests

2 participants