How to use OneOf with OpenAPI generator for Dart?

Viewed 402

I'm trying to use the OpenAPI generator for Dart (in particular dart-dio-next). First, some code and tooling invocation snippets:

API specification: https://raw.githubusercontent.com/aptos-labs/aptos-core/main/api/doc/openapi.yaml. In particular I'm focusing on TransactionPayload right now.

OpenAPI generator version:

$ openapi-generator --version
openapi-generator-cli 5.4.0

CLI invocation:

openapi-generator generate \
  -i https://raw.githubusercontent.com/aptos-labs/aptos-core/main/api/doc/openapi.yaml \
  -g dart-dio-next \
  -c openapi-generator.yaml \
  --enable-post-process-file \

Full script: https://gist.github.com/banool/e22d94025ecd713f1596285bc6661449.

openapi-generator.yaml:

# Unrelated fields like author omitted
pubName: aptos_api_dart
pubLibrary: "aptos_api_dart"
legacyDiscriminatorBehavior: true
# disallowAdditionalPropertiesIfNotPresent: false
useEnumExtension: true

Here are some potentially relevant docs / issues I've found:

Now, when I'm trying to use the client I'm doing this:

// Build a transaction request.
TransactionPayloadBuilder transactionPayloadBuilder =
    TransactionPayloadBuilder()
      ..writeSet = (WriteSetBuilder())
      ..type = "script_function_payload"
      ..function_ = "0x1::Coin::transfer"
      ..typeArguments = ListBuilder(["0x1::TestCoin::TestCoin"])
      ..arguments = ListBuilder(
          [otherAddress.withPrefix(), "100"].map((e) => JsonObject(e)));
SubmitTransactionRequest submitTransactionRequest =
    (SubmitTransactionRequestBuilder()
          ..sender = account.address.withPrefix()
          ..payload = transactionPayloadBuilder)
        .build();

// Submit the transaction.
await client
    .getTransactionsApi()
    .submitTransaction(submitTransactionRequest: submitTransactionRequest);
});

But I get an error like this:

$ flutter test
00:02 +4 -1: /Users/dport/github/aptos_sdk_dart/test/full_library_test.dart: client account e2e test [E]
  Tried to build class "SubmitTransactionRequest" but nested builder for field "payload" threw: Tried to build class "TransactionPayload" but nested builder for field "code" threw: Tried to construct class "MoveScript" with null field "bytecode". This is forbidden; to allow it, mark "bytecode" with @nullable.
  package:aptos_api_dart/src/model/submit_transaction_request.g.dart 221:9  SubmitTransactionRequestBuilder.build
  test/full_library_test.dart 51:14                                         main.<fn>

The field it's talking about is not a required member of the variant of TransactionPayloadBuilder that I'm trying to use, ScriptFunctionPayload, and the server would know this based on type being the discriminator I believe. Naturally instead I should just build ScriptFunctionPayload, since that correctly requires only the necessary fields, except I can't pass this in to submitTransaction, because it expects a TransactionPayloadBuilder. Is there some way to make it generate a version of signTransaction that accepts any of the variants?

You can see what it generates here: https://github.com/banool/aptos_api_dart.

Assume I don't have control over the openapi spec and can't change it in any way.

Any help with how to use this correctly would be very much appreciated.

Thanks!

0 Answers
Related