feat: allow @ExampleObject values to be loaded from a file (#5180)#5183
Open
kuntal1461 wants to merge 1 commit into
Open
feat: allow @ExampleObject values to be loaded from a file (#5180)#5183kuntal1461 wants to merge 1 commit into
kuntal1461 wants to merge 1 commit into
Conversation
…pi#5180) Add externalFile() field to @ExampleObject annotation so large example payloads can be stored in classpath or filesystem files instead of inline string constants. Supports classpath: and file: prefixes (bare paths default to classpath). When both externalFile and value are set, externalFile takes precedence; falls back to value if the file is not found. Authored-By: Kuntal Maity <kuntal.1461@gmail.com>
| return Optional.empty(); | ||
| } | ||
|
|
||
| private static String loadExternalFile(String path) { |
Contributor
There was a problem hiding this comment.
Note: I am not affiliated with this repo. These are my own opinions.
I believe file handling is outside of what AnnotationUtils responsibilities, and that it would make sense to extract this to some separate file-handling util class. This so that the AnnotationUtils only know "I should fetch this from a file", but it does not have to consider itself in any way of how files are actually fetched and parsed.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
externalFile()field to@ExampleObjectannotation to load example payloads from classpath or filesystem filesAnnotationsUtils.resolveExample()to read the file content and process it identically to the inlinevaluefield (JSON-parsed, falling back to raw string)classpath:andfile:prefixes; bare paths default to classpathexternalFileandvalueare set,externalFiletakes precedence; gracefully falls back tovalueif the file is not foundMotivation
Closes #5180. For APIs with large payloads, defining examples inline in
@ExampleObject(value = "...")is unmanageable — it requires defining enormous string constants in Java classes. This feature lets users store example payloads in separate files:Test plan
AnnotationsUtilsTest: file-found (JSON parsed asJsonNode),externalFileprecedence overvalue, missing-file fallback tovalue,classpath:prefix handlingExamplesTest:ExternalFileRequestBodyExampleloads JSON fromexamples/external/user-request.jsonand verifies OpenAPI YAML outputswagger-core: 691,swagger-jaxrs2: 226)ByteArrayOutputStream+ byte-loop instead ofInputStream.readAllBytes())