Fix discriminator values in writable type variants#3301
Conversation
|
|
🦋 Changeset detectedLatest commit: 7e6945d The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3301 +/- ##
=======================================
Coverage 34.89% 34.89%
=======================================
Files 401 401
Lines 22244 22244
Branches 1429 1429
=======================================
Hits 7763 7763
Misses 14477 14477
Partials 4 4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@hey-api/codegen-core
@hey-api/nuxt
@hey-api/openapi-ts
@hey-api/shared
@hey-api/types
@hey-api/vite-plugin
commit: |
|
@copilot you need to add an actual snapshot test using the spec you added in this pull request so we can verify the fix works |
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Added snapshot test for the new spec in commit 0b6a4fd. The test validates the current behavior where discriminator values in oneOf contexts are correct, while documenting the remaining issue with allOf inheritance. |
|
@copilot have a look at the "Generated TS Writable types (incorrect):" section in the original issue and make sure you actually address the problems. So far, you've only confirmed we can reproduce the issue |
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Fixed in commit 8126ad1. Writable types now correctly preserve discriminator values from the OpenAPI spec (e.g., |
Writable type variants generated from schemas with discriminators were using schema names (
'DogPayloadWritable') instead of preserving the original discriminator values ('dog') from the OpenAPI spec, causing backend deserialization failures.Changes
Fixed discriminator values in writable type variants:
AnimalPayloadWritable)Test coverage:
specs/3.1.x/discriminator-one-of-read-write.yamldemonstrating the issue with oneOf + discriminators + readOnly propertiespackages/openapi-ts-tests/main/test/3.1.x.test.tsto validate the fixSolution
When schemas with discriminators are split into read/write variants, parent schemas (like
AnimalPayload) that define discriminator mappings are now also cloned into writable variants (likeAnimalPayloadWritable). The discriminator mappings in these parent variants are updated to point to the appropriate child variants:AnimalPayload(read) maps:dog -> DogPayload,cat -> CatPayloadAnimalPayloadWritable(write) maps:dog -> DogPayloadWritable,cat -> CatPayloadWritableWritable child schemas reference the writable parent variant via allOf, ensuring they inherit the correct discriminator mappings.
Result
All discriminator values now correctly preserve the original OpenAPI spec values:
Original prompt
This section details on the original issue you should resolve
<issue_title>Write schemas generated for oneOf types override type discriminator values</issue_title>
<issue_description>### Description
When
@hey-api/openapi-tsgenerates TypeScript types from an OpenAPI spec containing polymorphic types withreadOnlyproperties, it creates "Writable" variants with incorrecttypeDiscriminatorvalues.The generated types use a PascalCase discriminator derived from the type name (e.g.,
'DogPayloadWritable') instead of preserving the discriminator value from the OpenAPI spec (e.g.,'dog').This causes deserialization failures in the backend that still expects the original type discriminators it has defined.
Interestingly enough, this issue wasn't present in @hey-api/openapi-ts 0.84.4, which I suppose might have been due to writable types not being generated properly with polymorphic types so this bug never surfaced itself because of that.
Reproducible example or configuration
Environment
@hey-api/openapi-ts: v0.90.10Reproduction
.NET models
Generated TS Read types (correct)
Generated TS Writable types (incorrect):
OpenAPI specification (optional)
OpenAPI Schema (Backend)
System information (optional)
No response</issue_description>
Comments on the Issue (you are @copilot in this section)
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.