References and compatibility with OpenApi
Hey, I use both async(fro streams) and open(for rest) api. And it's quite common case when it's needed to use some structure for requests and streams at the same time:
Type:
payload:
x-go-type: common.Type
x-go-type-import:
path: /my-module/path
This approach will work, but it breaks the sense of schemas - I can't use it for other languages and, for instance, front-end developers can't use it.
But official specification provide refs for such cases:
Type:
payload:
$ref: "../common/schemas/common.yaml#/components/schemas/Type"
And the case not only for open api but also for other schemas for async api e.g. when two services use the same structure.
Hello @nuttert ! Thanks for the issue !
Normally, it should have enable by this issue: https://github.com/lerenn/asyncapi-codegen/issues/192 Do you have an error when you're trying to import a schema from an openapi schema? :)
Okay I see it, thanks, but yes I could not generate a code with ref:
I created UserMeta message with payload:
I expected it will create a structure UserMeta and Payload will have the UserMeta type.
But it generated these:
(I pointed to the dependencies in the input -i ../schemas/channels_api.yaml,../../common/schemas/common.yaml)
Thanks for the example, I'll try to recreate it and solve it in the next days :)
Also I see that it is trying to parse the open API schema:
Error: json: cannot unmarshal array into Go struct field Specification.servers of type map[string]*asyncapiv3.Server
Usage:
asyncapi-codegen [flags]
Flags:
-c, --convert-keys string Schema property key names conversion strategy.
Supported values: snake, camel, kebab, none. (default "none")
-f, --disable-formatting Disables the code generation formatting
-g, --generate string Generation options (default "user,application,types")
-h, --help help for asyncapi-codegen
-i, --input strings AsyncAPI specification file to use, and its dependencies (default [asyncapi.yaml])
-o, --output string Destination file (default "asyncapi.gen.go")
-p, --package string Golang package name (default "asyncapi")
Error: json: cannot unmarshal array into Go struct field Specification.servers of type map[string]*asyncapiv3.Server
services/login/api/service.go:5: running "asyncapi-codegen": exit status 1
However, I expect it to only parse the components/headers/parameters and not the entire file.
After I removed servers field it failed with unknown error:
Error: open : no such file or directory
Usage:
asyncapi-codegen [flags]
Flags:
-c, --convert-keys string Schema property key names conversion strategy.
Supported values: snake, camel, kebab, none. (default "none")
-f, --disable-formatting Disables the code generation formatting
-g, --generate string Generation options (default "user,application,types")
-h, --help help for asyncapi-codegen
-i, --input strings AsyncAPI specification file to use, and its dependencies (default [asyncapi.yaml])
-o, --output string Destination file (default "asyncapi.gen.go")
-p, --package string Golang package name (default "asyncapi")
Error: open : no such file or directory
services/login/api/service.go:5: running "asyncapi-codegen": exit status 1
Despite the fact that open api generator validate it and correctly works with the schema.
By the way, the initial example above worked after I added x-go-type:
Sorry about the delay, I was a bit busy. Thanks for providing more insight :)
I think i had a similar issue, when using maps with more complex types other than strings.
The generator was generating invalid MarshalJSON methods.
I tried to marshal AdditionalProperties values as well which seems to fix the issue. But i'm not sure if this would be the right approach.
See: https://github.com/lerenn/asyncapi-codegen/blob/4692a76c6b01ddb3572db005d3bc3c33ed3b8bf2/pkg/codegen/generators/v3/templates/marshaling/additional_properties.tmpl#L18
Arf, thanks for proposing a fix ! I'll look ASAP