Skip to content

Add C# exporter backed by the .NET WASM converter#107

Open
flobernd wants to merge 5 commits into
mainfrom
csharp-exporter
Open

Add C# exporter backed by the .NET WASM converter#107
flobernd wants to merge 5 commits into
mainfrom
csharp-exporter

Conversation

@flobernd

Copy link
Copy Markdown
Member

Summary

Adds C# as a supported output format. Code generation runs inside a .NET WebAssembly bundle shipped separately as the optional @elastic/request-converter-dotnet package, which embeds the Elastic.Clients.Elasticsearch client. The new CSharpExporter loads that bundle lazily and delegates through the existing ExternalExporter JSON contract.

Intention

Bring C# to parity with the other languages generated from Dev Console input, including the language examples in elasticsearch-specification. Because the .NET converter compiles the whole client in, it cannot live as templates in this repo; it ships per minor version as its own npm package (built and published from elastic/elasticsearch-net#8931), and consumers install the version matching their target Elasticsearch version.

Changes

  • CSharpExporter (src/exporters/csharp.ts), registered as csharp/c# and listed as C#. Defaults produce fluent descriptor syntax with a strongly typed illustrative document type (MyDocument); caller options override.
  • Bundle resolution order: explicit constructor path, then the CSHARP_REQUEST_CONVERTER_BUNDLE environment variable, then the @elastic/request-converter-dotnet package (declared as an optional peerDependency, >=8.19.0).
  • Fixture-based unit tests plus an integration test against a real bundle, gated on CSHARP_REQUEST_CONVERTER_BUNDLE.
  • The demo keeps JavaScript as its default language; C# appears in the dropdown.
  • Jest now runs under node --experimental-vm-modules, required to dynamically import the ESM bundle from this CommonJS package. Expect Node's ExperimentalWarning on stderr during test runs.
  • README documents the csharp format and its snake_case options.

Notes

  • Without the bundle installed, converting to C# fails with a clear install hint; nothing else in the package is affected. Loading is fully lazy, and the dynamic-import shim is only created when the bundle actually loads, so CSP-restricted embedders that never select C# are unaffected.
  • complete and printResponse are currently no-ops for C# (not yet implemented by the bundle), and checkOnly rejects with the install hint when the bundle is missing instead of returning false.
  • The bundle package must be published before the optional peer resolves from the registry; until then, tests use the fixture and the env var path.

flobernd added 5 commits July 22, 2026 15:35
CSharpExporter wraps ExternalExporter to load the optional
@elastic/request-converter-dotnet package, resolving the bundle from a
constructor argument, the CSHARP_REQUEST_CONVERTER_BUNDLE env var, or the
package name in that order. It defaults to descriptor syntax with a
strongly typed document, overridable per call.

Also switches the test script to run Jest under Node's
--experimental-vm-modules: the WASM bundle is ESM-only, so loading it
needs a true dynamic import that survives tsc's downlevel to require()
under the commonjs module target, and Jest's VM sandbox rejects dynamic
import() without that flag.

@JoshMock JoshMock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! LGTM.

Comment thread src/exporters/csharp.ts
Comment on lines +25 to +31
function getDynamicImport(): DynamicImport {
dynamicImport ??= new Function(
"specifier",
"return import(specifier)",
) as DynamicImport;
return dynamicImport;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever. 😮

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants