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

Maven plugin with Jackson and ACCEPT_SINGLE_VALUE_AS_ARRAY #458

Closed
Boereck opened this issue Jul 19, 2024 · 4 comments · Fixed by #460
Closed

Maven plugin with Jackson and ACCEPT_SINGLE_VALUE_AS_ARRAY #458

Boereck opened this issue Jul 19, 2024 · 4 comments · Fixed by #460
Assignees
Labels
enhancement New feature or request

Comments

@Boereck
Copy link

Boereck commented Jul 19, 2024

Hi,

I am using the jsonschema-maven-plugin to generate a JSON Schema for Jackson annotated classes. On the ObjectMapper in the code I set the property com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY to true. I cannot find a configuration option for the Jackson module of the maven plugin. Is there an option I missed, or is it not (yet) possible to configure such feature on the maven plugin?

Thanks for the work on this projekt, and best regards,
Max

@CarstenWickner
Copy link
Member

CarstenWickner commented Jul 19, 2024

Hi @Boereck,
Indeed, this is currently not supported out-of-the-box.

But I've created an example of how this can be easily achieved through configuration (for use via the Maven plugin, you'd have to wrap it in a Module of your own): SingleArrayItemExample

Basically you can do this:

configBuilder.forFields()
        .withTargetTypeOverridesResolver(scope -> scope.isContainerType() && !scope.isFakeContainerItemScope()
                Arrays.asList(scope.getContainerItemType(), scope.getType()) : null);

For a data type like this:

    class Example {
        public List<ArrayItem> someArray;
    }

    class ArrayItem {
        public String value;
    }

The generated schema ends up with an anyOf containing the single item type and an array of the item type:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$defs": {
        "ArrayItem": {
            "type": "object",
            "properties": {
                "value": {
                    "type": "string"
                }
            }
        }
    },
    "type": "object",
    "properties": {
        "someArray": {
            "anyOf": [
                {
                    "$ref": "#/$defs/ArrayItem"
                }, {
                    "type": "array",
                    "items": {
                        "$ref": "#/$defs/ArrayItem"
                    }
                }
            ]
        }
    }
}

We could add a standard Option on the main generator library for that behavior. It's straightforward enough.
I might do it in the next couple of days.

@Boereck
Copy link
Author

Boereck commented Jul 20, 2024

Wow, that was fast! Thank you so much!

@CarstenWickner
Copy link
Member

The release of v4.36.0 was also done.
It should be available in a couple of hours.

@Boereck
Copy link
Author

Boereck commented Jul 21, 2024

Awesome! Works like a charm. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants