Skip to content

Commit b148b95

Browse files
jamietannadanicc097
authored andcommitted
Introduce content type into generated JSON types (oapi-codegen#1078)
* Introduce content type into generated JSON types As noted in oapi-codegen#1051, we have cases where multiple uses of JSON types leads to a clash in generated type names. This is a slight breaking change for folks' generated code, but ensures that conflicts do not happen, and that we are more predictable with generated type names. Closes oapi-codegen#1051. * Add gorilla/mux to the README
1 parent 1df8029 commit b148b95

4 files changed

Lines changed: 170 additions & 32 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ you can focus on implementing the business logic for your service.
1515

1616
We have chosen to focus on [Echo](https://github.com/labstack/echo) as
1717
our default HTTP routing engine, due to its speed and simplicity for the generated
18-
stubs, and [Chi](https://github.com/go-chi/chi), and [Gin](https://github.com/gin-gonic/gin)
19-
have also been added by contributors as additional routers. We chose Echo because
20-
the `Context` object is a mockable interface, and it allows for some advanced
21-
testing.
18+
stubs, and [Chi](https://github.com/go-chi/chi), [Gin](https://github.com/gin-gonic/gin),
19+
and [gorilla/mux](https://github.com/gorilla/mux) have also been added by
20+
contributors as additional routers. We chose Echo because the `Context` object
21+
is a mockable interface, and it allows for some advanced testing.
2222

2323
This package tries to be too simple rather than too generic, so we've made some
2424
design decisions in favor of simplicity, knowing that we can't generate strongly

internal/test/schemas/schemas.gen.go

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

internal/test/schemas/schemas.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,20 @@ paths:
133133
application/json:
134134
schema:
135135
$ref: "#/components/schemas/DeprecatedProperty"
136-
136+
/issues/1051:
137+
get:
138+
operationId: Issue1051
139+
description: |
140+
Multiple media types contain JSON
141+
responses:
142+
'200':
143+
content:
144+
application/vnd.something.v1+json:
145+
schema:
146+
type: object
147+
application/json:
148+
schema:
149+
type: object
137150
components:
138151
schemas:
139152
GenericObject:

pkg/codegen/operations.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,18 @@ func (o *OperationDefinition) GetResponseTypeDefinitions() ([]ResponseTypeDefini
293293

294294
var typeName string
295295
switch {
296+
296297
// HAL+JSON:
297298
case StringInArray(contentTypeName, contentTypesHalJSON):
298299
typeName = fmt.Sprintf("HALJSON%s", ToCamelCase(responseName))
299-
// JSON:
300-
case StringInArray(contentTypeName, contentTypesJSON) || util.IsMediaTypeJson(contentTypeName):
300+
case "application/json" == contentTypeName:
301+
// if it's the standard application/json
301302
typeName = fmt.Sprintf("JSON%s", ToCamelCase(responseName))
303+
// Vendored JSON
304+
case StringInArray(contentTypeName, contentTypesJSON) || util.IsMediaTypeJson(contentTypeName):
305+
baseTypeName := fmt.Sprintf("%s%s", ToCamelCase(contentTypeName), ToCamelCase(responseName))
306+
307+
typeName = strings.ReplaceAll(baseTypeName, "Json", "JSON")
302308
// YAML:
303309
case StringInArray(contentTypeName, contentTypesYAML):
304310
typeName = fmt.Sprintf("YAML%s", ToCamelCase(responseName))

0 commit comments

Comments
 (0)