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

OpenAPI Emitter: Response should respect @useRef #3601

Closed
3 tasks done
mcchrish opened this issue Jun 17, 2024 · 6 comments
Closed
3 tasks done

OpenAPI Emitter: Response should respect @useRef #3601

mcchrish opened this issue Jun 17, 2024 · 6 comments
Assignees
Labels
bug Something isn't working emitter:openapi3 Issues for @typespec/openapi3 emitter triaged:core
Milestone

Comments

@mcchrish
Copy link

Clear and concise description of the problem

namespace Pet Service {
  @get
  op get(@path petId: string): CustomResponse;
}

@useRef("https://example.com/example.yml")
model CustomResponse {
  @statusCode
  status: 200;
}

Should compile to:

      responses:
        '200':
          $ref: 'https://example.com/example.yml'

Checklist

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
@markcowl markcowl added the bug Something isn't working label Jun 18, 2024
@markcowl markcowl added this to the [2024] July milestone Jun 18, 2024
@markcowl
Copy link
Contributor

p2

@markcowl markcowl added emitter:openapi3 Issues for @typespec/openapi3 emitter triaged:core labels Jun 18, 2024
@markcowl markcowl modified the milestones: [2024] July, [2024] August Jul 15, 2024
@sarangan12
Copy link
Contributor

Playground Link

@sarangan12
Copy link
Contributor

sarangan12 commented Jul 17, 2024

In the package openapi3, the following test will succeed.

it("set external reference model", async () => {
  const [{ Foo }, diagnostics] = await runner.compileAndDiagnose(`
        @test @useRef("https://example.com/example.yml")
        model Foo {
          @statusCode
          status: 200;
        }
      `);
  expectDiagnosticEmpty(diagnostics);
  strictEqual(getRef(runner.program, Foo), "https://example.com/example.yml");
});

So, there is no problem in setting the reference. The reference is set correctly. But, it is not written to the output. i.e. this test will error out:

describe("openapi3: useRef decorator", () => {
  it("adds a ref extension to a model", async () => {
    const oapi = await openApiFor(`
      @test @useRef("../common.json#/definitions/Foo")
      model Foo {
        @statusCode
        status: 200;
        name: string;
      }
      @get op get(): Foo;
    `);
    ok(oapi.paths["/"].get);
    strictEqual(oapi.paths["/"].get.responses["200"]["$ref"], "../common.json#/definitions/Foo"); // <--- Will error out here
  });
});

github-merge-queue bot pushed a commit that referenced this issue Jul 18, 2024
Per the requirement in [Issue
3601](#3601), the 'useRef'
plugin must be respected and used correctly. This PR does that.

**Before Changes**

![image](https://github.com/user-attachments/assets/be4bdeb3-4c1e-4f4a-a1ed-75abfee3f98c)

**After Changes**

![image](https://github.com/user-attachments/assets/55978f2b-25d4-4e85-91a1-4b313ade62de)

---------

Co-authored-by: Timothee Guerin <timothee.guerin@outlook.com>
@sarangan12
Copy link
Contributor

Code Complete

@chrisradek
Copy link
Member

Closed by #3894

@wingfaic
Copy link

Should $ref be emitted without siblings? (specifically the built in response descriptions which can't be removed)

Otherwise it'll cause this default spectral rule to fail https://docs.stoplight.io/docs/spectral/4dec24461f3af-open-api-rules#no-ref-siblings.

Example

@useRef("https://example.com")
model Error {
  @statusCode _: 500;
}

@useRef("https://example.com")
model Response {}

@route("/get")
op get(): Response | Error;

0.59.0-dev.4 Currently outputs

get:
  operationId: get
  parameters: []
  responses:
    '200':
      description: The request has succeeded.
      $ref: https://example.com
    '500':
      description: Server error
      $ref: https://example.com

But should be

get:
  operationId: get
  parameters: []
  responses:
    '200':
      $ref: https://example.com
    '500':
      $ref: https://example.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working emitter:openapi3 Issues for @typespec/openapi3 emitter triaged:core
Projects
None yet
Development

No branches or pull requests

5 participants