Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Responses using references are not properly expanded #16965

Closed
5 of 6 tasks
dominicbarnes opened this issue Nov 2, 2023 · 2 comments · Fixed by #16986
Closed
5 of 6 tasks

[BUG] Responses using references are not properly expanded #16965

dominicbarnes opened this issue Nov 2, 2023 · 2 comments · Fixed by #16986

Comments

@dominicbarnes
Copy link

dominicbarnes commented Nov 2, 2023

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I've got an API that uses the same response format repeatedly, so I opted for using a $ref to avoid the repetition in my spec.

It appears that when you do this, the generator does not seem to correctly pick up the response schema, resulting in it being left out. I've tested this in the go and javascript generators, so I have a suspicion it's a more widespread issue.

The workaround here is to just avoid $ref and repeat the response config each time it is needed.

openapi-generator version

7.1.0-SNAPSHOT (latest docker image as of today)

OpenAPI declaration file content or url
openapi: 3.1.0

info:
  title: required to pass validation
  version: 0.0.0

paths:
  /no_ref:
    get:
      responses:
        '200':
          description: required to pass validation
          content:
            text/plain:
              schema:
                type: string

  /ref/no_ref:
    get:
      responses:
        '200':
          $ref: '#/components/responses/no_ref'

  /ref/ref:
    get:
      responses:
        '200':
          $ref: '#/components/responses/ref'

components:
  responses:
    no_ref:
      description: required to pass validation
      content:
        text/plain:
          schema:
            type: string

    ref:
      description: required to pass validation
      content:
        text/plain:
          schema:
            $ref: '#/components/schemas/simple_text'

  schemas:
    simple_text:
      type: string
Generation Details

I am doing this on macOS, so I've needed to use docker to work, but I suspect the equivalent CLI instructions should suffice. (there's no extra config to worry about)

docker run --rm -v "$PWD:/local" openapitools/openapi-generator-cli:latest generate \
	-g go \
	-i /local/spec.yaml \
 	-o /local/client/go

docker run --rm -v "$PWD:/local" openapitools/openapi-generator-cli:latest generate \
	-g javascript \
	-i /local/spec.yaml \
 	-o /local/client/javascript
Steps to reproduce

Once you have the generated code, here's what I've noticed:

go

The Execute() methods typically return (Model, *http.Response, error), which is found in api_default.go:

// no $ref: works as expected
func (r ApiNoRefGetRequest) Execute() (string, *http.Response, error) {

// uses $ref for response, but no $ref for content: missing model
func (r ApiRefNoRefGetRequest) Execute() (*http.Response, error) {

// uses $ref for response and content: missing model
func (r ApiRefRefGetRequest) Execute() (*http.Response, error) {
javascript

The returnType variables generated typically use the model model or null when the response content has no detected schema/model, which is found in src/api/DefaultApi.js

// no $ref: works as expected
let returnType = 'String';

// uses $ref for response, but no $ref for content: missing model
let returnType = null;

// uses $ref for response and content: missing model
let returnType = null;
Related issues/PRs

I have not yet found a ticket that seems to reference this issue as well, as most of my search criteria result in hundreds of issues as they are unfortunately rather common terms.

Suggest a fix

n/a

@wing328
Copy link
Member

wing328 commented Nov 5, 2023

openapi: 3.1.0

A workaround is to use 3.0.1 instead while we're working on better 3.1.0 support for the response schema.

@wing328
Copy link
Member

wing328 commented Nov 5, 2023

merged #16986 to fix the issue.

please give it a try with the latest master

@wing328 wing328 added this to the 7.1.0 milestone Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants