fix: propagate @Schema(description, example) on DTO fields to query parameter descriptions#397
Open
vcfee wants to merge 1 commit into
Open
Conversation
…arameter descriptions When a DTO is bound as query parameters via Spring's implicit @ModelAttribute binding, the plugin only honored the field's Javadoc and ignored @Schema(description / example). The same @Schema(description) is correctly read on the components.schemas path, so the behavior was inconsistent. * SpringMvcReader.bindDtoToQueryParams now also reads @Schema(description, example) from the field and getter (mirrors the components.schemas lookup logic). * YamlWriter no longer overrides description/summary when the Javadoc fallback is empty, so a missing Javadoc cannot erase a description set from @Schema. Javadoc still wins when both exist (precedence preserved). Tests: SchemaDescriptionDtoController + SchemaDescriptionDto cover four scenarios (Schema only / Javadoc only / both / Schema example only). All 147 existing unit tests still pass. Co-authored-by: Cursor <cursoragent@cursor.com>
Owner
|
Hello @vcfee Thank you very much for this contribution. The description is detailed and clear. I'm curently abroad away from my computer and I'll be able to look the details of this PR after June 20. Kind regards, |
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
When a DTO is bound as query parameters of a
@GetMappingendpoint (via Spring'simplicit
@ModelAttributebinding), the plugin only honored the field's Javadocfor the parameter
description. Field-level@Schema(description = "...")/@Schema(example = "...")annotations were ignored.The same
@Schema(description = "...")is correctly read on thecomponents.schemas.X.properties.Y.descriptionpath, so the behavior wasinconsistent across the two paths.
Before
Generated OpenAPI:
After
Changes
SpringMvcReader.bindDtoToQueryParams— when binding a DTO field to aquery parameter, now also reads
@Schema(description, example)from thefield and getter. The lookup logic mirrors what is already used in
components.schemasso the two paths stay consistent.YamlWriter(line 339-345) — when applying the Javadoc fallback for aparameter that came from a DTO field, only override
description/summaryif Javadoc provides a non-empty value. Previouslyan empty Javadoc would erase a description that was correctly set from
@Schema.Precedence (unchanged)
When both Javadoc and
@Schemaexist on the same DTO field, Javadoc stillwins — this preserves the existing behavior for users who rely on Javadoc.
Tests
Added
SpringClassAnalyserTest#schema_description_on_dto_query_paramandSchemaDescriptionDtoController/SchemaDescriptionDto. The DTO coversfour scenarios:
fieldFromSchemaOnly@SchemafieldFromJavadocOnlyfieldFromBothJavadocWins@SchemafieldWithExampleFromSchemaOnly@SchemaThe new approved YAML is committed alongside the test.
All 147 existing unit tests still pass:
Real-world driver
Many existing codebases (Spring + Swagger v3) only annotate DTOs with
@Schema, especially when using@Datafrom Lombok which discourages writingJavadoc on the field. Today, every such DTO field shows up as an undocumented
query parameter in the generated OpenAPI / in any downstream tool (Apifox /
Swagger UI / Postman).
A quick scan on one of our internal services showed 540 out of 1556 query
parameters (~35%) missing descriptions purely because of this gap, even though
the source DTOs were fully annotated with
@Schema.Notes
mvn formatter:formatreports no diff.dependency that was used in this PR; main source uses Spring's
MergedAnnotationmechanism that the plugin already relies on).