Skip to content

feat(motoko): add Motoko client generator#23544

Draft
ggreif wants to merge 11 commits intoOpenAPITools:masterfrom
ggreif:motoko-client
Draft

feat(motoko): add Motoko client generator#23544
ggreif wants to merge 11 commits intoOpenAPITools:masterfrom
ggreif:motoko-client

Conversation

@ggreif
Copy link
Copy Markdown

@ggreif ggreif commented Apr 14, 2026

Summary

This PR adds a Motoko client generator for OpenAPI Generator, targeting the Internet Computer (IC) platform.

Motoko is the native smart-contract language for the IC. The generated client makes HTTP outcalls from IC canisters using the Management Canister's http_request API, and uses the serde Mops package for JSON serialization/deserialization.

Files included

Generator implementation

  • modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/MotokoClientCodegen.java

Mustache templates (6 files)

  • modules/openapi-generator/src/main/resources/motoko/api.mustache
  • modules/openapi-generator/src/main/resources/motoko/model.mustache
  • modules/openapi-generator/src/main/resources/motoko/enum.mustache
  • modules/openapi-generator/src/main/resources/motoko/Config.mustache
  • modules/openapi-generator/src/main/resources/motoko/README.mustache
  • modules/openapi-generator/src/main/resources/motoko/mops.toml.mustache

Tests

  • modules/openapi-generator/src/test/java/org/openapitools/codegen/motoko/MotokoClientCodegenTest.java
  • modules/openapi-generator/src/test/java/org/openapitools/codegen/motoko/MotokoClientCodegenModelTest.java
  • modules/openapi-generator/src/test/java/org/openapitools/codegen/motoko/MotokoClientCodegenOptionsTest.java
  • modules/openapi-generator/src/test/java/org/openapitools/codegen/options/MotokoClientCodegenOptionsProvider.java

Registration & sample configs

  • modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig — single-line addition registering MotokoClientCodegen
  • docs/generators.md — single-line addition listing motoko (beta) in the client generators section
  • bin/configs/motoko-petstore-new.yaml — petstore sample config (with dfx)
  • bin/configs/motoko-petstore-nodfx.yaml — petstore sample config (without dfx)

Validation

Generation was tested against the standard petstore spec (modules/openapi-generator/src/test/resources/3_0/petstore.yaml). The generator produced:

  • src/Apis/PetApi.mo, StoreApi.mo, UserApi.mo — API client modules
  • src/Models/Pet.mo, Order.mo, User.mo, etc. — model types with JSON serialization
  • src/Config.mo — canister configuration
  • mops.toml, README.md — project metadata

The generated .mo files include correct IC Management Canister HTTP outcall types, proper Motoko module structure, and serde-based JSON encoding/decoding.

Generator features

  • Generates IC canister-compatible HTTP client code in Motoko
  • Supports both replicated and non-replicated query calls
  • Configurable: useDfx option to generate dfx deployment configuration
  • Handles enums, optional fields, arrays, and nested models
  • Uses mops package manager conventions

🤖 Generated with Claude Code


Summary by cubic

Adds a new motoko client generator for Internet Computer canisters using Management Canister http_request and serde JSON. Includes a committed Petstore sample and icp.yaml scaffolding via a new useIcp option alongside useDfx.

  • New Features

    • MotokoClientCodegen with templates: api, model, enum, Config, README, mops.toml, icp.yaml.
    • IC HTTP outcalls (replicated/non‑replicated), transform hook, shared Config; auth: bearer, API key, basic.
    • Options: useDfx and useIcp; both enable useImportedInterface to import ic:aaaaa-aa (inline types when neither).
    • serde JSON for enums/oneOf, arrays, maps, optionals; auto‑convert non‑negative Int to Nat.
    • Registered generator and docs at docs/generators/motoko.md; Petstore sample at samples/client/petstore/motoko; sample config at bin/configs/motoko-petstore.yaml; unit tests added.
  • Bug Fixes

    • Enforce mutual exclusivity for useDfx and useIcp (throws if both); CLI help and docs updated.
    • Cleaned up bin/configs/motoko-petstore.yaml to remove -nodfx suffixes in outputDir and artifactId.

Written for commit cfc8e66. Summary will update on new commits.

ggreif and others added 11 commits April 14, 2026 18:18
Adds a Motoko OpenAPI client generator targeting the Internet Computer
(IC) platform. Includes the codegen Java class, 6 Mustache templates,
unit tests, service-loader registration, generators.md listing, and
petstore sample configs for validation.
- Add Apache 2.0 license header
- Add @OverRide on getTag/getName/getHelp
- Add GeneratorMetadata with Stability.BETA
- Add modifyFeatureSet() declaring supported features
- Remove dead swagger-v1 imports (ArrayProperty, MapProperty, Property,
  Parameter) and unused StringUtils
- Add missing meta/features imports
- Move service-loader entry to correct alphabetical position (after
  MysqlSchemaCodegen, before N4jsClientCodegen)
- Add docs/generators/motoko.md following standard generator doc format

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Adds a new `useIcp` boolean generator option (default: false) as a
canary for the icp-cli tool that replaces dfx:

- When useIcp=true, generates an icp.yaml scaffold configured with the
  @dfinity/motoko recipe pointing at the consumer canister's main.mo
- README cycles section switches to icp-cli commands (local network
  auto-seeds cycles; mainnet via icp deploy -e ic)
- api.mustache: remove dfx-specific wording from PUT/DELETE TODO comment
  (both dfx and icp-cli use moc for ic:aaaaa-aa resolution)
- useDfx flag and behaviour unchanged

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Introduce a computed `useImportedInterface` flag (true when useDfx OR
useIcp) so that both toolchain paths import all types from ic:aaaaa-aa
instead of re-declaring them inline.

icp-cli's @dfinity/motoko recipe uses the full management canister
interface (including PUT/DELETE). dfx lags behind — but that is dfx's
issue, not something the generator template should paper over.

The inline fallback (neither flag set) keeps the hand-written interface
with full PUT/DELETE support.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Throw IllegalArgumentException in processOpts() when both flags are
set. Update CLI option descriptions to document the constraint.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Generated from motoko-petstore-nodfx.yaml using the petstore OpenAPI spec.
Provides the committed reference output required by the new-generator checklist.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…motoko

Drop the -nodfx suffix — it's the canonical Motoko petstore sample.
Also rename the config from motoko-petstore-nodfx.yaml to motoko-petstore.yaml.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…unavailable)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…had -nodfx suffix)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

1 participant