fix: [csharp] JsonConverter anyOf creates uncompilable code#21137
Conversation
|
thanks for the PR cc @devhl-labs |
|
I believe mappedModels is used for the discriminator. I also believe anyOf can have a discriminator so I am not sure this is the right fix. |
anyOf can have a discriminator (see my example) and that is the case when this generates uncompilable code. Right after this in the template there is a special handling for anyOf to set the correct Options. It doesnt't make sense to have the early return and then generate code after as well as it will be unreachable: If this is not the correct way to solve it, what do you propose @devhl-labs? |
|
For context here is the generated code before and after the fix: if (!fruitType.IsSet)
throw new ArgumentException("Property is required for class FruitAnyOfDisc.", nameof(fruitType));
if (fruitType.IsSet && fruitType.Value == null)
throw new ArgumentNullException(nameof(fruitType), "Property is not nullable for class FruitAnyOfDisc.");
if (appleAnyOfDisc != null)
return new FruitAnyOfDisc(appleAnyOfDisc);
if (bananaAnyOfDisc != null)
return new FruitAnyOfDisc(bananaAnyOfDisc);
throw new JsonException();
Option<AppleAnyOfDisc?> appleAnyOfDiscParsedValue = appleAnyOfDisc == null
? default
: new Option<AppleAnyOfDisc?>(appleAnyOfDisc);
Option<BananaAnyOfDisc?> bananaAnyOfDiscParsedValue = bananaAnyOfDisc == null
? default
: new Option<BananaAnyOfDisc?>(bananaAnyOfDisc);
return new FruitAnyOfDisc(appleAnyOfDiscParsedValue, bananaAnyOfDiscParsedValue); if (!fruitType.IsSet)
throw new ArgumentException("Property is required for class FruitAnyOfDisc.", nameof(fruitType));
if (fruitType.IsSet && fruitType.Value == null)
throw new ArgumentNullException(nameof(fruitType), "Property is not nullable for class FruitAnyOfDisc.");
Option<AppleAnyOfDisc?> appleAnyOfDiscParsedValue = appleAnyOfDisc == null
? default
: new Option<AppleAnyOfDisc?>(appleAnyOfDisc);
Option<BananaAnyOfDisc?> bananaAnyOfDiscParsedValue = bananaAnyOfDisc == null
? default
: new Option<BananaAnyOfDisc?>(bananaAnyOfDisc);
return new FruitAnyOfDisc(appleAnyOfDiscParsedValue, bananaAnyOfDiscParsedValue); |
|
You have a discriminator but no mapping. I think we have some issues with mappings anyway so lets merge this so you get unblocked. |
Using an anyOf with a discriminator creates uncompilable code with cshapr generichost. The issue is that the generated code tries to call a constructor with only one parameter, but the constructor will have multiple parameters.
Steps to reproduce:
Output:
@mandrean (2017/08) @shibayan (2020/02) @Blackclaws (2021/03) @lucamazzanti (2021/05) @iBicha (2023/07)
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)