Skip to content

Commit 601b6c8

Browse files
@jessieay and @rmwjessieay
@jessieay and @rmw
authored andcommitted
Remove leading slashes from dirname
* When user names a resource with a leading slash, library was trying to create a directory at the root directory of the machine * Failure was at `lib/rspec_api_documentation/writers/json_writer.rb:18` (which is where dir is being made based on resource name) * This resulted in permissions errors that were hard to debug * This change strips leading slash when evaluating which dir to put docs into
1 parent dc861da commit 601b6c8

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/rspec_api_documentation/writers/json_writer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def respond_to?(method, include_private = false)
7474
end
7575

7676
def dirname
77-
resource_name.to_s.downcase.gsub(/\s+/, '_')
77+
resource_name.to_s.downcase.gsub(/\s+/, '_').sub(/^\//,'')
7878
end
7979

8080
def filename

spec/writers/json_example_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44
describe RspecApiDocumentation::Writers::JsonExample do
55
let(:configuration) { RspecApiDocumentation::Configuration.new }
66

7+
describe "#dirname" do
8+
it "strips out leading slashes" do
9+
example = double(resource_name: "/test_string")
10+
11+
json_example =
12+
RspecApiDocumentation::Writers::JsonExample.new(example, configuration)
13+
14+
expect(json_example.dirname).to eq "test_string"
15+
end
16+
17+
it "does not strip out non-leading slashes" do
18+
example = double(resource_name: "test_string/test")
19+
20+
json_example =
21+
RspecApiDocumentation::Writers::JsonExample.new(example, configuration)
22+
23+
expect(json_example.dirname).to eq "test_string/test"
24+
end
25+
end
26+
727
describe '#filename' do
828
specify 'Hello!/ 世界' do |example|
929
expect(described_class.new(example, configuration).filename).to eq("hello!_世界.json")

0 commit comments

Comments
 (0)