fix: let explicit @Schema(format) override type-derived format (#5185)#5186
fix: let explicit @Schema(format) override type-derived format (#5185)#5186seonwooj0810 wants to merge 1 commit into
Conversation
|
I am not a maintainer but I feel a little unwell with this "global" fix. It does fix my issue, but it might allow for unintended behavior of the resolving. For example, I might annotate a int field with @Schema(format = "email") and the resolver would allow it. On the other hand, the 3.1 version allows that out of the box. A maintainer should look into that I just wanted to voice my concerns |
3caa575 to
1f6b0eb
Compare
…er-api#5185) `@Schema(format = "uri-reference")` on a `java.net.URI` field was ignored because `resolveSchemaMembers` only applied the annotation format when the schema had no format yet. For `java.net.URI` the `PrimitiveType` already sets `format: uri`, so the user-specified `uri-reference` was dropped. An explicit `format` on `@Schema` expresses the user's intent and must take precedence over a format derived from the property type. A URI field without an explicit format still resolves to the default `uri`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1f6b0eb to
f67b8a0
Compare
|
Thanks for raising this, @VadimKobyakov — it's a fair point to think through. I'd argue the broader behavior is actually correct here. In both OpenAPI and JSON Schema, The swagger-core doesn't validate format/type compatibility anywhere else either, so adding a type-vs-format gate just for this path would be inconsistent and arguably out of scope for the resolver. This change only makes 3.0 behave like 3.1 already does. That said, I fully agree a maintainer should make the final call. If they'd prefer a narrower fix (e.g. a dedicated |
Fixes #5185
Problem
When a field carries an explicit
@Schema(format = "...")whose owning type already resolves to a built-in format, the explicit format is silently dropped. The reported case isjava.net.URIannotated with@Schema(format = "uri-reference"): the type resolves toformat: urifirst, and because the schema already has a (type-derived) format, the explicit override is ignored — producingformat: uriinstead of the requesteduri-reference.@Schema#formatis documented as "Provides an optional override for the format", so an explicitly supplied format should win over the one inferred from the property type.Change
In
ModelResolver.resolveSchemaMembers(...)the format was only applied when the schema did not already have one:This is relaxed so that a non-blank explicit format always takes precedence over a type-derived format:
This fixes the
uri-referencecase in #5185 and any other type whose explicit@Schema(format)differs from its inferred format, without needing a per-type enum entry. A field with no explicit@Schema(format)still falls back to the type-derived format (e.g. plainURI→uri).Tests
Added
Ticket5185Testcovering both OpenAPI 3.0 and 3.1 resolution:@Schema(format = "uri-reference") URIresolves toformat: uri-referenceURI(no annotation) still resolves toformat: uriTest evidence
Full
swagger-coremodule suite after the change:Verification done: (1) No in-flight PR —
gh pr list --search "5185" / "uri-reference"returned none; no open PR touches this path. (2) No active claim — issue is unassigned and has no claim comments. (3) Code-focused — change is inModelResolver.javaplus a Java test. (4) Bug still present onmaster— the guarded condition is unchanged atModelResolverline 3184. (5) No parent epic closing this. Branch is based directly on currentmaster(no rebase needed).Note: the issue suggested adding a dedicated
URI_REFERENCEPrimitiveTypeenum entry. This PR instead fixes the root cause — explicit@Schema(format)should always override the inferred format — which coversuri-referenceand any future explicit-override case with a smaller, more general change. Happy to switch to the enum-based approach if maintainers prefer it.