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]: invalid openapi output running from github actions on ubuntu-latest #3397

Closed
4 tasks done
royberris opened this issue May 18, 2024 · 3 comments · Fixed by #3516
Closed
4 tasks done

[Bug]: invalid openapi output running from github actions on ubuntu-latest #3397

royberris opened this issue May 18, 2024 · 3 comments · Fixed by #3516
Assignees
Labels
bug Something isn't working compiler:emitter-framework Issues for the emitter framework
Milestone

Comments

@royberris
Copy link

royberris commented May 18, 2024

Describe the bug

When running the tsp compiler from github actions it produced an invalid openapi file. When I try to parse that schema in to https://editor.swagger.io/ it gives me compile erros.

I am trying to compare a typespec generated openapi file with my dotnet project's openapi file. Using json2yaml and openapi-diff. When parsing to openapi-diff I get a parse error. Also errors are occuring on the editor.swagger.io.

Then I started comparing the output files from the github actions container (running the action locally with act) and also the output from running it from windows.

Below is from my scenario, but in the reproduction section I show the issue with the different output.

All code is here: https://github.com/royberris/TypeSpec.NET
You can see the pipeline failing: https://github.com/royberris/TypeSpec.NET/actions/runs/9169657101/job/25210475358

Reproduction

Workflow:

jobs:
  compare-openapi:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '20'

    - name: Install TypeSpec CLI
      run: npm install -g @typespec/compiler

   - name: NPM install
      run: |
        cd typespec
        npm install

   - name: Generate TypeSpec OpenAPI spec
      run: |
        tsp compile typespec/main.tsp

The main.tsp

import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi3";
import "@typespec/versioning";

using TypeSpec.Http;
using TypeSpec.Versioning;

@service({
  title: "Weather API",
})
@route("/api/weather")
@versioned(Versions)
namespace Weather {
    enum Versions {
        "v1"
    }

    model WeatherResult {
        name: string;
        givenDate?: plainDate;
    }

    @get
    @route("/weather")
    op getWeather(@query location: string, @query date?: plainDate): {
        @statusCode statusCode: 200;
        @body result: WeatherResult;
    };
}

The config

emit:
  - "@typespec/openapi3"

The output running TypeSpec from a windows environment:

openapi: 3.0.0
info:
  title: Weather API
  version: v1
tags: []
paths:
  /api/weather/weather:
    get:
      operationId: getWeather
      parameters:
        - name: location
          in: query
          required: true
          schema:
            type: string
        - name: date
          in: query
          required: false
          schema:
            type: string
            format: date
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/WeatherResult'
components:
  schemas:
    Versions:
      type: string
      enum:
        - v1
    WeatherResult:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        givenDate:
          type: string
          format: date

Running it from the github actions:

openapi: 3.0.0
info:
  title: Weather API
  version: v1
tags: []
paths:
  /api/weather/weather:
    get:
      operationId: getWeather
      parameters:
        - name: location
          in: query
          required: true
          schema:
            type: string
        - name: date
          in: query
          required: false
          schema:
            type: string
            format: date
      responses:
        '200':
          description: The request has succeeded.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/WeatherResult'
components:
  schemas:
    Versions:
      type: string
      enum:
        - v1
    WeatherResult:
      type: object
      required:
        - name
      properties:
        value:
          name:
            value:
              type: string
            kind: code
          givenDate:
            value:
              type: string
              format: date
            kind: code
        kind: code

Checklist

@royberris royberris added the bug Something isn't working label May 18, 2024
@markcowl markcowl added the compiler:emitter-framework Issues for the emitter framework label May 20, 2024
@markcowl markcowl added this to the [2024] July milestone May 20, 2024
@timotheeguerin
Copy link
Member

@royberris the problem here seems to be it is loading the global compiler version you have instead of hte one defined in the typespec folder. I think as a workaround for now you can run tsp compile . from the typespec folder instead of above

@royberris
Copy link
Author

@timotheeguerin that does seem to resolve the issue. But still unexpected that it outputs that WeatherResult object with the extra value property and the kind properties. Is this because when it is outside the typespec folder it doesn't know the configuration etc?

@timotheeguerin
Copy link
Member

That is an issue because it is doing some instance of comparaison but loading a différent version of the class. Will keep it open because there is a few things we can do here.

  1. Have the openapi emitter create its own emitter framework
  2. Resolve the compiler to use from the main file

@timotheeguerin timotheeguerin self-assigned this Jul 1, 2024
heaths pushed a commit to heaths/typespec that referenced this issue Jul 1, 2024
…directly (microsoft#3516)

fix microsoft#3397

Problem with calling `getAssetEmitter` is it create an asset emitter
with the instance of the compiler used in the compilation and not the
instance of the compiler defined in the type emitter necessarly. This
cause issue with `instanceof` checks which are then not the exact same
class as its loaded form different instance of the compiler
Calling `createAssetEmitter` solve the issue because it is imported in
teh context of the emitter package and will use the emitter package
version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler:emitter-framework Issues for the emitter framework
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants