Correct TypeScript deserialization for discriminated union property types#7669
Open
jeffreybulanadi wants to merge 1 commit into
Open
Conversation
0570b32 to
89094bc
Compare
…ypes
When a model property is typed as a named oneOf schema that carries a
discriminator (e.g. WeatherForecast = oneOf[RainyDayForecast, SunnyDayForecast]
with discriminator), the generated TypeScript deserializer was incorrectly
chaining individual subtype factory calls with ??:
n.getCollectionOfObjectValues(createRainyDayForecastFromDiscriminatorValue)
?? n.getCollectionOfObjectValues(createSunnyDayForecastFromDiscriminatorValue)
The fix detects when the composed property type has basic discriminator
information and routes those properties through GetDeserializationMethodName
on the base type, producing the correct output:
n.getCollectionOfObjectValues<WeatherForecast>(createWeatherForecastFromDiscriminatorValue)
The same correction applies to single-object properties.
Closes microsoft#6615
89094bc to
7887ea4
Compare
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 model property references a named oneOf schema with a discriminator (e.g. \WeatherForecast\ defined as \oneOf: [RainyDayForecast, SunnyDayForecast]\ with \discriminator.propertyName), the TypeScript deserializer was generating incorrect ??-chained subtype factory calls instead of delegating to the base type's discriminator factory function.
Root Cause
In \CodeFunctionWriter.WritePropertyDeserializationBlock, the \GetOriginalComposedType\ branch handled all composed-type properties identically by joining individual subtype factory calls with ??. This is correct for true anonymous unions (oneOf without discriminator) but wrong for named discriminated unions, where a base-type factory (\createXFromDiscriminatorValue) already handles discriminator-based subtype resolution.
Before
\\ ypescript
"forecasts": n => { weatherSummary.forecasts = n.getCollectionOfObjectValues(createRainyDayForecastFromDiscriminatorValue) ?? n.getCollectionOfObjectValues(createSunnyDayForecastFromDiscriminatorValue); },
"primaryForecast": n => { weatherSummary.primaryForecast = n.getObjectValue(createRainyDayForecastFromDiscriminatorValue) ?? n.getObjectValue(createSunnyDayForecastFromDiscriminatorValue); },
\\
After
\\ ypescript
"forecasts": n => { weatherSummary.forecasts = n.getCollectionOfObjectValues(createWeatherForecastFromDiscriminatorValue); },
"primaryForecast": n => { weatherSummary.primaryForecast = n.getObjectValue(createWeatherForecastFromDiscriminatorValue); },
\\
Changes
Closes #6615