Skip to content

v0.64.1: generated code does not compile for message headers with enum #343

Open

Description

@ideskov

Description

Since v0.64.1 (PR #338, "generate enum types and constants"), a message header declared as
type: string with an enum gets a named Go type for its struct field. The code that reads
headers from the broker message still converts the value with the raw schema type (string(v)),
so the generated code does not compile. Both required and optional headers are affected.

v0.63.0 generates code that compiles from the same spec.

Reproduction

asyncapi.yaml:

asyncapi: 3.0.0
info:
  title: repro
  version: 1.0.0
channels:
  events:
    address: events
    messages:
      Event:
        $ref: "#/components/messages/Event"
operations:
  consumeEvent:
    action: receive
    channel:
      $ref: "#/channels/events"
components:
  messages:
    Event:
      headers:
        type: object
        properties:
          required_kind:
            type: string
            enum: [created]
          optional_kind:
            type: string
            enum: [created]
        required:
          - required_kind
      payload:
        type: object
        properties:
          id:
            type: string
go tool asyncapi-codegen -i asyncapi.yaml -p repro -o repro.gen.go
go build ./...
./repro.gen.go:525:31: cannot use &h (value of type *string) as *OptionalKindPropertyFromHeadersFromEventMessage value in assignment
./repro.gen.go:527:31: cannot use string(v) (value of type string) as RequiredKindPropertyFromHeadersFromEventMessage value in assignment

Generated code (excerpt):

type HeadersFromEventMessage struct {
	OptionalKind *OptionalKindPropertyFromHeadersFromEventMessage `json:"optional_kind,omitempty" validate:"omitempty,oneof='created'"`
	RequiredKind RequiredKindPropertyFromHeadersFromEventMessage  `json:"required_kind" validate:"oneof='created'"`
}

// in newMessageFromBrokerMessage:
case k == "optional_kind": // Retrieving OptionalKind header
	h := string(v)
	msg.Headers.OptionalKind = &h
case k == "required_kind": // Retrieving RequiredKind header
	msg.Headers.RequiredKind = string(v)

Expected

case k == "optional_kind":
	h := OptionalKindPropertyFromHeadersFromEventMessage(v)
	msg.Headers.OptionalKind = &h
case k == "required_kind":
	msg.Headers.RequiredKind = RequiredKindPropertyFromHeadersFromEventMessage(v)

Versions

  • asyncapi-codegen v0.64.1 (v0.63.0 works)
  • AsyncAPI 3.0.0
  • go1.26.5 darwin/arm64

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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