fix(csharp): scan webhooks for operation-input models
In the csharp client with library=generichost, a oneOf model that is referenced only from a top-level webhooks operation gets internal constructors (the ones that take a oneOf member), although the generated API exposes the model in a public method signature. Code outside the generated assembly therefore cannot construct the request body (new AnimalChoice(cat) does not compile there).
webhooks is the OpenAPI 3.1 field for requests that the API provider sends to the consumer. generichost turns each webhook operation into an API method, like a path operation (in the fixture below, which has no tags, the method lands in DefaultApi).
Reproducer
modules/openapi-generator/src/test/resources/3_1/csharp/webhook-only-model.yaml (added by this PR): no paths at all and one webhook, petChanged (operationId: onPetChanged), whose request body is $ref: '#/components/schemas/AnimalChoice'; AnimalChoice is a oneOf of Cat and Dog. The file declares openapi: 3.1.0 because webhooks is a 3.1 field; nothing else in it is 3.1-specific.
Generated with -g csharp --library generichost. On master, Model/AnimalChoice.cs has
internal AnimalChoice(Cat cat)
...
internal AnimalChoice(Dog dog)
while Api/DefaultApi.cs declares, in public interface IDefaultApi (and implements as a public async method in public sealed partial class DefaultApi):
Task<IOnPetChangedApiResponse> OnPetChangedAsync(AnimalChoice animalChoice, System.Threading.CancellationToken cancellationToken = default);
So a caller outside the assembly cannot build the argument of that public method with new.
The cause: AbstractCSharpCodegen.getOperationInputModels() collects the models reachable from operation parameters and request bodies and marks them with the vendor extension x-model-is-operation-input. generichost/modelGeneric.mustache uses the mark to choose between {{>visibility}} (public, or internal when nonPublicApi is set) and internal for the oneOf constructors. The method only walked openAPI.getPaths(), so models reachable only from webhooks were never marked.
Fix
getOperationInputModels() now collects the PathItems of openAPI.getPaths() and of openAPI.getWebhooks() (both null-safe) and runs the existing traversal over all of them. It no longer returns early when getPaths() is null, so a spec that has only webhooks is scanned too.
After regenerating, the same lines read public AnimalChoice(Cat cat) and public AnimalChoice(Dog dog).
Behavior change
For oneOf models reachable only through webhook operations, the constructors that take a oneOf member get {{>visibility}} (public unless nonPublicApi=true) instead of internal. Other constructors and non-oneOf models are not affected. Models that are also referenced from paths are unaffected. generichost/modelGeneric.mustache is the only template that reads x-model-is-operation-input, so no other C# library or generator changes.
Tests
| Test | Asserts |
|---|---|
testGenerichostWebhookModelGetsPublicCtor (new) | the parsed spec has no paths (so the null-paths branch is exercised); generating webhook-only-model.yaml with generichost produces Model/AnimalChoice.cs that contains public AnimalChoice( and does not contain internal AnimalChoice( |
./mvnw -nsu -Denforcer.skip=true -pl modules/openapi-generator -am -Dtest=CSharpClientCodegenTest -Dsurefire.failIfNoSpecifiedTests=false test
| Tests run | Result | |
|---|---|---|
| master + this PR | 31 | 0 failures, 0 skipped |
this PR with the AbstractCSharpCodegen.java change reverted (test kept; run limited to the new test) | 1 | 1 failure ā testGenerichostWebhookModelGetsPublicCtor |
Samples
No sample changed. The only csharp config whose input spec declares webhooks is bin/configs/csharp-generichost-latest-helloWorld.yaml (3_1/csharp/hello-world.yaml); its webhook payload is an inline object schema, not a $ref to a oneOf model. Regenerating it with the CLI built from this branch gives a src/Org.OpenAPITools identical to the committed samples/client/petstore/csharp/generichost/latest/HelloWorld (built by samples-dotnet10.yaml).
cc @wing328 and the C# technical committee: @mandrean @shibayan @Blackclaws @lucamazzanti
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples ā scoped to this change: built the CLI (
./mvnw -pl modules/openapi-generator-cli -am package -DskipTests) and regenerated the onlycsharpconfig whose spec has webhooks (csharp-generichost-latest-helloWorld.yaml) with it;src/Org.OpenAPIToolsis identical to the committed sample, so no sample change is committed.export_docs_generators.shnot run: no generator option or metadata changes.
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
š¤ Generated with Claude Code
Summary by cubic
Fixes the csharp generichost client so oneOf models referenced only by webhooks operations get public constructors.
Previously, getOperationInputModels() scanned only openAPI.getPaths(), so a model reachable solely from a webhook was never marked as an operation input, and its oneOf constructors were generated internal even though the public API exposes the model ā callers outside the assembly could not construct the request body with new. The scan now also traverses openAPI.getWebhooks() and no longer skips specs where paths is null, covering webhooks-only 3.1 documents. Models also referenced from paths are unaffected, as are non-oneOf models.
Adds a webhooks-only fixture and a test asserting the oneOf model's constructors are generated public.