@@ -28,7 +28,7 @@ func NewReflector() *Reflector {
2828
2929 r .DefaultOptions = append (r .DefaultOptions , jsonschema .InterceptSchema (func (params jsonschema.InterceptSchemaParams ) (stop bool , err error ) {
3030 // See https://spec.openapis.org/oas/v3.1.0.html#data-types.
31- switch params .Value .Kind () { //nolint:exhaustive // Not all kinds have formats defined.
31+ switch params .Value .Kind () { //nolint // Not all kinds have formats defined.
3232 case reflect .Int64 :
3333 params .Schema .WithFormat ("int64" )
3434 case reflect .Int32 :
@@ -46,9 +46,9 @@ func NewReflector() *Reflector {
4646}
4747
4848// NewOperationContext initializes openapi.OperationContext to be prepared
49- // and added later with Reflector.AddOperation.
50- func (r * Reflector ) NewOperationContext (method , pathPattern string ) (openapi.OperationContext , error ) {
51- method , pathPattern , pathParams , err := openapi .SanitizeMethodPath (method , pathPattern )
49+ // and added later with Reflector.AddOperation or Reflector.AddWebhook .
50+ func (r * Reflector ) NewOperationContext (method , pathPatternOrWebhookName string ) (openapi.OperationContext , error ) {
51+ method , pathPattern , pathParams , err := openapi .SanitizeMethodPath (method , pathPatternOrWebhookName )
5252 if err != nil {
5353 return nil , err
5454 }
@@ -61,7 +61,7 @@ func (r *Reflector) NewOperationContext(method, pathPattern string) (openapi.Ope
6161 }
6262
6363 if operation != nil {
64- return nil , fmt .Errorf ("operation already exists: %s %s" , method , pathPattern )
64+ return nil , fmt .Errorf ("operation already exists: %s %s" , method , pathPatternOrWebhookName )
6565 }
6666
6767 operation = & Operation {}
@@ -211,24 +211,43 @@ func (o operationContext) Operation() *Operation {
211211
212212// AddOperation configures operation request and response schema.
213213func (r * Reflector ) AddOperation (oc openapi.OperationContext ) error {
214+ c , err := r .setupOC (oc )
215+ if err != nil {
216+ return err
217+ }
218+
219+ return r .SpecEns ().AddOperation (oc .Method (), oc .PathPattern (), * c .op )
220+ }
221+
222+ // AddWebhook configures webhook request and response schema.
223+ func (r * Reflector ) AddWebhook (oc openapi.OperationContext ) error {
224+ c , err := r .setupOC (oc )
225+ if err != nil {
226+ return err
227+ }
228+
229+ return r .SpecEns ().AddWebhook (oc .Method (), c .PathPattern (), * c .op )
230+ }
231+
232+ func (r * Reflector ) setupOC (oc openapi.OperationContext ) (operationContext , error ) {
214233 c , ok := oc .(operationContext )
215234 if ! ok {
216- return fmt .Errorf ("wrong operation context %T received, %T expected" , oc , operationContext {})
235+ return c , fmt .Errorf ("wrong operation context %T received, %T expected" , oc , operationContext {})
217236 }
218237
219238 if err := r .setupRequest (c .op , oc ); err != nil {
220- return fmt .Errorf ("setup request %s %s: %w" , oc .Method (), oc .PathPattern (), err )
239+ return c , fmt .Errorf ("setup request %s %s: %w" , oc .Method (), oc .PathPattern (), err )
221240 }
222241
223242 if err := c .op .validatePathParams (c .pathParams ); err != nil {
224- return fmt .Errorf ("validate path params %s %s: %w" , oc .Method (), oc .PathPattern (), err )
243+ return c , fmt .Errorf ("validate path params %s %s: %w" , oc .Method (), oc .PathPattern (), err )
225244 }
226245
227246 if err := r .setupResponse (c .op , oc ); err != nil {
228- return fmt .Errorf ("setup response %s %s: %w" , oc .Method (), oc .PathPattern (), err )
247+ return c , fmt .Errorf ("setup response %s %s: %w" , oc .Method (), oc .PathPattern (), err )
229248 }
230249
231- return r . SpecEns (). AddOperation ( oc . Method (), oc . PathPattern (), * c . op )
250+ return c , nil
232251}
233252
234253func (r * Reflector ) setupRequest (o * Operation , oc openapi.OperationContext ) error {
0 commit comments