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

feat: extend support for void methods #441

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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());
}

/**
Original file line number Diff line number Diff line change
@@ -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) {
Original file line number Diff line number Diff line change
@@ -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.
@@ -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;
@@ -256,4 +278,10 @@ private static class TestCircularClass2 {
public List<TestCircularClass1> list1;

}

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