-
Notifications
You must be signed in to change notification settings - Fork 0
fix nullability customizer #47
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
...springboot/toolbox/swagger/customization/default_not_null/NullablePropertyCustomizer.java
This file was deleted.
Oops, something went wrong.
158 changes: 158 additions & 0 deletions
158
...its/springboot/toolbox/swagger/customization/default_not_null/NullableCustomizerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package it.aboutbits.springboot.toolbox.swagger.customization.default_not_null; | ||
|
|
||
| import io.swagger.v3.oas.models.Components; | ||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.media.Schema; | ||
| import io.swagger.v3.oas.models.media.StringSchema; | ||
| import org.jspecify.annotations.NullUnmarked; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| @NullUnmarked | ||
| class NullableCustomizerTest { | ||
|
|
||
| public static class BaseClass { | ||
| @Nullable | ||
| private String baseField; | ||
|
|
||
| public String getBaseField() { | ||
| return baseField; | ||
| } | ||
| } | ||
|
|
||
| public static class SubClass extends BaseClass { | ||
| private String subField; | ||
|
|
||
| public String getSubField() { | ||
| return subField; | ||
| } | ||
| } | ||
|
|
||
| public static class MethodAnnotated { | ||
| private String annotatedGetter; | ||
|
|
||
| @Nullable | ||
| public String getAnnotatedGetter() { | ||
| return annotatedGetter; | ||
| } | ||
| } | ||
|
|
||
| public static class DirectMethodAnnotated { | ||
| private String directMethod; | ||
|
|
||
| @Nullable | ||
| public String directMethod() { | ||
| return directMethod; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void shouldFindFieldInSuperClass() { | ||
| var customizer = new NullableCustomizer(); | ||
| var openApi = new OpenAPI(); | ||
| var components = new Components(); | ||
|
|
||
| var subClassSchema = new Schema<Object>(); | ||
| subClassSchema.setName(SubClass.class.getName()); | ||
| subClassSchema.addProperty("baseField", new StringSchema()); | ||
| subClassSchema.addProperty("subField", new StringSchema()); | ||
|
|
||
| components.addSchemas(SubClass.class.getName(), subClassSchema); | ||
| openApi.setComponents(components); | ||
|
|
||
| assertDoesNotThrow(() -> customizer.customise(openApi)); | ||
|
SirCotare marked this conversation as resolved.
Outdated
|
||
|
|
||
| List<String> required = subClassSchema.getRequired(); | ||
| assertTrue(required != null && required.contains("subField"), "subField should be required"); | ||
| assertTrue(required == null || !required.contains("baseField"), "baseField should NOT be required"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldFindAnnotationOnGetter() { | ||
| var customizer = new NullableCustomizer(); | ||
| var openApi = new OpenAPI(); | ||
| var components = new Components(); | ||
|
|
||
| var schema = new Schema<Object>(); | ||
| schema.setName(MethodAnnotated.class.getName()); | ||
| schema.addProperty("annotatedGetter", new StringSchema()); | ||
|
|
||
| components.addSchemas(MethodAnnotated.class.getName(), schema); | ||
| openApi.setComponents(components); | ||
|
|
||
| assertDoesNotThrow(() -> customizer.customise(openApi)); | ||
|
|
||
| List<String> required = schema.getRequired(); | ||
| assertTrue(required == null || !required.contains("annotatedGetter"), "annotatedGetter should NOT be required"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldFindAnnotationOnDirectMethod() { | ||
| var customizer = new NullableCustomizer(); | ||
| var openApi = new OpenAPI(); | ||
| var components = new Components(); | ||
|
|
||
| var schema = new Schema<Object>(); | ||
| schema.setName(DirectMethodAnnotated.class.getName()); | ||
| schema.addProperty("directMethod", new StringSchema()); | ||
|
|
||
| components.addSchemas(DirectMethodAnnotated.class.getName(), schema); | ||
| openApi.setComponents(components); | ||
|
|
||
| assertDoesNotThrow(() -> customizer.customise(openApi)); | ||
|
|
||
| List<String> required = schema.getRequired(); | ||
| assertTrue(required == null || !required.contains("directMethod"), "directMethod should NOT be required"); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldHandleConcatenatedFqns() { | ||
| var customizer = new NullableCustomizer(); | ||
| var openApi = new OpenAPI(); | ||
| var components = new Components(); | ||
|
|
||
| var schema = new Schema<Object>(); | ||
| // Simulating the concatenated FQN pattern described in the issue | ||
| var concatenatedFqn = SubClass.class.getName() + "It.aboutbits.something"; | ||
| schema.setName(concatenatedFqn); | ||
| schema.addProperty("baseField", new StringSchema()); | ||
|
|
||
| components.addSchemas(concatenatedFqn, schema); | ||
| openApi.setComponents(components); | ||
|
|
||
| assertDoesNotThrow(() -> customizer.customise(openApi)); | ||
|
|
||
| List<String> required = schema.getRequired(); | ||
| assertTrue( | ||
| required == null || !required.contains("baseField"), | ||
| "baseField should NOT be required even with concatenated FQN" | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldNotThrowWhenFieldNotFound() { | ||
| var customizer = new NullableCustomizer(); | ||
| var openApi = new OpenAPI(); | ||
| var components = new Components(); | ||
|
|
||
| var schema = new Schema<Object>(); | ||
| schema.setName(SubClass.class.getName()); | ||
| schema.addProperty("nonExistent", new StringSchema()); | ||
|
|
||
| components.addSchemas(SubClass.class.getName(), schema); | ||
| openApi.setComponents(components); | ||
|
|
||
| assertDoesNotThrow(() -> customizer.customise(openApi)); | ||
|
|
||
| List<String> required = schema.getRequired(); | ||
| assertTrue( | ||
| required != null && required.contains("nonExistent"), | ||
| "nonExistent field should be considered required if not found and not nullable" | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.