Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redeclared the struct name in the same package problem #190

Closed
codycoach opened this issue Apr 7, 2024 · 3 comments · Fixed by #195
Closed

Redeclared the struct name in the same package problem #190

codycoach opened this issue Apr 7, 2024 · 3 comments · Fixed by #195
Assignees
Labels
bug Something isn't working

Comments

@codycoach
Copy link

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
Copy link
Owner

lerenn commented Apr 7, 2024

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

@lerenn lerenn added the bug Something isn't working label Apr 7, 2024
@lerenn lerenn self-assigned this Apr 7, 2024
@codycoach
Copy link
Author

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
Copy link
Owner

lerenn commented Apr 9, 2024

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

@lerenn lerenn linked a pull request Apr 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants