Skip to content

Conversation

@juntae6942
Copy link
Contributor

@juntae6942 juntae6942 commented Oct 24, 2025

Closes: #5003

Description
This PR fixes a regression bug that causes an AnnotatedType cache collision when a polymorphic type is resolved in two different contexts: once as a 'property' (e.g., direct return type) and once as a 'subtype' (e.g., from @JsonSubTypes).

The regression was introduced when equals()/hashCode() began normalizing context annotations but did not properly account for flags like schemaProperty or propertyName. This caused both 'property' and 'subtype' resolutions to merge into the same cache entry, leading to an incomplete schema (the subtype-specific fragment was skipped).

This fix addresses the issue in two parts:

AnnotatedType.java: equals() and hashCode() are updated to correctly compare the schemaProperty flag. The propertyName is now also conditionally compared only when schemaProperty is true, aligning equals and hashCode logic.

ModelResolver.java: resolveSubtypes() is updated to explicitly set schemaProperty(true) when creating an AnnotatedType for a class discovered via @JsonSubTypes.

This combination ensures that the 'property' context (schemaProperty=false) and 'subtype' context (schemaProperty=true) generate distinct cache keys, resolving the collision.

Fixes: ## Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • ♻️ Refactor (non-breaking change)
  • 🧪 Tests
  • 📝 Documentation
  • 🧹 Chore (build or tooling)

Checklist

  • I have added/updated tests as needed
  • I have added/updated documentation where applicable
  • The PR title is descriptive
  • The code builds and passes tests locally
  • I have linked related issues (if any)

Screenshots / Additional Context
This fix can be verified using the reproduction project provided in the bug report: https://github.com/benjaminknauer/swagger-2-2-39-polymorphism-bug-example

With this patch applied, the BuggySchemaIntegrationTest in that project passes successfully.

The new unit tests added to AnnotatedTypeTest.java specifically validate the new equals/hashCode logic for all schemaProperty and propertyName scenarios to prevent future regressions.

@ewaostrowska
Copy link
Contributor

Hi @juntae6942,

Thank you for your quick response to this issue :) we do appreciate it a lot.

This should fix the problem of polymorphic schemas caching.
However, for now I cannot accept this PR because of the side effects it may cause. Setting schemaProperty(true) in the ModelResolver is the part that can be troublesome. That flag may influence not only equality/hashCode, but also conditional logic elsewhere that checks isSchemaProperty() to decide how to decorate the resolved schema. There’s a risk of formatting differences in the composed schema. I have added a test to illustrate this issue.

There are few approaches that could be taken here, but I need decision from the team. Please hold on for now with the topic. I will inform you whenever there's any decision.

Thanks again for your contribution!

@ewaostrowska ewaostrowska force-pushed the fix/annotated-type-caching-5003 branch from d8efab6 to 3f6791c Compare October 28, 2025 14:09
…isSubtype property in AnnotatedType and add tests
@ewaostrowska ewaostrowska force-pushed the fix/annotated-type-caching-5003 branch from 3f6791c to 51cb682 Compare October 28, 2025 14:55
@ewaostrowska
Copy link
Contributor

@juntae6942,
I think introducing a isSubtype variable here instead of setting schemaProperty(true) for subtypes should resolve this issue.

@juntae6942
Copy link
Contributor Author

Hi ewaostrowska,

Ah, I see you've already pushed a commit using the isSubtype flag. That makes perfect sense, and the approach looks good! 👍

Thanks for taking care of the fix!

Is there anything else needed from my end for this?

@ewaostrowska
Copy link
Contributor

@juntae6942 no, I don't think anything anything more from your side is needed :) thanks for contributing!

@juntae6942
Copy link
Contributor Author

ewaostrowska, Thanks for the quick feedback! I really appreciate it.
I’d love to continue contributing to Swagger in the future as well. 😊

@ewaostrowska ewaostrowska merged commit 8260e8f into swagger-api:master Oct 31, 2025
5 checks passed
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.

[Bug]: Regression: AnnotatedType equality change drops polymorphic subtype allOf entries

3 participants