lerenn / asyncapi-codegen

An AsyncAPI Golang Code generator that generates all Go code from the broker to the application/user. Just plug your application to your favorite message broker!
Apache License 2.0
76 stars 21 forks source link

References and compatibility with OpenApi #224

Open nuttert opened 1 month ago

nuttert commented 1 month ago

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.

lerenn commented 1 month ago

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? :)

nuttert commented 1 month ago

Okay I see it, thanks, but yes I could not generate a code with ref: I created UserMeta message with payload:

image

I expected it will create a structure UserMeta and Payload will have the UserMeta type.

But it generated these:

image

(I pointed to the dependencies in the input -i ../schemas/channels_api.yaml,../../common/schemas/common.yaml)

lerenn commented 1 month ago

Thanks for the example, I'll try to recreate it and solve it in the next days :)

nuttert commented 3 weeks ago

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.

nuttert commented 3 weeks ago

By the way, the initial example above worked after I added x-go-type:

image
lerenn commented 2 weeks ago

Sorry about the delay, I was a bit busy. Thanks for providing more insight :)