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

Update schema_cleaner.rb #118

Merged
merged 2 commits into from
Jun 27, 2023
Merged

Update schema_cleaner.rb #118

merged 2 commits into from
Jun 27, 2023

Conversation

AlexeyMatskevich
Copy link
Contributor

    45|       *RSpec::OpenAPI::HashHelper.matched_paths_deeply_nested(base, 'paths', 'properties'),
    46|     ]
    47|     paths_to_objects.each do |path|
    48|       parent = base.dig(*path.take(path.length - 1))
    49|       # "required" array  must not be present if empty
=>  50|       debugger unless parent['required']
    51|       parent.delete('required') if parent['required'].empty?
    52|     end
    53|   end
    54| 
=>#0    block {|path=["paths", "/reviewer_admin/api/questions/...|} in cleanup_empty_required_array! at /usr/local/bundle/gems/rspec-openapi-0.8.1/lib/rspec/openapi/schema_cleaner.rb:50
  #1    [C] Array#each at /usr/local/bundle/gems/rspec-openapi-0.8.1/lib/rspec/openapi/schema_cleaner.rb:47
  # and 23 frames (use `bt' command for all frames)
(rdbg) parent
{"type"=>"object", "properties"=>{}}
(ruby) parent['required']
nil
(ruby) parent['required'].empty?
eval error: undefined method `empty?' for nil:NilClass

undefined method `empty?' for nil:NilClass
@codecov
Copy link

codecov bot commented Jun 16, 2023

Codecov Report

Merging #118 (f2d78d0) into master (bf8ac1e) will not change coverage.
The diff coverage is 0.00%.

@@           Coverage Diff           @@
##           master     #118   +/-   ##
=======================================
  Coverage   65.88%   65.88%           
=======================================
  Files          31       31           
  Lines         686      686           
=======================================
  Hits          452      452           
  Misses        234      234           
Impacted Files Coverage Δ
lib/rspec/openapi/schema_cleaner.rb 20.83% <0.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Apply rubocop offenses
@exoego
Copy link
Owner

exoego commented Jun 16, 2023

Would you elaborate the test code and OpenAPI file cause that error?

@AlexeyMatskevich
Copy link
Contributor Author

Yes, i just run OPENAPI=1 rspec, and I get an error:

An error occurred in an `after(:suite)` hook.
Failure/Error: parent.delete('required') if parent['required'].empty?

NoMethodError:
  undefined method `empty?' for nil:NilClass

I don't know with which openapi specs and with which tests (rspec) this happens, because the error occurs after the suite.
I use rspec-openapi 0.8.1 version.

@AlexeyMatskevich
Copy link
Contributor Author

AlexeyMatskevich commented Jun 16, 2023

I find some example ( I use Hanami 2.0 and rom.rb)

# [openapi](openapi: {authenticate: true}) it's self written plugin for rspec-openapi
RSpec.describe "AnswerAttempts#create", type: :request, openapi: {authenticate: true} do
  let(:test) { ReviewerFactory[:test] }
  let(:block) { ReviewerFactory[:block] }
  let(:question) { ReviewerFactory[:question, block: block] }
  let!(:correct_answer) { ReviewerFactory[:answer_option, question: question, correct: true] }
  let!(:wrong_answer) { ReviewerFactory[:answer_option, question: question] }
  let(:session) do
    Reviewer::Persistence::Repositories::Sessions.new.sessions.command(:create).call(
      {
        started_at: Time.now, 
        finished_at: Time.now + Duration.new(minutes: 30).total
      }
    )
  end
  let(:data) {
    { 
      data: {
        answer_option_ids: [
          correct_answer.id,
          wrong_answer.id
        ]
      }
    }
  }
  let(:user) { { login: "email5@test.com", password: "CrypTyPassword123456" } }

  before do
    Reviewer::Persistence::Repositories::TestBlocks.new.test_blocks.command(:create).call({ test_id: test.id, block_id: block.id})

    Reviewer::Authentication::App.rodauth(:reviewer).create_account(user)
    DB[:accounts].where(email: user[:login]).update(status: 2)

    account = Reviewer::Persistence::Repositories::Accounts.new.accounts.where(email: "email5@test.com").one
    Reviewer::Persistence::Repositories::AccountTests.new.account_tests.command(:create).call(
      {
        test_id: test.id, 
        account_id: account.id, 
        session_id: session.id 
      }
    )
  end

  context "when user is authenticated", openapi: {enable_example: false} do
    before do
      json_post "/reviewer/auth/login", user

      json_post "/reviewer/api/questions/#{question.id}/answer_attempts", data
    end

    it "has status 201" do
      expect(last_response.status).to eq(201)
    end
  end

  context "when user is not authenticated" do
    before { json_post "/reviewer/api/questions/#{question.id}/answer_attempts" }
    it "has status 401" do
      expect(last_response.status).to eq(401)
    end
  end
end

and openapi spec for that

---
openapi: 3.0.3
info:
  title: app
  version: 0.0.1
servers:
- url: http://localhost:2300
paths:
  "/reviewer/api/questions/{question_id}/answer_attempts":
    post:
      summary: POST /reviewer/api/questions/{question_id}/answer_attempts
      tags:
      - questions
      security:
      - CookieAuth: []
      parameters:
      - name: question_id
        in: path
        required: true
        schema:
          type: integer
        example: 2
      requestBody:
        content:
          application/json:
            schema:
              type: object
              properties: {}
            examples:
              Closed question:
                value:
                  data:
                    answer_option_ids:
                    - 31
                    - 32
              Open question:
                value:
                  data:
                    answer: Test answer text for open question
      responses:
        '401':
          description: when user is not authenticated it has status 401
        '201':
          description: when user is authenticated it has status 201
components:
  securitySchemes:
    CookieAuth:
      type: apiKey
      in: cookie
      name: roda.session

I run OPENAPI=1 rspec spec/requests/slices/reviewer/actions/answer_attempts/create_spec.rb and got the same error.

@exoego
Copy link
Owner

exoego commented Jun 27, 2023

I tried to add test cases for this but could not.
Merging as-is and will figure out later.
Thanks!

@exoego exoego merged commit f70e40d into exoego:master Jun 27, 2023
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants