@@ -23,8 +23,8 @@ func (p *PathItem) WithOperation(method string, operation Operation) *PathItem {
2323
2424var regexFindPathParameter = regexp .MustCompile (`{([^}:]+)(:[^/]+)?(?:})` )
2525
26- // AddOperation validates and sets operation by path and method .
27- func (s * Spec ) AddOperation (method , path string , operation Operation ) error {
26+ // SetupOperation creates operation if it is not present and applies setup functions .
27+ func (s * Spec ) SetupOperation (method , path string , setup ... func ( * Operation ) error ) error {
2828 method = strings .ToLower (method )
2929 pathParametersSubmatches := regexFindPathParameter .FindAllStringSubmatch (path , - 1 )
3030
@@ -36,10 +36,6 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
3636 }
3737
3838 pathItem := s .Paths .MapOfPathItemValues [path ]
39- if _ , found := pathItem .MapOfOperationValues [method ]; found {
40- return errors .New ("operation with method and path already exists" )
41- }
42-
4339 pathParams := map [string ]bool {}
4440
4541 if len (pathParametersSubmatches ) > 0 {
@@ -54,6 +50,15 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
5450
5551 var errs []string
5652
53+ operation := pathItem .MapOfOperationValues [method ]
54+
55+ for _ , f := range setup {
56+ err := f (& operation )
57+ if err != nil {
58+ return err
59+ }
60+ }
61+
5762 paramIndex := make (map [string ]bool , len (operation .Parameters ))
5863
5964 for _ , p := range operation .Parameters {
@@ -90,3 +95,20 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
9095
9196 return nil
9297}
98+
99+ // AddOperation validates and sets operation by path and method.
100+ //
101+ // It will fail if operation with method and path already exists.
102+ func (s * Spec ) AddOperation (method , path string , operation Operation ) error {
103+ method = strings .ToLower (method )
104+ pathItem := s .Paths .MapOfPathItemValues [path ]
105+
106+ if _ , found := pathItem .MapOfOperationValues [method ]; found {
107+ return errors .New ("operation with method and path already exists" )
108+ }
109+
110+ return s .SetupOperation (method , path , func (op * Operation ) error {
111+ * op = operation
112+ return nil
113+ })
114+ }
0 commit comments