How to use OneOf with OpenAPI generator for Dart? - flutter

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:
https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/dart.md
https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/dart-dio.md
https://github.com/OpenAPITools/openapi-generator/issues/9507
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!

Related

Karate- Gatling: Not able to run scenarios based on tags

I am trying to run performance test on scenario tagged as perf from the below feature file-
#tag1 #tag2 #tag3
**background:**
user login
#tag4 #perf
**scenario1:**
#tag4
**scenario2:**
Below is my .scala file setup-
class PerfTest extends Simulation {
val protocol = karateProtocol()
val getTags = scenario("Name goes here").exec(karateFeature("classpath:filepath"))
setUp(
getTags.inject(
atOnceUsers(1)
).protocols(protocol)
)
I have tried passing the tags from command line and as well as passing the tag as argument in exec method in scala setup.
Terminal command-
mvn clean test-compile gatling:test "-Dkarate.env={env}" "-Dkarate.options= --tags #perf"
.scala update:- I have also tried passing the tag as an argument in the karate execute.
val getTags = scenario("Name goes here").exec(karateFeature("classpath:filepath", "#perf"))
Both scenarios are being executed with either approach. Any pointers how i can force only the test with tag perf to run?
I wanted to share the finding here. I realized it is working fine when i am passing the tag info in .scala file.
My scenario with perf tag was a combination of GET and POST call as i needed some data from GET call to pass in POST call. That's why i was seeing both calls when running performance test.
I did not find any reference in karate gatling documentation for passing tags in terminal execution command. So i am assuming that might not be a valid case.

Open api generator : change Client name

I am using the "OpenApi generator" to create the client library for an api.
openapi-generator generate -i https://xxxx.cloudfront.net/openapi/en-us/product/2-0/openapi.yaml -g ruby -o tmp/test/custom_name
This generate a class "OpenapiClient" but I need to change this name to "CustomApiClient". How can I pass this value?
This seems to me a standard thing to do, why is not so immediate? Am I following the best practice?
# Load the gem
require 'openapi_client'
# Setup authorization
OpenapiClient.configure do |config|
# Configure Bearer authorization: bearer
config.access_token = 'YOUR_BEARER_TOKEN'
end
This seemed way harder to find than it should have been, but if you run openapi-generator config-help -g ruby, it will show you all the options for ruby client configurations. Specifically, gemName. So you can simply create a config file like:
# config.yaml
gemName: custom_api_client
and provide -c /path/to/your/config.yaml when running the generate command.

Google Cloud AI Platform: I can't create a model version using the "--accelerator" parameter

In order to get online predictions, I'm creating a model version on the ai-platform. It works fine unless I want to use the --accelerator parameter.
Here is the command that works:
gcloud alpha ai-platform versions create [...] --model [...] --origin=[...] --python-version=3.5 --runtime-version=1.14 --package-uris=[...] --machine-type=mls1-c4-m4 --prediction-class=[...]
Here is the parameter that makes it not work:
--accelerator=^:^count=1:type=nvidia-tesla-k80
This is the error message I get:
ERROR: (gcloud.alpha.ai-platform.versions.create) INVALID_ARGUMENT: Request contains an invalid argument.
I expect it to work, since 1) the parameter exists and uses these two keys (count and type), 2) I use the correct syntax for the parameter, any other syntaxes would return a syntax error, and 3) the "nvidia-tesla-k80" value exists (it is described in --help) and is available in the region in which the model is deployed.
Make sure you are using a recent version of the Google Cloud SDK.
Then you can use the following command:
gcloud beta ai-platform versions create $VERSION_NAME \
--model $MODEL_NAME \
--origin gs://$MODEL_DIRECTORY_URI \
--runtime-version 1.15 \
--python-version 3.7 \
--framework tensorflow \
--machine-type n1-standard-4 \
--accelerator count=1,type=nvidia-tesla-t4
For reference you can enable logging during model creation:
gcloud beta ai-platform models create {MODEL_NAME} \
--regions {REGION}
--enable-logging \
--enable-console-logging
The format for the --accelerator parameter as you can check in the official documentation is:
--accelerator=count=1,type=nvidia-tesla-k80
I think that might cause your issue, let me know.

Convert CloudFormation template (YAML) to Troposphere code

I have a large sized CloudFormation template written in Yaml, I want to start using Troposphere instead. Is there any easy way to convert the CF template to Troposphere code?
I have noticed this script here https://github.com/cloudtools/troposphere/blob/master/troposphere/template_generator.py
This creates a Troposphere python object, but I am not sure if its possible to output it Troposphere code.
You can do it by converting the CF YAML to JSON and running the https://github.com/cloudtools/troposphere/blob/master/scripts/cfn2py passing the JSON file in as an argument.
Adding to the good tip from #OllieB
Install dependencies using pip or poetry:
https://python-poetry.org/docs/#installation
https://github.com/cloudtools/troposphere
https://github.com/awslabs/aws-cfn-template-flip
pip install 'troposphere[policy]'
pip install cfn-flip
poetry add -D 'troposphere[policy]'
poetry add -D cfn-flip
The command line conversion is something like:
cfn-flip -c -j template-vpc.yaml template-vpc.json
cfn2py template-vpc.json > template_vpc.py
WARNING: it appears that the cfn2py script might not be fully unit tested or something, because it can generate some code that does not pass troposphere validations. I recommend adding a simple round-trip test to the end of the output python script, e.g.
if __name__ == "__main__":
template_py = json.loads(t.to_json())
with open("template-vpc.json", "r") as json_fd:
template_cfn = json.load(json_fd)
assert template_py == template_cfn
See also https://github.com/cloudtools/troposphere/issues/1879 for an example of auto-generation of pydantic models from CFN json schemas.
from troposphere.template_generator import TemplateGenerator
import yaml
with open("aws/cloudformation/template.yaml") as f:
source_content = yaml.load(f, Loader=yaml.BaseLoader)
template = TemplateGenerator(source_content)
This snippet shall give you a template object from Troposphere library. You can then make modifications using Troposphere api.

Possible to change the package name when generating client code

I am generating the client scala code for an API using the Swagger Edtior. I pasted the json then did a Generate Client/Scala. It gives me a default root package of
io.swagger.client
I can't see any obvious way of specifying something different. Can this be done?
Step (1): Create a file config.json and add following lines and define package names:
{
"modelPackage" : "com.xyz.model",
"apiPackage" : "com.xyz.api"
}
Step (2): Now, pass the above file name along with codegen command with -c option:
$ java -jar swagger-codegen-cli.jar generate -i path/swagger.json -l java -o Code -c path/config.json
Now, it will generate your java packages like com.xyz… instead of default one io.swagger.client…
Run the following command to get information about the supported configuration options
java -jar swagger-codegen-cli.jar config-help -l scala
This will give you information about supported by this generator (Scala in this example):
CONFIG OPTIONS
sortParamsByRequiredFlag
Sort method arguments to place required parameters before optional parameters. (Default: true)
ensureUniqueParams
Whether to ensure parameter names are unique in an operation (rename parameters that are not). (Default: true)
modelPackage
package for generated models
apiPackage
package for generated api classes
Next, define a config.json file with the above parameters:
{
"modelPackage": "your package name",
"apiPackage": "your package name"
}
And supply config.json as input to swagger-codegen using the -c flag.