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

Use raw HTML content so example can be generated with Rails 7.1+ #189

Merged
merged 6 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2024-01-13 11:12:43 UTC using RuboCop version 1.50.2.
# on 2024-03-25 05:35:37 UTC using RuboCop version 1.62.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 9
# Offense count: 11
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 48

# Offense count: 2
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 192
Max: 207

# Offense count: 5
# Offense count: 8
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 13

# Offense count: 16
# Offense count: 19
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 31
Max: 34

# Offense count: 1
# Offense count: 3
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 13
Expand Down
7 changes: 5 additions & 2 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def build(context, example:)
description: description,
security: security,
status: response.status,
response_body: safe_parse_body(response),
response_body: safe_parse_body(response, response.media_type),
response_headers: response_headers,
response_content_type: response.media_type,
response_content_disposition: response.header['Content-Disposition'],
Expand All @@ -42,7 +42,10 @@ def build(context, example:)

private

def safe_parse_body(response)
def safe_parse_body(response, media_type)
# Use raw body, because Nokogiri-parsed HTML are modified (new lines injection, meta injection, and so on) :(
return response.body if media_type == 'text/html'
Fixed Show fixed Hide fixed

response.parsed_body
rescue JSON::ParserError
nil
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def build(record)

if record.response_body
disposition = normalize_content_disposition(record.response_content_disposition)

response[:content] = {
normalize_content_type(record.response_content_type) => {
schema: build_property(record.response_body, disposition: disposition),
Expand Down
9 changes: 9 additions & 0 deletions spec/rails/app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class PagesController < ApplicationController
def get
if params[:head] == '1'
head :no_content
else
render html: '<!DOCTYPE html><html lang="en"><head><title>Hello</title></head><body>Hello</body></html>'.html_safe
end
end
end
2 changes: 2 additions & 0 deletions spec/rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

get '/my_engine/test' => ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['ANOTHER TEST']] }

get '/pages' => 'pages#get'

defaults format: 'json' do
resources :tables, only: [:index, :show, :create, :update, :destroy]
resources :images, only: [:index, :show] do
Expand Down
15 changes: 15 additions & 0 deletions spec/rails/doc/smart/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ info:
servers:
- url: http://localhost:3000
paths:
"/pages":
get:
summary: get
tags:
- Page
responses:
'200':
description: return HTML
content:
"text/html":
schema:
type: string
example: '<!DOCTYPE html><html lang="en"><head><title>Hello</title></head><body>Hello</body></html>'
# '204':
# description: return no content
"/tables":
get:
summary: index
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/rails_smart_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@
end
end
end

RSpec.describe 'Pages', type: :request do
describe '#get' do
it 'return HTML' do
get '/pages'
expect(response.status).to eq(200)
end
end
end