Skip to content

Commit bc58190

Browse files
authored
Generate Webhooks (#62)
1 parent 6a09ea5 commit bc58190

19 files changed

Lines changed: 802 additions & 82 deletions

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ on the real value-add for your organization.
4040

4141
### Configuration & Filtering
4242
- **YAML-based configuration** with JSON schema validation
43-
- **Flexible filtering** - Include/exclude by paths, tags, operation IDs, schema properties, or extensions
43+
- **Flexible filtering** - Include/exclude by paths, tags, operation IDs, webhooks, schema properties, or extensions
4444
- **Transitive pruning** - Automatically remove schemas that are only referenced by filtered-out properties
4545
- **[OpenAPI Overlays](https://doordash-oss.github.io/oapi-codegen-dd/overlays/)** - Modify specs without editing originals (add extensions, remove paths)
4646
- **External file `$ref` resolution** - Split specs across multiple files with relative `$ref` references (auto-detected from spec path, or set `base-path` in config)
@@ -85,7 +85,8 @@ Tested against [2,137 real-world OpenAPI 3.0 specs](https://github.com/mockzilla
8585
| **Name Conflicts** | Manual fix required | Automatic resolution |
8686
| **Validation** | None | `Validate()` methods |
8787
| **Server Scaffold** | Interface only, manual boilerplate | Full typed solution (service, middleware, main.go) |
88-
| **Filtering** | Tags, operation IDs | + Paths, extensions, schema properties |
88+
| **Webhooks** | Not supported | Full type generation with filtering |
89+
| **Filtering** | Tags, operation IDs | + Paths, webhooks, extensions, schema properties |
8990
| **Overlays** | Single | Multiple, applied in order |
9091
| **Output** | Single file | Single or multiple files |
9192
| **Templates** | Monolithic | Composable with `{{define}}` blocks |

configuration-schema.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@
139139
"properties": {
140140
"include": {
141141
"$ref": "#/definitions/FilterParamsConfig",
142-
"description": "Paths, tags, operation IDs, and schema properties to include."
142+
"description": "Paths, tags, operation IDs, webhooks, and schema properties to include."
143143
},
144144
"exclude": {
145145
"$ref": "#/definitions/FilterParamsConfig",
146-
"description": "Paths, tags, operation IDs, and schema properties to exclude."
146+
"description": "Paths, tags, operation IDs, webhooks, and schema properties to exclude."
147147
}
148148
},
149149
"required": []
@@ -173,6 +173,13 @@
173173
},
174174
"description": "List of operation IDs to include or exclude."
175175
},
176+
"webhooks": {
177+
"type": "array",
178+
"items": {
179+
"type": "string"
180+
},
181+
"description": "List of webhook names to include or exclude."
182+
},
176183
"schema-properties": {
177184
"type": "object",
178185
"description": "Mapping of schema names to property names to include or exclude.",

docs/configuration.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,25 @@ filter:
427427
- deleteUser
428428
```
429429

430+
### Filter by Webhooks
431+
432+
Filter webhook entries by name. This is useful for OpenAPI 3.1 specs that define [webhooks](https://spec.openapis.org/oas/v3.1.0#fixed-fields) -
433+
schemas referenced by included webhooks are preserved during pruning.
434+
435+
```yaml
436+
filter:
437+
include:
438+
webhooks:
439+
- order.created
440+
- payment.completed
441+
exclude:
442+
webhooks:
443+
- internal.debug
444+
```
445+
446+
Webhook operations also respect tag and operation ID filters - if a webhook's operation doesn't match the tag/operation ID filter,
447+
it will be removed just like path operations.
448+
430449
### Filter by Schema Properties
431450

432451
Filter which properties are included in generated types. This triggers **transitive pruning** - schemas that are only referenced by filtered-out properties will also be pruned.

examples/webhooks/ex1/api.yaml

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
openapi: 3.1.0
2+
info:
3+
version: '1.0'
4+
title: Webhook Events API
5+
description: >
6+
Example spec demonstrating webhook support.
7+
Defines inbound webhook callbacks for payment and order events.
8+
webhooks:
9+
payment.authorized:
10+
post:
11+
summary: Payment has been authorized
12+
operationId: paymentAuthorized
13+
tags:
14+
- payments
15+
requestBody:
16+
required: true
17+
content:
18+
application/json:
19+
schema:
20+
$ref: '#/components/schemas/PaymentEvent'
21+
responses:
22+
'200':
23+
description: Acknowledged
24+
content:
25+
application/json:
26+
schema:
27+
$ref: '#/components/schemas/WebhookAck'
28+
order.shipped:
29+
post:
30+
summary: Order has been shipped
31+
operationId: orderShipped
32+
tags:
33+
- orders
34+
requestBody:
35+
required: true
36+
content:
37+
application/json:
38+
schema:
39+
$ref: '#/components/schemas/OrderEvent'
40+
responses:
41+
'200':
42+
description: Acknowledged
43+
content:
44+
application/json:
45+
schema:
46+
$ref: '#/components/schemas/WebhookAck'
47+
inventory.low:
48+
post:
49+
summary: Inventory level is low
50+
operationId: inventoryLow
51+
tags:
52+
- inventory
53+
requestBody:
54+
required: true
55+
content:
56+
application/json:
57+
schema:
58+
type: object
59+
required:
60+
- sku
61+
- remaining
62+
properties:
63+
sku:
64+
type: string
65+
remaining:
66+
type: integer
67+
responses:
68+
'200':
69+
description: Acknowledged
70+
internal.debug:
71+
post:
72+
summary: Internal debug event (excluded from generation)
73+
operationId: internalDebug
74+
tags:
75+
- internal
76+
requestBody:
77+
required: true
78+
content:
79+
application/json:
80+
schema:
81+
$ref: '#/components/schemas/DebugEvent'
82+
responses:
83+
'200':
84+
description: Acknowledged
85+
components:
86+
schemas:
87+
PaymentEvent:
88+
type: object
89+
required:
90+
- id
91+
- amount
92+
- currency
93+
properties:
94+
id:
95+
type: string
96+
format: uuid
97+
amount:
98+
type: integer
99+
description: Amount in minor units (e.g. cents)
100+
currency:
101+
type: string
102+
minLength: 3
103+
maxLength: 3
104+
metadata:
105+
$ref: '#/components/schemas/EventMetadata'
106+
OrderEvent:
107+
type: object
108+
required:
109+
- id
110+
- trackingNumber
111+
properties:
112+
id:
113+
type: string
114+
format: uuid
115+
trackingNumber:
116+
type: string
117+
carrier:
118+
type: string
119+
metadata:
120+
$ref: '#/components/schemas/EventMetadata'
121+
EventMetadata:
122+
type: object
123+
properties:
124+
timestamp:
125+
type: string
126+
format: date-time
127+
source:
128+
type: string
129+
WebhookAck:
130+
type: object
131+
properties:
132+
received:
133+
type: boolean
134+
DebugEvent:
135+
type: object
136+
properties:
137+
level:
138+
type: string
139+
message:
140+
type: string

examples/webhooks/ex1/cfg.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# yaml-language-server: $schema=../../../configuration-schema.json
2+
package: gen
3+
output:
4+
use-single-file: false
5+
filter:
6+
exclude:
7+
webhooks:
8+
- internal.debug

examples/webhooks/ex1/gen/common.go

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/webhooks/ex1/gen/types.go

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/webhooks/ex1/gen/webhooks.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/webhooks/ex1/generate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package gen
2+
3+
//go:generate go run github.com/doordash-oss/oapi-codegen-dd/v3/cmd/oapi-codegen -config cfg.yaml api.yaml

0 commit comments

Comments
 (0)