microsoft / microsoft/typespec
Add diagnostic for `@discriminator` applied to a model without the specified discriminator property defined directly
- Dominant language
- Java
- Stars
- 5.9k
- Forks
- 394
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 104
Description
Thransferred from: https://github.com/Azure/typespec-azure/issues/3236
The following tsp is likely not what the author intended:
```
model Foo {
discriminatorProp: string;
fooProp: string;
}
@discriminator("discriminatorProp")
model Bar extends Foo {
bazProp: string;
}
model ChildModel extends Bar {
discriminatorProp: "child"
// other props
}
```
Since Foo is not decorated with the discriminator, any operations returning Foo should not be deserialized into the derived models. Also, any operations returning Foo will not cause a Usage to be inferred for any child types. This is most likely a mistake in the spec - https://github.com/Azure/azure-rest-api-specs/blob/main/specification/ai/HealthInsights/HealthInsights.Common/model.common.fhir.resources.tsp#L36. See the base model `resource` which defines the resourceType property but not as a discriminator. This can't simply be fixed without incurring breaking changes in various languages. We should add a linter to prevent this antipattern in the future. If there is a valid use case for this, the author can explain in the linter suppression.
Contributor guide
Assessment
This issue has not been assessed yet.