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
24 changes: 12 additions & 12 deletions internal/test/client/client.gen.go

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

18 changes: 18 additions & 0 deletions internal/test/issues/issue-1127/api.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/issues/issue-1127/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package issue1127

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --config=server.config.yaml spec.yaml
5 changes: 5 additions & 0 deletions internal/test/issues/issue-1127/server.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package: issue1127
output:
api.gen.go
generate:
models: true
42 changes: 42 additions & 0 deletions internal/test/issues/issue-1127/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
openapi: 3.0.1
info:
title: api
version: "0.1.0"
paths:
/whatever:
post:
operationId: CreateWhatever
requestBody:
content:
application/json-patch+json:
schema:
$ref: "#/components/schemas/Whatever"
application/json:
schema:
$ref: "#/components/schemas/Whatever"
text/json:
schema:
$ref: "#/components/schemas/Whatever"
application/*+json:
schema:
$ref: "#/components/schemas/Whatever"
responses:
201:
description: Whatever created
content:
text/plain:
schema:
$ref: "#/components/schemas/Whatever"
application/json:
schema:
$ref: "#/components/schemas/Whatever"
text/json:
schema:
$ref: "#/components/schemas/Whatever"
components:
schemas:
Whatever:
type: object
properties:
someProperty:
type: string
18 changes: 15 additions & 3 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,15 @@ func (r RequestBodyDefinition) Suffix() string {

// IsSupportedByClient returns true if we support this content type for client. Otherwise only generic method will ge generated
func (r RequestBodyDefinition) IsSupportedByClient() bool {
return r.NameTag == "JSON" || r.NameTag == "Formdata" || r.NameTag == "Text"
return r.IsJSON() || r.NameTag == "Formdata" || r.NameTag == "Text"
}

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

// IsSupported returns true if we support this content type for server. Otherwise io.Reader will be generated
Expand Down Expand Up @@ -642,9 +650,11 @@ func GenerateBodyDefinitions(operationID string, bodyOrRef *openapi3.RequestBody
var defaultBody bool

switch {
case util.IsMediaTypeJson(contentType):
case contentType == "application/json":
tag = "JSON"
defaultBody = true
case util.IsMediaTypeJson(contentType):
tag = mediaTypeToCamelCase(contentType)
case strings.HasPrefix(contentType, "multipart/"):
tag = "Multipart"
case contentType == "application/x-www-form-urlencoded":
Expand Down Expand Up @@ -742,8 +752,10 @@ func GenerateResponseDefinitions(operationID string, responses openapi3.Response
content := response.Content[contentType]
var tag string
switch {
case util.IsMediaTypeJson(contentType):
case contentType == "application/json":
tag = "JSON"
case util.IsMediaTypeJson(contentType):
tag = mediaTypeToCamelCase(contentType)
case contentType == "application/x-www-form-urlencoded":
tag = "Formdata"
case strings.HasPrefix(contentType, "multipart/"):
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/client.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *{{ $clientTypeName }}) {{$opid}}{{.Suffix}}(ctx context.Context{{genPar
// New{{$opid}}Request{{.Suffix}} calls the generic {{$opid}} builder with {{.ContentType}} body
func New{{$opid}}Request{{.Suffix}}(server string{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Request, error) {
var bodyReader io.Reader
{{if eq .NameTag "JSON" -}}
{{if .IsJSON -}}
buf, err := json.Marshal(body)
if err != nil {
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions pkg/codegen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ func replaceInitialism(s string) string {
})
}

// mediaTypeToCamelCase converts a media type to a PascalCase representation
func mediaTypeToCamelCase(s string) string {
// ToCamelCase doesn't - and won't - add `/` to the characters it'll allow word boundary
s = strings.Replace(s, "/", "_", 1)
// including a _ to make sure that these are treated as word boundaries by `ToCamelCase`
s = strings.Replace(s, "*", "Wildcard_", 1)
s = strings.Replace(s, "+", "Plus_", 1)

return ToCamelCaseWithInitialism(s)
}

// SortedSchemaKeys returns the keys of the given SchemaRef dictionary in sorted
// order, since Golang scrambles dictionary keys
func SortedSchemaKeys(dict map[string]*openapi3.SchemaRef) []string {
Expand Down