Skip to content

Commit

Permalink
APIT-982 support tags retrieval from openapi specs (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthAnz authored Nov 14, 2024
1 parent 9715f7d commit 5c7f038
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/importer/openapi3_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ func (o *OpenAPI3Importer) convertSpec(spec *openapi3.T) (string, error) {
return result.String(), err
}

// getTagNames extracts names from OpenAPI tags
func getTagNames(tags openapi3.Tags) []string {
names := make([]string, len(tags))
for i, tag := range tags {
names[i] = tag.Name
}
return names
}

func (o *OpenAPI3Importer) buildSyslInfo(spec *openapi3.T, basepath string) SyslInfo {
info := SyslInfo{
OutputData: OutputData{
Expand All @@ -166,6 +175,7 @@ func (o *OpenAPI3Importer) buildSyslInfo(spec *openapi3.T, basepath string) Sysl
Title: spec.Info.Title,
Description: spec.Info.Description,
OtherFields: []string{},
OpenapiTags: getTagNames(spec.Tags),
}
values := []string{
"version", spec.Info.Version,
Expand All @@ -188,6 +198,7 @@ func (o *OpenAPI3Importer) buildSyslInfo(spec *openapi3.T, basepath string) Sysl
info.OtherFields = append(info.OtherFields, key, val)
}
}

return info
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/importer/tests/openapi2/tags.sysl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
##########################################
## ##
## AUTOGENERATED CODE -- DO NOT EDIT! ##
## ##
##########################################

TestApp "foo" [package="com.example.package"]:
@openapi_tags = ["test1", "test \"2\""]
@description =:
| No description.

#---------------------------------------------------------------------------
# definitions
10 changes: 10 additions & 0 deletions pkg/importer/tests/openapi2/tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
swagger: "2.0"
info:
title: foo
tags:
- name: test1
description: test1 description
- name: test "2"
description: test2 description
paths:
definitions:
8 changes: 8 additions & 0 deletions pkg/importer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type SyslInfo struct {
Title string
Description string
OtherFields []string // Ordered key, val pair
OpenapiTags []string `json:"openapi_tags,omitempty" yaml:"openapi_tags,omitempty"`
}

type MethodEndpoints struct {
Expand Down Expand Up @@ -103,6 +104,13 @@ func (w *writer) writeHeader(info SyslInfo) error {
w.writeLines(fmt.Sprintf("@%s = %s", key, quote(val)))
}
}
if len(info.OpenapiTags) > 0 {
escapedTags := make([]string, len(info.OpenapiTags))
for i, tag := range info.OpenapiTags {
escapedTags[i] = strings.ReplaceAll(tag, `"`, `\"`)
}
w.writeLines(fmt.Sprintf(`@openapi_tags = ["%s"]`, strings.Join(escapedTags, `", "`)))
}
w.writeLines("@description =:", PushIndent)
desc := getDescription(info.Description)
w.writeLines(buildDescriptionLines("| ", desc, CommentLineLength-w.ind.CurrentIndentLen())...)
Expand Down

0 comments on commit 5c7f038

Please sign in to comment.