@@ -3,10 +3,14 @@ package api_doc
33import (
44 "context"
55 "fmt"
6+ "reflect"
7+ "strings"
8+
9+ yaml "gopkg.in/yaml.v3"
10+
611 "github.com/APIParkLab/APIPark/service/universally/commit"
712 "github.com/eolinker/go-common/autowire"
813 "github.com/getkin/kin-openapi/openapi3"
9- "reflect"
1014)
1115
1216type IAPIDocService interface {
@@ -65,15 +69,6 @@ func (d *DocLoader) Valid() error {
6569 if d .openAPI3Doc .Paths == nil {
6670 return fmt .Errorf ("openAPI3Doc.Paths is nil" )
6771 }
68- //err := d.openAPI3Doc.Validate(openapi3Loader.Context)
69- //if err != nil {
70- // openAPI2Doc, err := openapi2conv.FromV3(d.openAPI3Doc)
71- // if err != nil {
72- // return err
73- // }
74- // validate.
75- //
76- //}
7772 return nil
7873}
7974
@@ -110,3 +105,30 @@ func (d *DocLoader) APICount() int64 {
110105 }
111106 return count
112107}
108+
109+ func (d * DocLoader ) AddPrefixInAll (prefix string ) error {
110+ prefix = strings .TrimSpace (prefix )
111+ if prefix == "" || d .openAPI3Doc == nil || d .openAPI3Doc .Paths == nil {
112+ return nil
113+ }
114+
115+ prefix = fmt .Sprintf ("/%s/" , strings .Trim (prefix , "/" ))
116+ for path , item := range d .openAPI3Doc .Paths .Map () {
117+ path = strings .TrimSpace (path )
118+ if strings .HasPrefix (path , prefix ) {
119+ continue
120+ }
121+ newPath := fmt .Sprintf ("%s%s" , prefix , strings .TrimPrefix (path , "/" ))
122+ d .openAPI3Doc .Paths .Delete (path )
123+ d .openAPI3Doc .Paths .Set (newPath , item )
124+ }
125+ return nil
126+ }
127+
128+ func (d * DocLoader ) Marshal () ([]byte , error ) {
129+ result , err := d .openAPI3Doc .MarshalYAML ()
130+ if err != nil {
131+ return nil , err
132+ }
133+ return yaml .Marshal (result )
134+ }
0 commit comments