rspec-openapi icon indicating copy to clipboard operation
rspec-openapi copied to clipboard

API schema that is not subject to rspec execution is deleted.

Open srockstyle opened this issue 2 years ago • 1 comments

When running OPENAPI=1 rspec spec/requests/samples_spec.rb:15 is run, the schema that is not under test is also deleted.

request spec

describe 'Sample API TEST', :api_admin, type: :request do
  let(:headers) { { headersample: "headersample" } }
  let(:sample) { create(:sample, name: "srockstyle") }
  let(:params) { { } }

  before do
    restaurant
  end
  it 'Test A' do
    get "/v1/sample", headers: headers, params: params
    expect(response).to(have_http_status(200))
  end

  it 'Test B' do
    get "/v1/sample/#{sample.id}", headers: headers, params: params
    expect(response).to(have_http_status(200))
  end
end

openapi.yaml created after running OPENAPI=1 rspec spec/requests/samples_spec.rb

---
  openapi: 3.0.3
  info:
    title: app
    version: 1.0.0
  servers: []
  paths:
    "/v1/samples":
      post:
        summary: index
        tags:
        - Sample
        parameters:
          - name: id
            in: path
            required: true
            schema:
              type: integer
            example: 1
        responses:
          '200':
            description: Index sample
            content:
              application/json:
                schema:
                  type: array
                  items:
                    type: object
                    properties:
                      id:
                        type: integer
                      name:
                        type: string
                example:
                - id: 1
                  name: srockstyle
                  job: sample
    "/v1/samples/{id}":
      post:
        summary: index
        tags:
        - Sample
        responses:
          '200':
            description: Index sample
            content:
              application/json:
                schema:             
                  type: object
                  properties:
                    id:
                      type: integer
                    name:
                      type: string
                example:
                  id: 1
                  name: srockstyle
                  job: sample

Then spec was specified on a line and the command was executed OPENAPI=1 rspec spec/requests/samples_spec.rb:10, yaml left only the specified parts and deleted the parts that were not specified.

---
  openapi: 3.0.3
  info:
    title: app
    version: 1.0.0
  servers: []
  paths:
    "/v1/samples":
      post:
        summary: index
        tags:
        - Sample
        parameters:
          - name: id
            in: path
            required: true
            schema:
              type: integer
            example: 1
        responses:
          '200':
            description: Index sample
            content:
              application/json:
                schema:
                  type: array
                  items:
                    type: object
                    properties:
                      id:
                        type: integer
                      name:
                        type: string
                example:
                - id: 1
                  name: srockstyle
                  job: sample

I expect the following behavior, are there any options?

  • Do not delete schema that is not to be executed
  • get request" is processed as "post request", so we want to treat it as "get".

srockstyle avatar Dec 01 '22 00:12 srockstyle

This automatic deletion is a new behavior introduced in v0.7.0. In the previous versions, to delete the non-existent endpoints/parameters/fields in a request or response, users needed to delete the OpenAPI file and regenerate from zero. This is not great since manually-added parts are also erased. Or you might carefully delete the non-existent parts manually, which is time-consuming.

So I highly recommend running all specs to refresh your OpenAPI file in CI. I assume that most users are already running all specs in CI.

However, I also understand that users often run a subset of specs locally to confirm their changes. Automatic deletion is implemented here: https://github.com/exoego/rspec-openapi/blob/1e0eb93dbb0b0d80455894028da4964c3976ff74/lib/rspec/openapi/schema_cleaner.rb#L20-L38 My idea is a new option like OPENAPI_AUTOMATIC_DELETION=0 to disable automatic deletion.

exoego avatar Dec 01 '22 00:12 exoego