Skip to content

Commit 7f4cdf6

Browse files
author
Ramazon
committed
feat: add code generation for discriminator with allOf
1 parent 0d94f2f commit 7f4cdf6

16 files changed

Lines changed: 2139 additions & 5 deletions

File tree

examples/discriminator-allof/api.gen.go

Lines changed: 629 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Pet Store API with Discriminator
4+
version: 1.0.0
5+
description: Example API demonstrating discriminator usage with allOf
6+
7+
paths:
8+
/pets:
9+
get:
10+
summary: Get pets
11+
operationId: getPets
12+
responses:
13+
'200':
14+
description: List of pets
15+
content:
16+
application/json:
17+
schema:
18+
type: array
19+
items:
20+
$ref: '#/components/schemas/Pet'
21+
22+
/pets/{petId}/activities:
23+
post:
24+
summary: Log activity
25+
operationId: logActivity
26+
description: Test discriminator at schema level with allOf
27+
parameters:
28+
- name: petId
29+
in: path
30+
required: true
31+
schema:
32+
type: string
33+
requestBody:
34+
content:
35+
application/json:
36+
schema:
37+
$ref: '#/components/schemas/PetActivity'
38+
responses:
39+
'201':
40+
description: Activity logged
41+
42+
/pets/{petId}/health-records:
43+
post:
44+
summary: Add health record
45+
operationId: addHealthRecord
46+
description: Test discriminator in inline allOf element
47+
parameters:
48+
- name: petId
49+
in: path
50+
required: true
51+
schema:
52+
type: string
53+
requestBody:
54+
content:
55+
application/json:
56+
schema:
57+
$ref: '#/components/schemas/HealthRecord'
58+
responses:
59+
'201':
60+
description: Record added
61+
62+
/animals:
63+
get:
64+
summary: Get all animals
65+
operationId: getAllAnimals
66+
description: Returns array of animals with discriminator
67+
responses:
68+
'200':
69+
description: List of animals
70+
content:
71+
application/json:
72+
schema:
73+
type: array
74+
items:
75+
$ref: '#/components/schemas/Animal'
76+
post:
77+
summary: Register animal
78+
operationId: registerAnimal
79+
description: Test nested discriminators (multi-level hierarchy)
80+
requestBody:
81+
content:
82+
application/json:
83+
schema:
84+
$ref: '#/components/schemas/Animal'
85+
responses:
86+
'201':
87+
description: Animal registered
88+
89+
components:
90+
schemas:
91+
# Scenario 1: Standard discriminator (inherited from base)
92+
Pet:
93+
type: object
94+
required:
95+
- petType
96+
- name
97+
properties:
98+
petType:
99+
type: string
100+
name:
101+
type: string
102+
discriminator:
103+
propertyName: petType
104+
mapping:
105+
cat: '#/components/schemas/Cat'
106+
dog: '#/components/schemas/Dog'
107+
108+
Cat:
109+
allOf:
110+
- $ref: '#/components/schemas/Pet'
111+
- type: object
112+
properties:
113+
meow:
114+
type: boolean
115+
116+
Dog:
117+
allOf:
118+
- $ref: '#/components/schemas/Pet'
119+
- type: object
120+
properties:
121+
bark:
122+
type: boolean
123+
124+
# Scenario 2: Discriminator at schema level with allOf
125+
# Schema has allOf + discriminator defined at top level (not in base)
126+
BaseActivity:
127+
type: object
128+
required:
129+
- duration
130+
properties:
131+
duration:
132+
type: integer
133+
134+
PetActivity:
135+
allOf:
136+
- $ref: '#/components/schemas/BaseActivity'
137+
- type: object
138+
required:
139+
- activityType
140+
properties:
141+
activityType:
142+
type: string
143+
discriminator:
144+
propertyName: activityType
145+
mapping:
146+
feeding: '#/components/schemas/FeedingActivity'
147+
walking: '#/components/schemas/WalkingActivity'
148+
149+
FeedingActivity:
150+
allOf:
151+
- $ref: '#/components/schemas/PetActivity'
152+
- type: object
153+
required:
154+
- foodType
155+
properties:
156+
foodType:
157+
type: string
158+
159+
WalkingActivity:
160+
allOf:
161+
- $ref: '#/components/schemas/PetActivity'
162+
- type: object
163+
required:
164+
- distance
165+
properties:
166+
distance:
167+
type: number
168+
169+
# Scenario 3: Discriminator in inline allOf element
170+
# Discriminator defined inside an inline (non-$ref) allOf element
171+
BaseHealthRecord:
172+
type: object
173+
required:
174+
- date
175+
properties:
176+
date:
177+
type: string
178+
format: date
179+
180+
HealthRecord:
181+
allOf:
182+
- $ref: '#/components/schemas/BaseHealthRecord'
183+
- type: object
184+
required:
185+
- recordType
186+
properties:
187+
recordType:
188+
type: string
189+
discriminator:
190+
propertyName: recordType
191+
mapping:
192+
vaccination: '#/components/schemas/VaccinationRecord'
193+
checkup: '#/components/schemas/CheckupRecord'
194+
195+
VaccinationRecord:
196+
allOf:
197+
- $ref: '#/components/schemas/HealthRecord'
198+
- type: object
199+
required:
200+
- vaccine
201+
properties:
202+
vaccine:
203+
type: string
204+
205+
CheckupRecord:
206+
allOf:
207+
- $ref: '#/components/schemas/HealthRecord'
208+
- type: object
209+
required:
210+
- weight
211+
properties:
212+
weight:
213+
type: number
214+
215+
# Scenario 4: Nested discriminators (multi-level hierarchy)
216+
# Top -> Middle (with discriminator) -> Child
217+
Animal:
218+
type: object
219+
required:
220+
- animalType
221+
- name
222+
properties:
223+
animalType:
224+
type: string
225+
name:
226+
type: string
227+
discriminator:
228+
propertyName: animalType
229+
mapping:
230+
domestic: '#/components/schemas/DomesticAnimal'
231+
wild: '#/components/schemas/WildAnimal'
232+
233+
# Middle level with its own discriminator
234+
DomesticAnimal:
235+
allOf:
236+
- $ref: '#/components/schemas/Animal'
237+
- type: object
238+
required:
239+
- domesticType
240+
properties:
241+
domesticType:
242+
type: string
243+
owner:
244+
type: string
245+
discriminator:
246+
propertyName: domesticType
247+
mapping:
248+
housecat: '#/components/schemas/HouseCat'
249+
housedog: '#/components/schemas/HouseDog'
250+
251+
HouseCat:
252+
allOf:
253+
- $ref: '#/components/schemas/DomesticAnimal'
254+
- type: object
255+
properties:
256+
indoor:
257+
type: boolean
258+
259+
HouseDog:
260+
allOf:
261+
- $ref: '#/components/schemas/DomesticAnimal'
262+
- type: object
263+
properties:
264+
trained:
265+
type: boolean
266+
267+
# Another middle level with its own discriminator
268+
WildAnimal:
269+
allOf:
270+
- $ref: '#/components/schemas/Animal'
271+
- type: object
272+
required:
273+
- wildType
274+
properties:
275+
wildType:
276+
type: string
277+
habitat:
278+
type: string
279+
discriminator:
280+
propertyName: wildType
281+
mapping:
282+
lion: '#/components/schemas/Lion'
283+
tiger: '#/components/schemas/Tiger'
284+
285+
Lion:
286+
allOf:
287+
- $ref: '#/components/schemas/WildAnimal'
288+
- type: object
289+
required:
290+
- maneColor
291+
properties:
292+
maneColor:
293+
type: string
294+
295+
Tiger:
296+
allOf:
297+
- $ref: '#/components/schemas/WildAnimal'
298+
- type: object
299+
properties:
300+
stripeCount:
301+
type: integer
302+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package: discriminator
2+
generate:
3+
models: true
4+
output: api.gen.go
5+

0 commit comments

Comments
 (0)