openapi-to-cadl@0.4.0 does not add the discriminator field in the base class
- Dominant language
- TypeSpec
- Stars
- 4.8k
- Forks
- 736
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 1
Description
openapi-to-cadl@0.4.0 does not add the discriminator field in the base class. In this example, it is the `kind: string` that is missing from `model Pet`. This prevents autorest.csharp from generating code.
Ideally the openapi-to-cadl can roundtrip this example from https://github.com/microsoft/typespec/issues/1900.
``` tsp
@discriminator("kind")
model Pet {
kind: string;
}
model Cat extends Pet {
kind: "cat";
moew: boolean;
}
model Dog extends Pet {
kind: "dog";
bark: boolean;
}
```
```
PS C:\Users\cataggar\ms\TypeSpec-test> autorest --openapi-to-cadl --use=@autorest/openapi-to-cadl@0.4.0 --input-file=openapi.yaml
AutoRest code generation utility [cli version: 3.6.3; node: v18.15.0]
(C) 2018 Microsoft Corporation.
https://aka.ms/autorest
info | AutoRest core version selected from configuration: ^3.7.0.
info | Loading AutoRest core 'C:\Users\cataggar\.autorest\@autorestcore@3.9.5\nodemodules\@autorest\core\dist' (3.9.5)
info | Installing AutoRest extension '@autorest/openapi-to-cadl' (0.4.0 -> 0.4.0)
installing... [========================================] 100% | 231/231
info | Installed AutoRest extension '@autorest/openapi-to-cadl' (0.4.0->0.4.0)
info | Loading AutoRest extension '@autorest/modelerfour' (^4.23.5->4.23.5)
warning | PreCheck/CheckDuplicateSchemas | Checking for duplicate schemas, this could take a (long) while. Run with --verbose for more detail.
warning | PreCheck/PropertyRedeclaration | Schema 'Cat' has a property 'kind' that is conflicting with a property in the parent schema 'Pet' differs more than just description : [enum => 'cat']
warning | PreCheck/PropertyRedeclaration | Schema 'Dog' has a property 'kind' that is conflicting with a property in the parent schema 'Pet' differs more than just description : [enum => 'dog']
info | transformOperationGroup: Transforming object Cat
info | getDiscriminatorProperty: Transforming property kind of type choice
info | getDiscriminatorProperty: Transforming property moew of type boolean
info | getDiscriminatorProperty: Getting discriminator property for Cat
info | getDiscriminatorProperty: Getting discriminator property for Pet
info | transformOperationGroup: Transforming object Pet
info | transformOperationGroup: Transforming object Dog
info | getDiscriminatorProperty: Transforming property kind of type choice
info | getDiscriminatorProperty: Transforming property bark of type boolean
info | getDiscriminatorProperty: Getting discriminator property for Dog
info | getDiscriminatorProperty: Getting discriminator property for Pet
info | Autorest completed in 6.46s. 5 files generated.
```
Generated output:
``` tsp
import "@typespec/rest";
import "@typespec/http";
using TypeSpec.Rest;
using TypeSpec.Http;
namespace (title);
enum CatKind {
"cat"
}
enum DogKind {
"dog"
}
model Cat extends Pet {
"kind": CatKind;
"moew": boolean;
"kind": "cat";
}
@discriminator("kind")
model Pet {
}
model Dog extends Pet {
"kind": DogKind;
"bark": boolean;
"kind": "dog";
}
```
Contributor guide
Research direction
Reproduce the issue with the provided openapi.yaml example using AutoRest and @autorest/openapi-to-cadl@0.4.0. Inspect the converter's discriminator and base-model handling, then compare the generated TypeSpec output with the expected Pet, Cat, and Dog models; done means the base discriminator field is present and the generated output supports the stated roundtrip.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100