Skip to content

Commit

Permalink
fix(asyncapi/v3): fix servers (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenn authored May 1, 2024
1 parent 2a9cb48 commit ee8fe4a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 12 deletions.
7 changes: 1 addition & 6 deletions pkg/asyncapi/v3/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Channel struct {
Title string `json:"title"`
Summary string `json:"summary"`
Description string `json:"description"`
Servers []*Server `json:"servers"`
Servers []*Server `json:"servers"` // Reference only
Parameters map[string]*Parameter `json:"parameters"`
Tags []*Tag `json:"tags"`
ExternalDocs *ExternalDocumentation `json:"externalDocs"`
Expand Down Expand Up @@ -58,11 +58,6 @@ func (ch *Channel) generateMetadata(parentName, name string) error {
}
}

// Generate servers metadata
for i, srv := range ch.Servers {
srv.generateMetadata(ch.Name, "", &i)
}

// Generate parameters metadata
for name, param := range ch.Parameters {
param.generateMetadata(ch.Name, name)
Expand Down
2 changes: 1 addition & 1 deletion pkg/asyncapi/v3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Components) setDependencies(spec Specification) error {
func (c *Components) generateMetadataFromMaps() error {
// Generate metadata for servers
for name, entity := range c.Servers {
entity.generateMetadata("", name, nil)
entity.generateMetadata("", name)
}

// Generate metadata for channels
Expand Down
4 changes: 2 additions & 2 deletions pkg/asyncapi/v3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type Server struct {
}

// generateMetadata generates metadata for the Server.
func (srv *Server) generateMetadata(parentName, name string, number *int) {
func (srv *Server) generateMetadata(parentName, name string) {
// Prevent modification if nil
if srv == nil {
return
}

// Set name
srv.Name = generateFullName(parentName, name, "Server", number)
srv.Name = generateFullName(parentName, name, "Server", nil)

// Generate variables metadata
for n, s := range srv.Variables {
Expand Down
6 changes: 3 additions & 3 deletions pkg/asyncapi/v3/specification.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Specification struct {
Version string `json:"asyncapi"`
ID string `json:"id"`
Info Info `json:"info"`
Servers []*Server `json:"servers"`
Servers map[string]*Server `json:"servers"`
DefaultContentType string `json:"defaultContentType"`
Channels map[string]*Channel `json:"channels"`
Operations map[string]*Operation `json:"operations"`
Expand Down Expand Up @@ -105,8 +105,8 @@ func (s *Specification) generateMetadata() error {
}

// Generate servers metadata
for i, srv := range s.Servers {
srv.generateMetadata("", "Server", &i)
for name, srv := range s.Servers {
srv.generateMetadata("", name)
}

// Generate metadata for channels
Expand Down
68 changes: 68 additions & 0 deletions test/v3/issues/209/asyncapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/v3/issues/209/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
asyncapi: 3.0.0
info:
title: Hello world application
version: '0.1.0'
servers:
local:
host: localhost:9092
protocol: kafka
protocolVersion: '3.5'
description: Local Kafka broker
test:
host: test.k8s.cluster.local:9092
protocol: kafka
protocolVersion: '3.5'
description: Test environment K8S Kafka cluster
5 changes: 5 additions & 0 deletions test/v3/issues/209/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:generate go run ../../../../cmd/asyncapi-codegen -g types -p issue209 -i ./asyncapi.yaml -o ./asyncapi.gen.go

package issue209

// This is just to test that the generation is correct

0 comments on commit ee8fe4a

Please sign in to comment.