microsoft / microsoft/typespec

Add diagnostic for `@discriminator` applied to a model without the specified discriminator property defined directly

Open
#8,518 0 comments 0 reactions 0 assignees View on GitHub
compiler:core feature triaged:core
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.