micronaut-projects / micronaut-json-schema

Micronaut integration with JSON schema
Apache License 2.0
2 stars 0 forks source link

Adding nullability annotations which imply null is not accepted should generate required attribute #26

Closed sdelamo closed 4 months ago

sdelamo commented 5 months ago

Issue description

The following class adds jakarta.validation.constraints.NotBlank and jakarta.validation.constraints.NotNull annotations which imply null is not valid.

For a class such as:

package example.micronaut;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.jsonschema.JsonSchema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import java.util.Set;

/**
 *
 * @param productId The unique identifier for a product
 * @param productName name of the product
 * @param price The price of the product
 * @param tags Tags for the product
 */
@JsonSchema(description = "A product from Acme's catalog")
public record Product(@NotNull Integer productId,
                      @NotBlank String productName,
                      @NotNull @Positive Number price,
                      @Nullable @Size(min = 1) Set<String> tags) {
}

the following JSON Schema is generated:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "http://localhost:8080/schemas/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": [
    "object"
  ],
  "properties": {
    "price": {
      "title": "Number",
      "description": "The price of the product",
      "type": [
        "object"
      ],
      "exclusiveMinimum": 0
    },
    "productId": {
      "description": "The unique identifier for a product",
      "type": [
        "integer"
      ]
    },
    "productName": {
      "description": "name of the product",
      "type": [
        "string"
      ],
      "minLength": 1
    },
    "tags": {
      "description": "Tags for the product",
      "type": [
        "array",
        "null"
      ],
      "items": {
        "type": [
          "string"
        ]
      },
      "minItems": 1
    }
  }
}

I think the schema should contain "required": [ "productId", "productName", "price" ]

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
...
..
.
  "properties": {
...
..
.
  },
  "required": [ "productId", "productName", "price" ]
}

Also, please note that tags is annotated with io.micronaut.core.annotation.Nullable. The type is set with: "type": ["array","null"],. Shouldn't the type just be "type": ["array"],