From 0a960d518d73aedff8623d2696efb80c6609d710 Mon Sep 17 00:00:00 2001 From: Carsten Wickner <11309681+CarstenWickner@users.noreply.github.com> Date: Thu, 8 Aug 2024 10:14:44 +0200 Subject: [PATCH 1/2] Fix: Edge case where raw Enum is defined as field/method type (#467) --- CHANGELOG.md | 5 +-- .../generator/impl/module/EnumModule.java | 8 +++-- .../generator/impl/module/EnumModuleTest.java | 35 +++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b4330df8..5c832612 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,9 @@ 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` +#### Fixed +- avoid exception when trying to collect supported enum values from raw `Enum` type (i.e., missing type parameter) ## [4.36.0] - 2024-07-20 ### `jsonschema-generator` diff --git a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/module/EnumModule.java b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/module/EnumModule.java index 559e5942..234fc906 100644 --- a/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/module/EnumModule.java +++ b/jsonschema-generator/src/main/java/com/github/victools/jsonschema/generator/impl/module/EnumModule.java @@ -26,6 +26,7 @@ import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; import com.github.victools.jsonschema.generator.SchemaKeyword; import com.github.victools.jsonschema.generator.impl.AttributeCollector; +import com.github.victools.jsonschema.generator.impl.Util; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; @@ -103,7 +104,10 @@ private static boolean isEnum(ResolvedType type) { private static List extractEnumValues(MethodScope method) { ResolvedType declaringType = method.getDeclaringType(); if (EnumModule.isEnum(declaringType)) { - return EnumModule.extractEnumValues(declaringType.getTypeParameters().get(0), Enum::name); + ResolvedType enumType = declaringType.getTypeParameters().stream().findFirst().orElse(null); + if (enumType != null) { + return EnumModule.extractEnumValues(enumType, Enum::name); + } } return null; } @@ -117,7 +121,7 @@ private static List extractEnumValues(MethodScope method) { */ private static List extractEnumValues(ResolvedType enumType, Function, String> enumConstantToString) { Class erasedType = enumType.getErasedType(); - if (erasedType.getEnumConstants() == null) { + if (Util.isNullOrEmpty(erasedType.getEnumConstants())) { return null; } return Stream.of(erasedType.getEnumConstants()) diff --git a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/impl/module/EnumModuleTest.java b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/impl/module/EnumModuleTest.java index 6e749916..06489dac 100644 --- a/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/impl/module/EnumModuleTest.java +++ b/jsonschema-generator/src/test/java/com/github/victools/jsonschema/generator/impl/module/EnumModuleTest.java @@ -26,21 +26,27 @@ import com.github.victools.jsonschema.generator.CustomDefinitionProviderV2; import com.github.victools.jsonschema.generator.FieldScope; import com.github.victools.jsonschema.generator.MethodScope; +import com.github.victools.jsonschema.generator.Option; import com.github.victools.jsonschema.generator.OptionPreset; +import com.github.victools.jsonschema.generator.SchemaGenerator; import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; import com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart; import com.github.victools.jsonschema.generator.SchemaGeneratorGeneralConfigPart; import com.github.victools.jsonschema.generator.SchemaKeyword; import com.github.victools.jsonschema.generator.SchemaVersion; import java.util.stream.Stream; +import org.json.JSONException; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.MethodSource; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; /** * Test for the {@link EnumModule} class. @@ -162,6 +168,31 @@ public void testCustomSchemaDefinition_asStrings(SchemaVersion schemaVersion, En Assertions.assertEquals(value3, enumNode.get(2).textValue()); } + @Test + public void testRawEnumType_asString() throws JSONException { + this.initConfigBuilder(SchemaVersion.DRAFT_2020_12); + this.builder.with(Option.PUBLIC_NONSTATIC_FIELDS, Option.NONSTATIC_NONVOID_NONGETTER_METHODS); + instanceAsStringsFromName.applyToConfigBuilder(this.builder); + + JsonNode enumSchema = new SchemaGenerator(this.builder.build()).generateSchema(TestType.class) + .get(SchemaKeyword.TAG_PROPERTIES.forVersion(SchemaVersion.DRAFT_2020_12)) + .get("rawEnum"); + JSONAssert.assertEquals("{\"type\":\"string\"}", enumSchema.toString(), JSONCompareMode.STRICT); + } + + @Test + public void testRawEnumType_asObject() throws JSONException { + this.initConfigBuilder(SchemaVersion.DRAFT_2020_12); + this.builder.with(Option.PUBLIC_NONSTATIC_FIELDS, Option.NONSTATIC_NONVOID_NONGETTER_METHODS); + instanceAsObjects.applyToConfigBuilder(this.builder); + + JsonNode enumSchema = new SchemaGenerator(this.builder.build()).generateSchema(TestType.class) + .get(SchemaKeyword.TAG_PROPERTIES.forVersion(SchemaVersion.DRAFT_2020_12)) + .get("rawEnum"); + JSONAssert.assertEquals("{\"type\":\"object\",\"properties\":{\"compareTo(Enum)\":{\"type\":\"integer\"},\"name()\":{\"type\":\"string\"}}}", + enumSchema.toString(), JSONCompareMode.STRICT); + } + private enum TestEnum { VALUE1, VALUE2, VALUE3; @@ -170,4 +201,8 @@ public String toString() { return this.name().toLowerCase() + "_toString"; } } + + private static class TestType { + public Enum rawEnum; + } } From 6c939c04d9d3566ecf95348147eb71ba7fa16cac Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 22 Aug 2024 22:57:46 +0200 Subject: [PATCH 2/2] chore(deps): bump rexml from 3.3.3 to 3.3.6 in /slate-docs (#469) --- slate-docs/Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slate-docs/Gemfile.lock b/slate-docs/Gemfile.lock index 624a7217..1d5b9ba3 100644 --- a/slate-docs/Gemfile.lock +++ b/slate-docs/Gemfile.lock @@ -98,7 +98,7 @@ GEM rb-inotify (0.10.1) ffi (~> 1.0) redcarpet (3.6.0) - rexml (3.3.3) + rexml (3.3.6) strscan rouge (3.30.0) sass (3.7.4)