Skip to content

Commit 8e7938e

Browse files
Jamie Tannajamietanna
authored andcommitted
docs(client): document known issue with duplicated Response models
Closes oapi-codegen#1965.
1 parent f4a544e commit 8e7938e

5 files changed

Lines changed: 381 additions & 0 deletions

File tree

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,103 @@ However, for more complex URLs that defined `variables` in them, we generate the
17961796

17971797
For a complete example see [`examples/generate/serverurls`](examples/generate/serverurls).
17981798

1799+
### Duplicate types generated for clients's response object types
1800+
1801+
When generating the types for interacting with the generated client, `oapi-codegen` will use the `operationId` and add on a `Request` or `Response` suffix.
1802+
1803+
However, this can clash if you have named your component schemas in a similar way.
1804+
1805+
For instance:
1806+
1807+
```yaml
1808+
openapi: "3.0.0"
1809+
info:
1810+
version: 1.0.0
1811+
title: "Show that generated client boilerplate can clash if schemas are well named i.e. `*Request` and `*Response`"
1812+
paths:
1813+
/client:
1814+
put:
1815+
operationId: updateClient
1816+
requestBodies:
1817+
application/json:
1818+
schema:
1819+
$ref: '#/components/schemas/UpdateClientRequest'
1820+
responses:
1821+
400:
1822+
content:
1823+
application/json:
1824+
schema:
1825+
$ref: '#/components/schemas/UpdateClientResponse'
1826+
components:
1827+
schemas:
1828+
UpdateClientRequest:
1829+
type: object
1830+
properties:
1831+
code:
1832+
type: string
1833+
required:
1834+
- code
1835+
UpdateClientResponse:
1836+
type: object
1837+
required:
1838+
- name
1839+
properties:
1840+
name:
1841+
type: string
1842+
```
1843+
1844+
If you were to generate with this configuration:
1845+
1846+
```yaml
1847+
# yaml-language-server: $schema=https://raw.githubusercontent.com/oapi-codegen/oapi-codegen/HEAD/configuration-schema.json
1848+
package: client
1849+
output: client.gen.go
1850+
generate:
1851+
models: true
1852+
client: true
1853+
```
1854+
1855+
This would then result in `go build` failures:
1856+
1857+
```
1858+
# github.com/oapi-codegen/oapi-codegen/v2/examples/clienttypenameclash
1859+
./client.gen.go:184:6: UpdateClientResponse redeclared in this block
1860+
./client.gen.go:17:6: other declaration of UpdateClientResponse
1861+
./client.gen.go:192:7: r.HTTPResponse undefined (type UpdateClientResponse has no field or method HTTPResponse)
1862+
./client.gen.go:193:12: r.HTTPResponse undefined (type UpdateClientResponse has no field or method HTTPResponse)
1863+
./client.gen.go:200:7: r.HTTPResponse undefined (type UpdateClientResponse has no field or method HTTPResponse)
1864+
./client.gen.go:201:12: r.HTTPResponse undefined (type UpdateClientResponse has no field or method HTTPResponse)
1865+
./client.gen.go:224:3: unknown field Body in struct literal of type UpdateClientResponse
1866+
./client.gen.go:225:3: unknown field HTTPResponse in struct literal of type UpdateClientResponse
1867+
./client.gen.go:234:12: response.JSON400 undefined (type *UpdateClientResponse has no field or method JSON400)
1868+
```
1869+
1870+
To fix this, use the `response-type-suffix` Output Option:
1871+
1872+
```diff
1873+
# yaml-language-server: $schema=https://raw.githubusercontent.com/oapi-codegen/oapi-codegen/HEAD/configuration-schema.json
1874+
package: client
1875+
output: client.gen.go
1876+
generate:
1877+
models: true
1878+
client: true
1879+
+output-options:
1880+
+ response-type-suffix: Resp
1881+
```
1882+
1883+
This will then rename the generated types from the default to use the new suffix:
1884+
1885+
```diff
1886+
-type UpdateClientResponse struct {
1887+
+type UpdateClientResp struct {
1888+
Body []byte
1889+
HTTPResponse *http.Response
1890+
JSON400 *UpdateClientResponse
1891+
}
1892+
```
1893+
1894+
There is no currently planned work to change this behaviour.
1895+
17991896
## Generating API models
18001897

18011898
If you're looking to only generate the models for interacting with a remote service, for instance if you need to hand-roll the API client for whatever reason, you can do this as-is.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: "Show that generated client boilerplate can clash if schemas are well named i.e. `*Request` and `*Response`"
5+
paths:
6+
/client:
7+
put:
8+
operationId: updateClient
9+
requestBodies:
10+
application/json:
11+
schema:
12+
$ref: '#/components/schemas/UpdateClientRequest'
13+
responses:
14+
400:
15+
content:
16+
application/json:
17+
schema:
18+
$ref: '#/components/schemas/UpdateClientResponse'
19+
components:
20+
schemas:
21+
UpdateClientRequest:
22+
type: object
23+
properties:
24+
code:
25+
type: string
26+
required:
27+
- code
28+
UpdateClientResponse:
29+
type: object
30+
required:
31+
- name
32+
properties:
33+
name:
34+
type: string
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: client
3+
output: client.gen.go
4+
generate:
5+
models: true
6+
client: true
7+
output-options:
8+
response-type-suffix: Resp

examples/clienttypenameclash/client.gen.go

Lines changed: 239 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package client
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml

0 commit comments

Comments
 (0)