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
78 stars 21 forks source link

Redeclared the struct name in the same package problem #190

Closed codycoach closed 2 months ago

codycoach commented 3 months ago

Hi guys.

I found the issue when I tried to generate properties as objects in the same name. it redeclared my object property in the same package. Do you guys have any workaround for this one?

Sample Spec input

asyncapi: 3.0.0
info:
  title: Hello world application
  version: '0.1.0'
channels:
  hello:
    address: 'hello'
    messages:
      sayHello:
        payload:
          type: object
          properties:
            data:
              type: object
              properties:
                id:
                  type: string
                hello:
                  type: string
  foo:
    address: 'hello'
    messages:
      bar:
        payload:
          type: object
          properties:
            data:
              type: object
              properties:
                id:
                  type: string
                bar:
                  type: string
operations:
  receiveHello:
    action: 'receive'
    channel:
      $ref: '#/channels/hello'

Sample Output:

...
// DataProperty is a schema from the AsyncAPI specification required in messages
type DataProperty struct {
    Bar *string `json:"bar"`
    Id  *string `json:"id"`
}
...
// DataProperty is a schema from the AsyncAPI specification required in messages
type DataProperty struct {
    Hello *string `json:"hello"`
    Id    *string `json:"id"`
}
...
lerenn commented 3 months ago

Hello @codycoach ! Thanks for your interest in this project ☺️

Sorry about that, I didn't think of this when splitting struct in substructures. I will write a fix for this and I'll let you know when it will be live :)

codycoach commented 3 months ago

Hello @codycoach ! Thanks for your interest in this project ☺️

Sorry about that, I didn't think of this when splitting struct in substructures. I will write a fix for this and I'll let you know when it will be live :)

Thanks for your fast response :),

I found the workaround for this case with this spec.

asyncapi: 3.0.0
info:
  title: Hello world application
  version: '0.1.0'
channels:
  hello:
    address: 'hello'
    messages:
      sayHello:
        payload:
          type: object
          properties:
            data:
              $ref: '#/components/schemas/helloData'
  foo:
    address: 'hello'
    messages:
      bar:
        payload:
          type: object
          properties:
            data:
              $ref: '#/components/schemas/fooData'
operations:
  receiveHello:
    action: 'receive'
    channel:
      $ref: '#/channels/hello'

components:
  schemas:
    helloData:
      type: object
      properties:
        id:
          type: string
        hello:
          type: string
    fooData:
      type: object
      properties:
        id:
          type: string
        bar:
          type: string

and the output would be

...
type SayHelloMessagePayload struct {
    Data *HelloDataSchema `json:"data"`
}
...
type BarMessagePayload struct {
    Data *FooDataSchema `json:"data"`
}
...

You may close this issue and prefer this solution with covered by the document and unit test.

lerenn commented 3 months ago

Yes, it's indeed a way to solve this problem!

But I really don't like that a valid AsyncAPI document can't render valid generated code. So I think I'll just add a prefix on type with the parents name :)