GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #1127

New issue
Copy link
New issue
Copy link
Closed
#1159
Closed
Multiple json content types in a single requestBody or response lead to duplicate types and client interface functions#1127
#1159
Copy link
Labels
awaiting reply

Description

@jlengelsen
jlengelsen
opened on Jun 28, 2023
Issue body actions
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

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)
}
}

Activity

  1. gimpf commented on Jun 29, 2023

    @gimpf
    gimpf
    on Jun 29, 2023
    More actions

    Happens to me, too.

    v1.11.0 did not have the problem, v1.12.0 did.

  2. jamietanna commented on Jun 29, 2023

    @jamietanna
    jamietanna
    on Jun 29, 2023
    Member
    More actions

    To confirm, does this create duplicate names, such that we'd see a compilation error, or is this "just" duplicated code?

  3. jamietanna
    added
    awaiting reply
    on Jun 29, 2023
  4. fikin commented on Jul 4, 2023

    @fikin
    fikin
    on Jul 4, 2023
    More actions

    any suggestions how to go about it?

  5. jlengelsen commented on Jul 8, 2023

    @jlengelsen
    jlengelsen
    on Jul 8, 2023
    Author
    More actions

    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.

  6. jamietanna commented on Jul 11, 2023

    @jamietanna
    jamietanna
    on Jul 11, 2023
    Member
    More actions

    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!

  7. sagikazarmark commented on Jul 29, 2023

    @sagikazarmark
    sagikazarmark
    on Jul 29, 2023
    More actions

    @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. :)

  8. jamietanna commented on Jul 31, 2023

    @jamietanna
    jamietanna
    on Jul 31, 2023
    Member
    More actions

    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

  9. jamietanna
    added 8 commits that reference this issue on Jul 31, 2023

    Add `/` to word separators for `ToCamelCase`

    7d4c623

    Fix: Allow multiple JSON types to be generated

    1d8a51b

    Add `/` to word separators for `ToCamelCase`

    3fe0bc0

    Fix: Allow multiple JSON types to be generated

    5dbff79

    Add `/` to word separators for `ToCamelCase`

    eaf357f

    Fix: Allow multiple JSON types to be generated

    c068a55

    Fix: Allow multiple JSON types to be generated

    911d90a

    Fix: Allow multiple JSON types to be generated

    90577f2
  10. jamietanna commented on Jul 31, 2023

    @jamietanna
    jamietanna
    on Jul 31, 2023
    Member
    More actions

    #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
  11. sagikazarmark commented on Jul 31, 2023

    @sagikazarmark
    sagikazarmark
    on Jul 31, 2023
    More actions

    @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.

  12. ghost closed this as completedin #1159on Jul 31, 2023
  13. ghost added a commit that references this issue on Jul 31, 2023

    Fix: Allow multiple JSON types to be generated (#1159)

    b248b1d
  14. jamietanna commented on Jul 31, 2023

    @jamietanna
    jamietanna
    on Jul 31, 2023
    Member
    More actions

    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: string
  15. jamietanna commented on Jul 31, 2023

    @jamietanna
    jamietanna
    on Jul 31, 2023
    Member
    More actions

    Released as https://github.com/deepmap/oapi-codegen/releases/tag/v1.13.2

  16. debuggerpk
    added a commit that references this issue on Sep 18, 2023

    Fix: Allow multiple JSON types to be generated (oapi-codegen#1159)

    461d9bf
  17. danicc097
    added a commit that references this issue on Aug 31, 2024

    Fix: Allow multiple JSON types to be generated (oapi-codegen#1159)

    2800a93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting reply

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.