Skip to content

Commit

Permalink
fix(test/v3/issues/220): fix asyncapi v3 feature & test (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenn authored Jun 9, 2024
1 parent 6c7352a commit 931df74
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 317 deletions.
2 changes: 1 addition & 1 deletion pkg/asyncapi/v3/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Components) generateMetadata() error {

// Generate schemas metadata
for name, schema := range c.Schemas {
if err := schema.generateMetadata("", name+"Schema", nil, false); err != nil {
if err := schema.generateMetadata("", name+"_Schema", nil, false); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/asyncapi/v3/correlation_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *CorrelationID) generateMetadata(parentName, name string) {
}

// Set name
c.Name = generateFullName(parentName, name, "CorrelationID", nil)
c.Name = generateFullName(parentName, name, "Correlation_ID", nil)
}

// setDependencies sets dependencies between the different elements of the CorrelationID.
Expand Down
2 changes: 1 addition & 1 deletion pkg/asyncapi/v3/external_documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package asyncapiv3

const (
// ExternalDocsNameSuffix is the suffix that is added to the name of external docs.
ExternalDocsNameSuffix = "ExternalDocs"
ExternalDocsNameSuffix = "External_Docs"
)

// ExternalDocumentation is a representation of the corresponding asyncapi object filled
Expand Down
2 changes: 1 addition & 1 deletion pkg/asyncapi/v3/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (msg *Message) generateMetadata(parentName, name string, number *int) error

// Generate Payload metadata
// NOTE: Suffix Message name with payload for name and set no parent
if err := msg.Payload.generateMetadata("", msg.Name+"Payload", nil, false); err != nil {
if err := msg.Payload.generateMetadata("", msg.Name+"_Payload", nil, false); err != nil {
return err
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/asyncapi/v3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,68 +93,68 @@ func (s *Schema) generateMetadata(parentName, name string, number *int, isRequir

// Generate Properties metadata
for n, p := range s.Properties {
if err := p.generateMetadata(s.Name, n+"Property", nil, utils.IsInSlice(s.Required, n)); err != nil {
if err := p.generateMetadata(s.Name, n+"_Property", nil, utils.IsInSlice(s.Required, n)); err != nil {
return err
}
}

// Generate Pattern Properties metadata
for n, p := range s.PatternProperties {
if err := p.generateMetadata(s.Name, n+"PatternProperty", nil, utils.IsInSlice(s.Required, n)); err != nil {
if err := p.generateMetadata(s.Name, n+"_Pattern_Property", nil, utils.IsInSlice(s.Required, n)); err != nil {
return err
}
}

// Generate AdditionalProperties metadata
if s.AdditionalProperties != nil {
if err := s.AdditionalProperties.generateMetadata(s.Name, "AdditionalProperties", nil, false); err != nil {
if err := s.AdditionalProperties.generateMetadata(s.Name, "Additional_Properties", nil, false); err != nil {
return err
}
}

// Generate Additional Items metadata
for i, item := range s.AdditionalItems {
if err := item.generateMetadata(s.Name, "AdditionalItem", &i, false); err != nil {
if err := item.generateMetadata(s.Name, "Additional_Item", &i, false); err != nil {
return err
}
}

// Generate Items metadata
// NOTE: give the name of the parent to the items
if err := s.Items.generateMetadata("", "ItemFrom"+s.Name, nil, false); err != nil {
if err := s.Items.generateMetadata("", "Item_From_"+s.Name, nil, false); err != nil {
return err
}

// Generate Contains metadata
for i, item := range s.Contains {
if err := item.generateMetadata("", s.Name+"Content", &i, false); err != nil {
if err := item.generateMetadata("", s.Name+"_Content", &i, false); err != nil {
return err
}
}

// Generate AnyOf metadata
for _, v := range s.AnyOf {
if err := v.generateMetadata(s.Name, "AnyOf", nil, false); err != nil {
if err := v.generateMetadata(s.Name, "Any_Of", nil, false); err != nil {
return err
}
}

// Generate OneOf metadata
for _, v := range s.OneOf {
if err := v.generateMetadata(s.Name, "OneOf", nil, false); err != nil {
if err := v.generateMetadata(s.Name, "One_Of", nil, false); err != nil {
return err
}
}

// Generate AllOf metadata
for _, v := range s.AllOf {
if err := v.generateMetadata(s.Name, "AllOf", nil, false); err != nil {
if err := v.generateMetadata(s.Name, "All_Of", nil, false); err != nil {
return err
}
}

// Generate Not metadata
if err := s.Not.generateMetadata(s.Name, "NotSchema", nil, false); err != nil {
if err := s.Not.generateMetadata(s.Name, "Not_Schema", nil, false); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/asyncapi/v3/security_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *SecurityScheme) generateMetadata(parentName, name string, number *int)
}

// Set name
s.Name = generateFullName(parentName, name, "SecurityScheme", number)
s.Name = generateFullName(parentName, name, "Security_Scheme", number)
}

func (s *SecurityScheme) setDependencies(spec Specification) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/asyncapi/v3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func generateFullName(parentName, name, typeName string, number *int) string {

// If number is nil, add number to type
if number != nil {
typeName += fmt.Sprintf("%d", *number)
typeName += fmt.Sprintf("_%d", *number)
}

// If there is a parent name, prefix it with a "From"
if parentName != "" {
parentName = "From" + parentName
parentName = "From_" + parentName
}

// Return the name with the number
return template.Namify(name + typeName + parentName)
return template.Namify(name + "_" + typeName + "_" + parentName)
}
2 changes: 1 addition & 1 deletion pkg/codegen/generators/v3/templates/schema_definition.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
{{- /* ----------------------------- Object ----------------------------- */ -}}
{{- if eq .Type "object" -}}

type {{ .Name }} struct {
type {{ namify .Name }} struct {
{{- range $key, $value := .Properties -}}
{{if $value.Description}}
// Description: {{multiLineComment $value.Description}}
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Args(vs ...any) []any {
// CutSuffix is a function used to remove a suffix to a string.
func CutSuffix(s, suffix string) string {
s, _ = strings.CutSuffix(s, suffix)
s, _ = strings.CutSuffix(s, "_"+suffix)
return s
}

Expand Down
17 changes: 13 additions & 4 deletions test/v3/issues/220/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
asyncapi: 2.6.0
asyncapi: 3.0.0
info:
title: Sample App
version: 1.2.3

channels:
v2.issue220.test:
subscribe:
message:
testing:
address: v3.issue220.test
messages:
testingEvent:
payload:
$ref: '#/components/schemas/TEST'

operations:
handlingTesting:
action: receive
channel:
$ref: '#/channels/testing'
message:
$ref: '#/channels/testing/messages/testingEvent'

components:
schemas:
TEST:
Expand Down
Loading

0 comments on commit 931df74

Please sign in to comment.