Skip to content

Commit

Permalink
refactor: openapi3: standardize OAS3 extension support; move embedd…
Browse files Browse the repository at this point in the history
…ed `x-tagGroups` functionality to `ext/taggroups`
  • Loading branch information
grokify committed Dec 2, 2022
1 parent cfaf70f commit c7dff89
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 49 deletions.
10 changes: 10 additions & 0 deletions cmd/oas3lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/grokify/mogo/fmt/fmtutil"
"github.com/grokify/mogo/log/logutil"
"github.com/grokify/mogo/os/osutil"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/openapi3lint"
"github.com/grokify/spectrum/openapi3lint/lintutil"
flags "github.com/jessevdk/go-flags"
Expand All @@ -24,6 +25,15 @@ func main() {
logutil.FatalErr(err)
fmtutil.PrintJSON(opts)

if 1 == 1 {
spec, err := openapi3.ReadFile(opts.InputFileOAS3, false)
logutil.FatalErr(err)
sm := openapi3.SpecMore{Spec: spec}
ont := sm.Ontology()
fmtutil.PrintJSON(ont)
panic("Z")
}

vsets, err := ValidateSpecFiles(opts.InputFileOAS3, opts.PolicyFile, opts.Severity)
logutil.FatalErr(err)

Expand Down
2 changes: 1 addition & 1 deletion cmd/oas3validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
fmt.Println(md)
opts.XlsxWrite = strings.TrimSpace(opts.XlsxWrite)
if len(opts.XlsxWrite) > 0 {
err := sm.WriteFileXLSX(opts.XlsxWrite, nil, nil)
err := sm.WriteFileXLSX(opts.XlsxWrite, nil, nil, nil)
if err != nil {
log.Fatal(err)
}
Expand Down
135 changes: 135 additions & 0 deletions ext/taggroups/taggroups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package taggroups

import (
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/grokify/mogo/type/stringsutil"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/openapi3edit"
)

const XTagGroupsPropertyName = "x-tag-groups"

type TagGroupSet struct {
TagGroups []TagGroup
}

func NewTagGroupSet() TagGroupSet {
return TagGroupSet{TagGroups: []TagGroup{}}
}

func (set *TagGroupSet) Exists(tagName string) bool {
for _, tg := range set.TagGroups {
for _, tgTagName := range tg.Tags {
if tagName == tgTagName {
return true
}
}
}
return false
}

func (set *TagGroupSet) GetTagGroupNamesForTagNames(wantTagNames ...string) []string {
tagGroupNames := []string{}
for _, tg := range set.TagGroups {
for _, tgTagName := range tg.Tags {
for _, wantTagName := range wantTagNames {
if wantTagName == tgTagName {
tagGroupNames = append(tagGroupNames, tg.Name)
}
}
}
}
return stringsutil.SliceCondenseSpace(tagGroupNames, true, true)
}

func (set *TagGroupSet) AddToSpec(spec *openapi3.Spec) error {
if len(set.TagGroups) == 0 {
return nil
}
missing := SpecTagsWithoutGroups(spec, *set)
if len(missing) > 0 {
return fmt.Errorf("E_TAGS_WITHOUT_GROUPS [%s]", strings.Join(missing, ","))
}
se := openapi3edit.SpecEdit{}
se.SpecSet(spec)
se.ExtensionSet(XTagGroupsPropertyName, set.TagGroups)
// spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = set.TagGroups
return nil
}

// OperationMoreTagGroupNames this function is meant to be used wtih `SpecMore.Table()`
// and must follow the `OperationMoreStringFunc` interface.
func (set *TagGroupSet) OperationMoreTagGroupNames(opm *openapi3.OperationMore) string {
// row = append(row, strings.Join(tgs.GetTagGroupNamesForTagNames(op.Tags...), ", "))
if opm == nil || opm.Operation == nil {
return ""
}
return strings.Join(set.GetTagGroupNamesForTagNames(opm.Operation.Tags...), ", ")
}

type TagGroup struct {
Name string `json:"name"`
Popular bool `json:"popular"`
Tags []string `json:"tags"`
}

/*
func (sm *SpecMore) TagsWithoutGroups() ([]string, []string, []string, error) {
tgs, err := sm.TagGroups()
if err != nil {
return []string{}, []string{}, []string{}, err
}
allTags := []string{}
topTags := stringsutil.SliceCondenseSpace(sm.Tags(true, false), true, true)
allTags = append(allTags, topTags...)
opsTags := stringsutil.SliceCondenseSpace(sm.Tags(false, true), true, true)
allTags = append(allTags, opsTags...)
allTags = stringsutil.SliceCondenseSpace(allTags, true, true)
return allTags, topTags, opsTags, nil
}
*/

func SpecTagsWithoutGroups(spec *openapi3.Spec, tagGroupSet TagGroupSet) []string {
missing := []string{}
for _, tag := range spec.Tags {
if !tagGroupSet.Exists(tag.Name) {
missing = append(missing, tag.Name)
}
}
return missing
}

// SpecTagGroups parses a TagGroupSet from an OpenAPI3 spec.
func SpecTagGroups(spec *openapi3.Spec) (TagGroupSet, error) {
sm := openapi3.SpecMore{Spec: spec}
tgs := NewTagGroupSet()
iface, ok := sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName]
if !ok {
return tgs, nil
}

tagGroups := []TagGroup{}
if reflect.TypeOf(iface) == reflect.TypeOf(tagGroups) {
tgs.TagGroups = iface.([]TagGroup)
return tgs, nil
}

// message is stored as `json.RawMessage` when the data
// is read in from JSON, vs. set via code.
rawMessage := iface.(json.RawMessage)
err := json.Unmarshal(rawMessage, &tagGroups)
if err != nil {
return tgs, err
}
tgs.TagGroups = tagGroups
delete(sm.Spec.ExtensionProps.Extensions, XTagGroupsPropertyName)
sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = tagGroups
return tgs, nil
}
6 changes: 3 additions & 3 deletions openapi3/extension_taggroups.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package openapi3

/*
import (
"encoding/json"
"fmt"
Expand Down Expand Up @@ -62,8 +63,7 @@ type TagGroup struct {
Tags []string `json:"tags"`
}
/*
func (sm *SpecMore) TagsWithoutGroups() ([]string, []string, []string, error) {
func (sm *SpecMore) TagsWithoutGroupsOLDOLD() ([]string, []string, []string, error) {
tgs, err := sm.TagGroups()
if err != nil {
return []string{}, []string{}, []string{}, err
Expand All @@ -79,7 +79,6 @@ func (sm *SpecMore) TagsWithoutGroups() ([]string, []string, []string, error) {
allTags = stringsutil.SliceCondenseSpace(allTags, true, true)
return allTags, topTags, opsTags, nil
}
*/
func TagsWithoutGroups(spec *Spec, tagGroupSet TagGroupSet) []string {
missing := []string{}
Expand Down Expand Up @@ -117,3 +116,4 @@ func (sm *SpecMore) TagGroups() (TagGroupSet, error) {
sm.Spec.ExtensionProps.Extensions[XTagGroupsPropertyName] = tagGroups
return tgs, nil
}
*/
6 changes: 3 additions & 3 deletions openapi3/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ func MergeWithTables(spec1, spec2 *Spec, specExtraNote string, mergeOpts *MergeO
tbls := []*table.Table{}
sm1 := SpecMore{Spec: spec1}
sm2 := SpecMore{Spec: spec2}
tbls1, err := sm1.OperationsTable(mergeOpts.TableColumns, mergeOpts.TableOpFilterFunc)
tbls1, err := sm1.OperationsTable(mergeOpts.TableColumns, mergeOpts.TableOpFilterFunc, mergeOpts.TableAddlColFormatFuncs)
if err != nil {
return nil, nil, err
}
tbls = append(tbls, tbls1)
tbls[0].Name = "Spec1"
tbls2, err := sm2.OperationsTable(mergeOpts.TableColumns, mergeOpts.TableOpFilterFunc)
tbls2, err := sm2.OperationsTable(mergeOpts.TableColumns, mergeOpts.TableOpFilterFunc, mergeOpts.TableAddlColFormatFuncs)
if err != nil {
return nil, nil, err
}
Expand All @@ -136,7 +136,7 @@ func MergeWithTables(spec1, spec2 *Spec, specExtraNote string, mergeOpts *MergeO
return specf, tbls, err
}
smf := SpecMore{Spec: specf}
tblsf, err := smf.OperationsTable(mergeOpts.TableColumns, mergeOpts.TableOpFilterFunc)
tblsf, err := smf.OperationsTable(mergeOpts.TableColumns, mergeOpts.TableOpFilterFunc, mergeOpts.TableAddlColFormatFuncs)
if err != nil {
return nil, nil, err
}
Expand Down
15 changes: 8 additions & 7 deletions openapi3/merge_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const (
)

type MergeOptions struct {
FileRx *regexp.Regexp
SchemaFunc func(schemaName string, sch1, sch2 interface{}, hint2 string) CollisionCheckResult
CollisionCheckResult CollisionCheckResult
ValidateEach bool
ValidateFinal bool
TableColumns *tabulator.ColumnSet
TableOpFilterFunc func(path, method string, op *oas3.Operation) bool
FileRx *regexp.Regexp
SchemaFunc func(schemaName string, sch1, sch2 interface{}, hint2 string) CollisionCheckResult
CollisionCheckResult CollisionCheckResult
ValidateEach bool
ValidateFinal bool
TableColumns *tabulator.ColumnSet
TableOpFilterFunc func(path, method string, op *oas3.Operation) bool
TableAddlColFormatFuncs *OperationMoreStringFuncMap
}

func NewMergeOptionsSkip() *MergeOptions {
Expand Down
17 changes: 9 additions & 8 deletions openapi3/openapi3html/pageparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import (
)

type PageParams struct {
PageTitle string
PageLink string
TableDomID string
Spec *openapi3.Spec
ColumnSet *tabulator.ColumnSet
OpsFilterFunc func(path, method string, op *oas3.Operation) bool
TableJSON []byte
PageTitle string
PageLink string
TableDomID string
Spec *openapi3.Spec
ColumnSet *tabulator.ColumnSet
OpsFilterFunc func(path, method string, op *oas3.Operation) bool
OpsAdditionalFormatFuncs *openapi3.OperationMoreStringFuncMap
TableJSON []byte
}

func (pp *PageParams) PageLinkHTML() string {
Expand All @@ -34,7 +35,7 @@ func (pp *PageParams) PageLinkHTML() string {

func (pp *PageParams) AddSpec(spec *openapi3.Spec) error {
sm := openapi3.SpecMore{Spec: spec}
tbl, err := sm.OperationsTable(pp.ColumnSet, pp.OpsFilterFunc)
tbl, err := sm.OperationsTable(pp.ColumnSet, pp.OpsFilterFunc, pp.OpsAdditionalFormatFuncs)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions openapi3/openapi3postman2/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/net/urlutil"
"github.com/grokify/spectrum/ext/taggroups"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/postman2"
"github.com/grokify/spectrum/postman2/simple"
Expand Down Expand Up @@ -106,9 +107,10 @@ func Merge(cfg Configuration, pman postman2.Collection, oas3spec *openapi3.Spec)
return pman, err
}

oas3specMore := openapi3.SpecMore{Spec: oas3spec}
tagGroupSet, err := oas3specMore.TagGroups()
//tagGroupSet, err := openapi3.SpecTagGroups(oas3spec)
// tagGroupSet, err := openapi3.SpecTagGroups(oas3spec)
// oas3specMore := openapi3.SpecMore{Spec: oas3spec}
// tagGroupSet, err := oas3specMore.TagGroups()
tagGroupSet, err := taggroups.SpecTagGroups(oas3spec)
if err != nil {
return pman, err
}
Expand Down Expand Up @@ -151,7 +153,7 @@ func Merge(cfg Configuration, pman postman2.Collection, oas3spec *openapi3.Spec)
return pman, nil
}

func postmanAddItemToFolders(pman postman2.Collection, pmItem *postman2.Item, tagNames []string, tagGroupSet openapi3.TagGroupSet) postman2.Collection {
func postmanAddItemToFolders(pman postman2.Collection, pmItem *postman2.Item, tagNames []string, tagGroupSet taggroups.TagGroupSet) postman2.Collection {
for _, tagName := range tagNames {
tagGroupNames := tagGroupSet.GetTagGroupNamesForTagNames(tagName)
if len(tagGroupNames) == 0 {
Expand Down
7 changes: 4 additions & 3 deletions openapi3/openapi3postman2/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/net/httputilmore"
"github.com/grokify/spectrum/ext/taggroups"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/postman2"
)

func CreateTagsAndTagGroups(pman postman2.Collection, spec *openapi3.Spec) (postman2.Collection, error) {
oas3specMore := openapi3.SpecMore{Spec: spec}
tagGroupSet, err := oas3specMore.TagGroups()
// oas3specMore := openapi3.SpecMore{Spec: spec}
tagGroupSet, err := taggroups.SpecTagGroups(spec)
// tagGroupSet, err := openapi3.SpecTagGroups(spec)
if err != nil {
return pman, err
Expand All @@ -23,7 +24,7 @@ func CreateTagsAndTagGroups(pman postman2.Collection, spec *openapi3.Spec) (post
return addFoldersFromTags(pman, spec.Tags), nil
}

func addFoldersFromTagGroups(pman postman2.Collection, tgSet openapi3.TagGroupSet, tags oas3.Tags) (postman2.Collection, error) {
func addFoldersFromTagGroups(pman postman2.Collection, tgSet taggroups.TagGroupSet, tags oas3.Tags) (postman2.Collection, error) {
tagsMore := openapi3.TagsMore{Tags: tags}
for _, tg := range tgSet.TagGroups {
tg.Name = strings.TrimSpace(tg.Name)
Expand Down
13 changes: 13 additions & 0 deletions openapi3/operation_more.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ type OperationMore struct {
Operation *oas3.Operation
}

type OperationMoreStringFunc func(opm *OperationMore) string

type OperationMoreStringFuncMap map[string]OperationMoreStringFunc

func (opmmap *OperationMoreStringFuncMap) Func(key string) OperationMoreStringFunc {
opmmapIndexable := map[string]OperationMoreStringFunc(*opmmap)
wantFunc, ok := opmmapIndexable[key]
if !ok {
return nil
}
return wantFunc
}

func (om *OperationMore) HasParameter(paramNameWant string) bool {
paramNameWantLc := strings.ToLower(strings.TrimSpace(paramNameWant))
for _, paramRef := range om.Operation.Parameters {
Expand Down
Loading

0 comments on commit c7dff89

Please sign in to comment.