Skip to content

Commit

Permalink
feat: extend support for void methods (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarstenWickner authored Mar 29, 2024
1 parent f226ad9 commit 4938865
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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`
#### Added
- check for custom definitions for `void` methods (this may result in exceptions inside custom configuration if a `null` return type is not considered)

#### Changed
- if present, apply custom definition for `void` methods

## [4.34.0] - 2024-03-14
### `jsonschema-generator`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,12 @@ private JsonNode populateMethodSchema(MethodScope method) {
*/
private JsonNode createMethodSchema(MemberDetails<MethodScope> methodDetails) {
if (methodDetails.getScope().isVoid()) {
return BooleanNode.FALSE;
// since 4.35.0: support custom definitions for void methods
CustomDefinition customDefinition = this.generatorConfig.getCustomDefinition(methodDetails.getScope(), this,
methodDetails.getIgnoredDefinitionProvider());
if (customDefinition == null) {
return BooleanNode.FALSE;
}
}
ObjectNode subSchema = this.generatorConfig.createObjectNode();
ObjectNode methodAttributes = AttributeCollector.collectMethodAttributes(methodDetails.getScope(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public FlattenedWrapperModule(Class<W> wrapperType) {
* @return whether the given type is deemed to be of the targeted wrapper type in the context of this module
*/
protected boolean isWrapperType(ResolvedType type) {
return this.wrapperType.isAssignableFrom(type.getErasedType());
return type != null && this.wrapperType.isAssignableFrom(type.getErasedType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,14 @@ private Map<String, Type> resolvePatternProperties(TypeScope scope) {
}

private boolean shouldHaveEmptySchema(TypeScope scope) {
return SchemaKeyword.TAG_TYPE_NULL == this.fixedJsonSchemaTypes.get(scope.getType().getErasedType());
return scope.getType() == null
|| SchemaKeyword.TAG_TYPE_NULL == this.fixedJsonSchemaTypes.get(scope.getType().getErasedType());
}

/**
* Implementation of the {@link CustomDefinitionProviderV2} interface for applying fixed schema definitions for simple java types.
*/
private class SimpleTypeDefinitionProvider implements CustomDefinitionProviderV2 {
private final class SimpleTypeDefinitionProvider implements CustomDefinitionProviderV2 {

@Override
public CustomDefinition provideCustomSchemaDefinition(ResolvedType javaType, SchemaGenerationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

/**
* Test for {@link SchemaGenerator} class.
Expand Down Expand Up @@ -229,6 +232,25 @@ public void testGenerateSchema_CustomPropertyDefinition(SchemaVersion schemaVers
TestUtils.assertGeneratedSchema(result, this.getClass(), "custom-property-definition-" + schemaVersion.name() + ".json");
}

@Test
public void testGenerateSchema_CustomPropertyDefinitionForVoidMethod() throws Exception {
CustomPropertyDefinitionProvider<MethodScope> customPropertyDefinitionProvider = (method, context) -> {
if (method.isVoid()) {
return new CustomPropertyDefinition(context.getGeneratorConfig().createObjectNode()
.put(context.getKeyword(SchemaKeyword.TAG_DESCRIPTION), "this method is void"));
}
return null;
};
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, OptionPreset.JAVA_OBJECT);
configBuilder.with(Option.VOID_METHODS);
configBuilder.forMethods()
.withCustomDefinitionProvider(customPropertyDefinitionProvider);
SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
JsonNode result = generator.generateSchema(TestClassWithVoidMethod.class);
JSONAssert.assertEquals("{\"type\":\"object\",\"properties\":{\"updateSomething()\":{\"description\":\"this method is void\"}}}",
result.toString(), JSONCompareMode.STRICT);
}

private static class TestDirectCircularClass {

public int number;
Expand Down Expand Up @@ -256,4 +278,10 @@ private static class TestCircularClass2 {
public List<TestCircularClass1> list1;

}

private static class TestClassWithVoidMethod {
public void updateSomething() {
// perform an action
}
}
}

0 comments on commit 4938865

Please sign in to comment.