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

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

Open
srockstyle opened this issue Dec 1, 2022 · 1 comment
Open
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@srockstyle
Copy link

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".
@exoego
Copy link
Owner

exoego commented Dec 1, 2022

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:

def cleanup!(base, spec)
# cleanup URLs
cleanup_hash!(base, spec, 'paths.*')
# cleanup HTTP methods
cleanup_hash!(base, spec, 'paths.*.*')
# cleanup parameters
cleanup_array!(base, spec, 'paths.*.*.parameters', %w[name in])
# cleanup requestBody
cleanup_hash!(base, spec, 'paths.*.*.requestBody.content.application/json.schema.properties.*')
cleanup_hash!(base, spec, 'paths.*.*.requestBody.content.application/json.example.*')
# cleanup responses
cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.schema.properties.*')
cleanup_hash!(base, spec, 'paths.*.*.responses.*.content.application/json.example.*')
base
end

My idea is a new option like OPENAPI_AUTOMATIC_DELETION=0 to disable automatic deletion.

@exoego exoego added enhancement New feature or request good first issue Good for newcomers labels Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants