Skip to content

Commit

Permalink
Fix: Edge case where raw Enum is defined as field/method type (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenWickner committed Aug 8, 2024
1 parent 67b1773 commit 0a960d5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,7 +104,10 @@ private static boolean isEnum(ResolvedType type) {
private static List<String> 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;
}
Expand All @@ -117,7 +121,7 @@ private static List<String> extractEnumValues(MethodScope method) {
*/
private static List<String> extractEnumValues(ResolvedType enumType, Function<Enum<?>, String> enumConstantToString) {
Class<?> erasedType = enumType.getErasedType();
if (erasedType.getEnumConstants() == null) {
if (Util.isNullOrEmpty(erasedType.getEnumConstants())) {
return null;
}
return Stream.of(erasedType.getEnumConstants())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Object>)\":{\"type\":\"integer\"},\"name()\":{\"type\":\"string\"}}}",
enumSchema.toString(), JSONCompareMode.STRICT);
}

private enum TestEnum {
VALUE1, VALUE2, VALUE3;

Expand All @@ -170,4 +201,8 @@ public String toString() {
return this.name().toLowerCase() + "_toString";
}
}

private static class TestType {
public Enum rawEnum;
}
}

0 comments on commit 0a960d5

Please sign in to comment.