GeezerVison is showing github.com — Multiple json content types in a single requestBody or response lead to duplicate types and client interface functions · Issue #1127 · oapi-codegen/oapi-codegen
Multiple json content types in a single requestBody or response lead to duplicate types and client interface functions #1127Descriptionopenapi: 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
Generated Client Boilerplate:
// CreateWhateverJSONRequestBody defines body for CreateWhatever for application/*+json ContentType.
type CreateWhateverJSONRequestBody = Whatever
// CreateWhateverJSONRequestBody defines body for CreateWhatever for application/json ContentType.
type CreateWhateverJSONRequestBody = Whatever
// CreateWhateverJSONRequestBody defines body for CreateWhatever for application/json-patch+json ContentType.
type CreateWhateverJSONRequestBody = Whatever
...
// The interface specification for the client above.
type ClientInterface interface {
// CreateWhatever request with any body
CreateWhateverWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error)
CreateWhatever(ctx context.Context, body CreateWhateverJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
CreateWhatever(ctx context.Context, body CreateWhateverJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
CreateWhatever(ctx context.Context, body CreateWhateverJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error)
...
// ClientWithResponsesInterface is the interface specification for the client with responses above.
type ClientWithResponsesInterface interface {
// CreateWhatever request with any body
CreateWhateverWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*CreateWhateverResponse, error)
CreateWhateverWithResponse(ctx context.Context, body CreateWhateverJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateWhateverResponse, error)
CreateWhateverWithResponse(ctx context.Context, body CreateWhateverJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateWhateverResponse, error)
CreateWhateverWithResponse(ctx context.Context, body CreateWhateverJSONRequestBody, reqEditors ...RequestEditorFn) (*CreateWhateverResponse, error)
}
}Activitygimpf commented on Jun 29, 2023 Happens to me, too.
v1.11.0 did not have the problem, v1.12.0 did. jamietanna commented on Jun 29, 2023 To confirm, does this create duplicate names, such that we'd see a compilation error, or is this "just" duplicated code? fikin commented on Jul 4, 2023 any suggestions how to go about it? jlengelsen commented on Jul 8, 2023
To confirm, does this create duplicate names, such that we'd see a compilation error, or is this "just" duplicated code?
It creates duplicate names and compilation errors. jamietanna commented on Jul 11, 2023 Hey, sorry yeah so we'll need to get a patch to deal with this - hopefully will be in next couple of weeks, thanks for bearing with! sagikazarmark commented on Jul 29, 2023 @jamietanna I don't if you had the time to look into this issue, but if we can agree to a concept, I can try to contribute a fix.
I have two ideas so far:
- Use
any and check types runtime so that the same function can accept different bodies (basically drop build time type safety)
- Create an interface with a private function (so that outside of the package noone can implement the interface) and implement it on supported types
I realize that none of those are perfect solutions, so if anyone has any better ideas, please don't keep them to yourself. :) jamietanna commented on Jul 31, 2023 Sorry for the delay with this - I meant to get it shipped in the v1. 13.1 patch release just now, but slipped my mind - am working on a fix now which should be out today jamietanna commented on Jul 31, 2023 #1159 should resolve this - will see what Marcin thinks and if this works for y'all too 😄 Reacted by Markus, Luax and Márk Sági-Kazár sagikazarmark commented on Jul 31, 2023 @jamietanna does this PR cover the case when different media types do not actually use the same schema? I mean, that's kind of the point, but from what I can tell in the example, each media types use the same schema. jamietanna commented on Jul 31, 2023 Yes @sagikazarmark - for instance with the following diff:
diff --git internal/test/issues/issue-1127/api.gen.go internal/test/issues/issue-1127/api.gen.go
index 6b03196..6800663 100644
--- internal/test/issues/issue-1127/api.gen.go
+++ internal/test/issues/issue-1127/api.gen.go
@@ -8,8 +8,13 @@ type Whatever struct {
SomeProperty *string `json:"someProperty,omitempty"`
}
+// Whatever2 defines model for Whatever2.
+type Whatever2 struct {
+ SomeProperty *string `json:"someProperty,omitempty"`
+}
+
// CreateWhateverApplicationWildcardPlusJSONRequestBody defines body for CreateWhatever for application/*+json ContentType.
-type CreateWhateverApplicationWildcardPlusJSONRequestBody = Whatever
+type CreateWhateverApplicationWildcardPlusJSONRequestBody = Whatever2
// CreateWhateverJSONRequestBody defines body for CreateWhatever for application/json ContentType.
type CreateWhateverJSONRequestBody = Whatever
diff --git internal/test/issues/issue-1127/spec.yaml internal/test/issues/issue-1127/spec.yaml
index e3eee06..409fb88 100644
--- internal/test/issues/issue-1127/spec.yaml
+++ internal/test/issues/issue-1127/spec.yaml
@@ -19,7 +19,7 @@ paths:
$ref: "#/components/schemas/Whatever"
application/*+json:
schema:
- $ref: "#/components/schemas/Whatever"
+ $ref: "#/components/schemas/Whatever2"
responses:
201:
description: Whatever created
@@ -40,3 +40,8 @@ components:
properties:
someProperty:
type: string
+ Whatever2:
+ type: object
+ properties:
+ someProperty:
+ type: stringjamietanna commented on Jul 31, 2023
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|