diff --git a/pkg/importer/openapi3_legacy.go b/pkg/importer/openapi3_legacy.go index b9044c51..daad8b2e 100644 --- a/pkg/importer/openapi3_legacy.go +++ b/pkg/importer/openapi3_legacy.go @@ -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{ @@ -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, @@ -188,6 +198,7 @@ func (o *OpenAPI3Importer) buildSyslInfo(spec *openapi3.T, basepath string) Sysl info.OtherFields = append(info.OtherFields, key, val) } } + return info } diff --git a/pkg/importer/tests/openapi2/tags.sysl b/pkg/importer/tests/openapi2/tags.sysl new file mode 100644 index 00000000..92f9d8d4 --- /dev/null +++ b/pkg/importer/tests/openapi2/tags.sysl @@ -0,0 +1,13 @@ +########################################## +## ## +## AUTOGENERATED CODE -- DO NOT EDIT! ## +## ## +########################################## + +TestApp "foo" [package="com.example.package"]: + @openapi_tags = ["test1", "test \"2\""] + @description =: + | No description. + + #--------------------------------------------------------------------------- + # definitions diff --git a/pkg/importer/tests/openapi2/tags.yaml b/pkg/importer/tests/openapi2/tags.yaml new file mode 100644 index 00000000..414e0aab --- /dev/null +++ b/pkg/importer/tests/openapi2/tags.yaml @@ -0,0 +1,10 @@ +swagger: "2.0" +info: + title: foo +tags: + - name: test1 + description: test1 description + - name: test "2" + description: test2 description +paths: +definitions: \ No newline at end of file diff --git a/pkg/importer/writer.go b/pkg/importer/writer.go index 5b59935b..5f8a88e8 100644 --- a/pkg/importer/writer.go +++ b/pkg/importer/writer.go @@ -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 { @@ -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())...)