@@ -45,13 +45,15 @@ type paramsInfo struct {
4545}
4646
4747// Generate the paths.go file.
48- func generatePaths (file string , spec * openapi3.T ) error {
48+ func generatePaths (file string , spec * openapi3.T ) ([] methodTemplate , error ) {
4949 f , err := openGeneratedFile (file )
5050 if err != nil {
51- return err
51+ return nil , err
5252 }
5353 defer f .Close ()
5454
55+ methods := make ([]methodTemplate , 0 )
56+
5557 // Iterate over all the paths in the spec and write the methods.
5658 // We want to ensure we keep the order.
5759 keys := make ([]string , 0 )
@@ -66,78 +68,87 @@ func generatePaths(file string, spec *openapi3.T) error {
6668 continue
6769 }
6870
69- err := buildPath (f , spec , path , p )
71+ tmpMethods , err := buildPath (f , spec , path , p )
7072 if err != nil {
71- return err
73+ return nil , err
7274 }
75+ methods = append (methods , tmpMethods ... )
7376 }
7477
75- return nil
78+ return methods , nil
7679}
7780
7881// buildPath builds the given path as an http request to the given file.
79- func buildPath (f * os.File , spec * openapi3.T , path string , p * openapi3.PathItem ) error {
82+ func buildPath (f * os.File , spec * openapi3.T , path string , p * openapi3.PathItem ) ([]methodTemplate , error ) {
83+ methods := make ([]methodTemplate , 0 )
8084 if p .Get != nil {
81- err := buildMethod (f , spec , http .MethodGet , path , p .Get , false )
85+ tmpMethods , err := buildMethod (f , spec , http .MethodGet , path , p .Get , false )
8286 if err != nil {
83- return err
87+ return nil , err
8488 }
89+ methods = append (methods , tmpMethods ... )
8590 }
8691
8792 if p .Post != nil {
88- err := buildMethod (f , spec , http .MethodPost , path , p .Post , false )
93+ tmpMethods , err := buildMethod (f , spec , http .MethodPost , path , p .Post , false )
8994 if err != nil {
90- return err
95+ return nil , err
9196 }
97+ methods = append (methods , tmpMethods ... )
9298 }
9399
94100 if p .Put != nil {
95- err := buildMethod (f , spec , http .MethodPut , path , p .Put , false )
101+ tmpMethods , err := buildMethod (f , spec , http .MethodPut , path , p .Put , false )
96102 if err != nil {
97- return err
103+ return nil , err
98104 }
105+ methods = append (methods , tmpMethods ... )
99106 }
100107
101108 if p .Delete != nil {
102- err := buildMethod (f , spec , http .MethodDelete , path , p .Delete , false )
109+ tmpMethods , err := buildMethod (f , spec , http .MethodDelete , path , p .Delete , false )
103110 if err != nil {
104- return err
111+ return nil , err
105112 }
113+ methods = append (methods , tmpMethods ... )
106114 }
107115
108116 if p .Patch != nil {
109- err := buildMethod (f , spec , http .MethodPatch , path , p .Patch , false )
117+ tmpMethods , err := buildMethod (f , spec , http .MethodPatch , path , p .Patch , false )
110118 if err != nil {
111- return err
119+ return nil , err
112120 }
121+ methods = append (methods , tmpMethods ... )
113122 }
114123
115124 if p .Head != nil {
116- err := buildMethod (f , spec , http .MethodHead , path , p .Head , false )
125+ tmpMethods , err := buildMethod (f , spec , http .MethodHead , path , p .Head , false )
117126 if err != nil {
118- return err
127+ return nil , err
119128 }
129+ methods = append (methods , tmpMethods ... )
120130 }
121131
122132 if p .Options != nil {
123- err := buildMethod (f , spec , http .MethodOptions , path , p .Options , false )
133+ tmpMethods , err := buildMethod (f , spec , http .MethodOptions , path , p .Options , false )
124134 if err != nil {
125- return err
135+ return nil , err
126136 }
137+ methods = append (methods , tmpMethods ... )
127138 }
128139
129- return nil
140+ return methods , nil
130141}
131142
132- func buildMethod (f * os.File , spec * openapi3.T , method string , path string , o * openapi3.Operation , isGetAllPages bool ) error {
143+ func buildMethod (f * os.File , spec * openapi3.T , method string , path string , o * openapi3.Operation , isGetAllPages bool ) ([] methodTemplate , error ) {
133144 respType , pagedRespType , err := getSuccessResponseType (o , isGetAllPages )
134145 if err != nil {
135- return err
146+ return nil , err
136147 }
137148
138149 if len (o .Tags ) == 0 || o .Tags [0 ] == "hidden" {
139150 fmt .Printf ("[WARN] TODO: skipping operation %q, since it has no tag or is hidden\n " , o .OperationID )
140- return nil
151+ return nil , nil
141152 }
142153
143154 methodName := strcase .ToCamel (o .OperationID )
@@ -147,7 +158,7 @@ func buildMethod(f *os.File, spec *openapi3.T, method string, path string, o *op
147158
148159 if isGetAllPages {
149160 if pagedRespType == "" {
150- return nil
161+ return nil , nil
151162 }
152163 }
153164
@@ -165,11 +176,11 @@ func buildMethod(f *os.File, spec *openapi3.T, method string, path string, o *op
165176
166177 pathParams , err := buildPathOrQueryParams ("path" , pInfo .parameters )
167178 if err != nil {
168- return err
179+ return nil , err
169180 }
170181 queryParams , err := buildPathOrQueryParams ("query" , pInfo .parameters )
171182 if err != nil {
172- return err
183+ return nil , err
173184 }
174185
175186 sanitisedDescription := strings .ReplaceAll (o .Description , "\n " , "\n // " )
@@ -211,18 +222,22 @@ func buildMethod(f *os.File, spec *openapi3.T, method string, path string, o *op
211222 }
212223
213224 if err := writeTpl (f , config ); err != nil {
214- return err
225+ return nil , err
215226 }
216227
228+ methods := make ([]methodTemplate , 0 )
229+ methods = append (methods , config )
230+
217231 if pInfo .isPageResult && ! isGetAllPages {
218232 // Run the method again with get all pages for ListAll methods.
219- err := buildMethod (f , spec , method , path , o , true )
233+ allPagesMethods , err := buildMethod (f , spec , method , path , o , true )
220234 if err != nil {
221- return err
235+ return nil , err
222236 }
237+ methods = append (methods , allPagesMethods ... )
223238 }
224239
225- return nil
240+ return methods , nil
226241}
227242
228243func getSuccessResponseType (o * openapi3.Operation , isGetAllPages bool ) (string , string , error ) {
0 commit comments