diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ca02817..48cc2880 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### `jsonschema-generator` +#### Changed +- consider `Boolean` values as valid in `const`/`enum` (i.e., no longer ignore them) + ### `jsonschema-module-jakarta-validation` #### Added - populate `const`/`enum` based on `@AssertTrue`/`@AssertFalse` diff --git a/jsonschema-examples/src/main/java/com/github/victools/jsonschema/examples/SingleArrayItemExample.java b/jsonschema-examples/src/main/java/com/github/victools/jsonschema/examples/SingleArrayItemExample.java new file mode 100644 index 00000000..55c7a319 --- /dev/null +++ b/jsonschema-examples/src/main/java/com/github/victools/jsonschema/examples/SingleArrayItemExample.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 VicTools. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.github.victools.jsonschema.examples; + +import com.fasterxml.classmate.ResolvedType; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.victools.jsonschema.generator.MemberScope; +import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerationContext; +import com.github.victools.jsonschema.generator.SchemaGenerator; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; +import com.github.victools.jsonschema.generator.SchemaVersion; +import com.github.victools.jsonschema.module.jakarta.validation.JakartaValidationModule; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Example created in response to #458. + *
+ * Demonstrating target type overrides to represent the Jackson {@code ACCEPT_SINGLE_VALUE_AS_ARRAY} feature. + */ +public class SingleArrayItemExample implements SchemaGenerationExampleInterface { + + @Override + public ObjectNode generateSchema() { + SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.PLAIN_JSON); + configBuilder.forFields() + .withTargetTypeOverridesResolver(this::acceptSingleValueAsArray); + SchemaGeneratorConfig config = configBuilder.build(); + SchemaGenerator generator = new SchemaGenerator(config); + return generator.generateSchema(Example.class); + } + + private List acceptSingleValueAsArray(MemberScope scope) { + if (scope.isContainerType() && !scope.isFakeContainerItemScope()) { + return Arrays.asList(scope.getContainerItemType(), scope.getType()); + } + return null; + } + + static class Example { + @NotNull + public List someArray; + } + + static class ArrayItem { + public String value; + } +} diff --git a/jsonschema-examples/src/test/java/com/github/victools/jsonschema/examples/ExampleTest.java b/jsonschema-examples/src/test/java/com/github/victools/jsonschema/examples/ExampleTest.java index c93bb35a..4a6c789c 100644 --- a/jsonschema-examples/src/test/java/com/github/victools/jsonschema/examples/ExampleTest.java +++ b/jsonschema-examples/src/test/java/com/github/victools/jsonschema/examples/ExampleTest.java @@ -39,6 +39,7 @@ public class ExampleTest { InheritanceRefExample.class, JacksonDescriptionAsTitleExample.class, JacksonSubtypeDefinitionExample.class, + SingleArrayItemExample.class, StrictTypeInfoExample.class, SubtypeLookUpExample.class, TargetTypeOverrideExample.class, diff --git a/jsonschema-examples/src/test/resources/com/github/victools/jsonschema/examples/SingleArrayItemExample-result.json b/jsonschema-examples/src/test/resources/com/github/victools/jsonschema/examples/SingleArrayItemExample-result.json new file mode 100644 index 00000000..03092d34 --- /dev/null +++ b/jsonschema-examples/src/test/resources/com/github/victools/jsonschema/examples/SingleArrayItemExample-result.json @@ -0,0 +1,28 @@ +{ + "$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" + } + } + ] + } + } +} \ No newline at end of file