Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions internal/test/strict-server/chi/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions internal/test/strict-server/client/client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 31 additions & 17 deletions internal/test/strict-server/echo/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 32 additions & 17 deletions internal/test/strict-server/fiber/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 32 additions & 17 deletions internal/test/strict-server/gin/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/test/strict-server/strict-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ paths:
oneOf:
- type: string
- $ref: "#/components/schemas/example"
application/alternative+json:
schema:
$ref: "#/components/schemas/example"
400:
$ref: "#/components/responses/badrequest"
default:
Expand Down
8 changes: 8 additions & 0 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ func (r ResponseContentDefinition) NameTagOrContentType() string {
return SchemaNameToTypeName(r.ContentType)
}

// IsJSON returns whether this is a JSON media type, for instance:
// - application/json
// - application/vnd.api+json
// - application/*+json
func (r ResponseContentDefinition) IsJSON() bool {
return util.IsMediaTypeJson(r.ContentType)
}

type ResponseHeaderDefinition struct {
Name string
GoName string
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/strict/strict-echo.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type strictHandler struct {
{{$multipleBodies := gt (len .Bodies) 1 -}}
{{range .Bodies -}}
{{if $multipleBodies}}if strings.HasPrefix(ctx.Request().Header.Get("Content-Type"), "{{.ContentType}}") { {{end}}
{{if eq .NameTag "JSON" -}}
{{if .IsJSON -}}
var body {{$opid}}{{.NameTag}}RequestBody
if err := ctx.Bind(&body); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/strict/strict-fiber-interface.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
{{end -}}
ctx.Status({{if $fixedStatusCode}}{{$statusCode}}{{else}}response.StatusCode{{end}})
{{$hasBodyVar := or ($hasHeaders) (not $fixedStatusCode) (not .IsSupported)}}
{{if eq .NameTag "JSON" -}}
{{if .IsJSON }}
{{$hasUnionElements := ne 0 (len .Schema.UnionElements)}}
return ctx.JSON(&{{if $hasBodyVar}}response.Body{{else}}response{{end}}{{if $hasUnionElements}}.union{{end}})
{{else if eq .NameTag "Text" -}}
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/strict/strict-fiber.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type strictHandler struct {
{{$multipleBodies := gt (len .Bodies) 1 -}}
{{range .Bodies -}}
{{if $multipleBodies}}if strings.HasPrefix(string(ctx.Request().Header.ContentType()), "{{.ContentType}}") { {{end}}
{{if eq .NameTag "JSON" -}}
{{if .IsJSON }}
var body {{$opid}}{{.NameTag}}RequestBody
if err := ctx.BodyParser(&body); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/strict/strict-gin.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type strictHandler struct {
{{$multipleBodies := gt (len .Bodies) 1 -}}
{{range .Bodies -}}
{{if $multipleBodies}}if strings.HasPrefix(ctx.GetHeader("Content-Type"), "{{.ContentType}}") { {{end}}
{{if eq .NameTag "JSON" -}}
{{if .IsJSON }}
var body {{$opid}}{{.NameTag}}RequestBody
if err := ctx.ShouldBind(&body); err != nil {
ctx.Status(http.StatusBadRequest)
Expand Down
Loading