Skip to content

feat: allow @ExampleObject values to be loaded from a file (#5180)#5183

Open
kuntal1461 wants to merge 1 commit into
swagger-api:masterfrom
kuntal1461:feature/5180-example-object-external-file
Open

feat: allow @ExampleObject values to be loaded from a file (#5180)#5183
kuntal1461 wants to merge 1 commit into
swagger-api:masterfrom
kuntal1461:feature/5180-example-object-external-file

Conversation

@kuntal1461
Copy link
Copy Markdown
Contributor

Summary

  • Add externalFile() field to @ExampleObject annotation to load example payloads from classpath or filesystem files
  • Modify AnnotationsUtils.resolveExample() to read the file content and process it identically to the inline value field (JSON-parsed, falling back to raw string)
  • Supports classpath: and file: prefixes; bare paths default to classpath
  • When both externalFile and value are set, externalFile takes precedence; gracefully falls back to value if the file is not found

Motivation

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:

@ExampleObject(
    name = "Create User",
    externalFile = "classpath:openapi/examples/create-user-request.json",
    summary = "Full user creation example"
)

Test plan

  • Unit tests added to AnnotationsUtilsTest: file-found (JSON parsed as JsonNode), externalFile precedence over value, missing-file fallback to value, classpath: prefix handling
  • Integration test added to ExamplesTest: ExternalFileRequestBodyExample loads JSON from examples/external/user-request.json and verifies OpenAPI YAML output
  • All 917 existing tests pass (swagger-core: 691, swagger-jaxrs2: 226)
  • Java 8 compatible (uses ByteArrayOutputStream + byte-loop instead of InputStream.readAllBytes())

…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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Allow values in @ExampleObject to be read from a file

2 participants