Skip to content

[BUG] Inconsistent type naming for message payloads when referencing schemas from allOf references #290

@lmendes86

Description

@lmendes86

Description

When generating code, there's an inconsistency in how message payload types are named when allOf is used. The generated message struct directly references the schema type instead of creating a properly named message payload type, which can cause compilation errors in certain AsyncAPI specifications.

Current behavior

Given an AsyncAPI spec with a message that references a schema:

  • Message: TestCreated
  • Schema reference: #/components/schemas/TestCreatedEvent

The generator creates:

type TestCreatedMessage struct {
	// Payload will be inserted in the message payload
	Payload TestCreatedMessagePayload
}

But the TestCreatedMessagePayload struct is not generated.

Expected behavior

The generator should use the schema type directly but with consistent naming across the codebase.

Steps to reproduce

  1. Create an AsyncAPI specification file test.yml:
asyncapi: "2.5.0"
info:
  title: Test API
  version: "1.0.0"

channels:
  test.message.created:
    subscribe:
      message:
        $ref: "#/components/messages/TestCreated"

components:
  messages:
    TestCreated:
      name: testCreated
      title: Test message
      payload:
        $ref: "#/components/schemas/TestCreatedEvent"

  schemas:
    Envelope:
      type: object
      discriminator: name
      properties:
        uuid:
          type: string
          format: uuid
          description: Unique identifier of the message.
        time:
          type: string
          format: date-time
          description: Millisecond precision UTC timestamp.
          example: '2022-07-14T09:00:17.458226Z'
      required:
        - uuid
        - time
        - name
        - payload

    TestCreatedEvent:
      allOf:
        - $ref: '#/components/schemas/Envelope'
        - type: object
          properties:
            payload:
              $ref: '#/components/schemas/TestEvent'

    TestEvent:
      type: object
      properties:
        id:
          type: string
        name:
          type: string
      required:
        - id
        - name
  1. Generate code:
asyncapi-codegen -i test.yml -p testpkg -o test.go -g types
  1. Observe that TestCreatedMessage struct references TestCreatedMessagePayload directly

Impact

This inconsistency causes issues when:

  1. Multiple tools or generators expect standardized naming conventions
  2. The AsyncAPI spec evolves and message payload types need to be differentiated from schema types
  3. Code generation is part of an automated pipeline that expects consistent type naming

Environment

  • asyncapi-codegen version: v0.46.3 (also reproduced in v0.45.3)
  • Go version: 1.21
  • OS: Linux

Possible Solution

The issue likely stems from how the generator resolves schema references for message payloads. The code should check if a message payload references a schema and create appropriate type aliases or use consistent naming conventions throughout the generated code.

Additional Context

This was discovered when generating code for multiple AsyncAPI specifications in a microservices environment where consistent type naming is critical for code compilation and maintenance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions