First, an OpenAPI spec. This is simplified to just demonstrate what I'm trying to accomplish, all of these components are more complex in reality:
---
openapi: 3.0.0
info:
title: My API
version: 0.1.0
paths:
"/transaction":
get:
responses:
"200":
description: "hey"
content:
application/json:
schema:
$ref: "#/components/schemas/OnChainTransaction"
application/x-bcs:
schema:
$ref: "#/components/schemas/OnChainTransaction"
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/SubmitTransactionRequest'
application/x-bcs:
schema:
$ref: '#/components/schemas/SubmitTransactionRequest'
responses:
"202":
description: "hey"
content:
application/json:
schema:
$ref: '#/components/schemas/PendingTransaction'
application/x-bcs:
schema:
$ref: '#/components/schemas/PendingTransaction'
components:
schemas:
OnChainTransaction:
oneOf:
- $ref: '#/components/schemas/GenesisTransaction'
- $ref: '#/components/schemas/UserTransaction'
discriminator:
propertyName: type
GenesisTransaction:
type: string
UserTransaction:
type: string
SubmitTransactionRequest:
type: object
allOf:
- $ref: '#/components/schemas/UserTransactionRequest'
- $ref: '#/components/schemas/UserTransactionSignature'
UserTransactionRequest:
type: string
UserTransactionSignature:
type: string
PendingTransaction:
required:
- transaction
properties:
transaction:
$ref: '#/components/schemas/OnChainTransaction'
You'll note the use of x-bcs. BCS is a custom serialization method.
So with this spec, I want the client to be able to do the following:
- Send a request to the POST endpoints as either JSON or BCS (using
Content-Type). - Use
Acceptto tell the server what format to return the response in (JSON or BCS). - Use discriminators, like oneOf and allOf for both requests and responses.
- Automatically serialize and deserialize BCS within the generated client.
So far I have been unable to find a TS based OpenAPI generator that supports all of these features natively. I have tried openapi-typescript, swagger-typescript-api, and all the TS generators in openapi-generator.
How can I invoke one of these TS generators to generate a client that acknowledges that there are multiple input / output types, understands discriminator types, and allows me to inject a function to serialize and deserialize the data for BCS format? It should also not just generate interfaces, but functions for making requests and processing responses.
Other relevant links: