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

Is it possible to generate only a single API endpoint? #355

Open
AmberWilkie opened this issue Aug 17, 2017 · 9 comments · May be fixed by #472
Open

Is it possible to generate only a single API endpoint? #355

AmberWilkie opened this issue Aug 17, 2017 · 9 comments · May be fixed by #472

Comments

@AmberWilkie
Copy link

Every time we change one route, we have to update all of the docs. It would be lovely to be able to update only one file (or based on one acceptance test file, etc.). My apologies in advance if the answer is obvious and I failed at finding it.

@simeonwillbanks
Copy link

I'm experiencing the same issue.

How about a Pull Request which introduces a pattern option to the rake tasks?

rake docs:generate['spec/acceptance/user_spec.rb']
rake docs:generate['spec/acceptance/v1/*_spec.rb']
rake docs:generate:ordered['spec/acceptance/user_spec.rb']
rake docs:generate:ordered['spec/acceptance/v1/*_spec.rb']

@simeonwillbanks
Copy link

@iamvery suggested an environment variable instead of an argument since rake task arguments can be tricky across different shells. e.g. bash and zsh

rake docs:generate SPEC_PATH_PATTERN=spec/acceptance/user_spec.rb

@iamvery
Copy link

iamvery commented Jan 18, 2018

(the idea comes from other uses I've seen, e.g. bin/rails db:migrate:down VERSION=xxxx; also HI 👋 )

@adelnabiullin
Copy link

adelnabiullin commented Oct 23, 2018

At this moment there is a possibility to use this to generate single endpoint: https://github.com/zipmark/rspec_api_documentation#append_json

But unfortunately it does not work. I did everything like documented there, but got following error:

/Users/username/src/projectname/.bundle/ruby/2.5.0/gems/rspec_api_documentation-6.1.0/lib/rspec_api_documentation/writers/append_json_writer.rb:31:in 'block in as_json': undefined method 'section_hash' for #<RspecApiDocumentation::Writers::AppendJsonIndex:0x00007fa8aa999f30> (NoMethodError)

Steps to reproduce:

  1. spec/spec_helper.rb
ENV["DOC_FORMAT"] ||= "json"
RspecApiDocumentation.configure do |config|
  config.format = ENV["DOC_FORMAT"]
end
  1. lib/tasks/docs.rake
desc 'Generate API request documentation from API specs (by appending to existing)'
RSpec::Core::RakeTask.new('docs:generate:append', :spec_file) do |t, task_args|
  if spec_file = task_args[:spec_file]
    ENV["DOC_FORMAT"] = "append_json"
  end
  t.pattern    = spec_file || 'spec/acceptance/**/*_spec.rb'
  t.rspec_opts = ["--format RspecApiDocumentation::ApiFormatter"]
end
  1. then run the command:
rake docs:generate:append\[spec/acceptance/orders_spec.rb\]
  1. got an error mentioned below

@oestrich
Copy link
Contributor

Looks like the append json format broke sometime recently. If you're interested in fixing it that'd be a great PR! I don't use append_json, so I don't know when I'll get a chance to look at this otherwise.

@li-thy-um
Copy link
Contributor

li-thy-um commented Feb 22, 2019

here is my solution to generate Markdown file with SPEC_PATH_PATTERN

---
 lib/tasks/docs.rake                     | 11 +++++++++++
 spec/support/rspec_api_documentation.rb | 30 ++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/lib/tasks/docs.rake b/lib/tasks/docs.rake
new file mode 100644
index 00000000..fec2e566
--- /dev/null
+++ b/lib/tasks/docs.rake
@@ -0,0 +1,11 @@
+require 'rspec/core/rake_task'
+
+desc 'Generate API request documentation from API specs'
+RSpec::Core::RakeTask.new('docs:generate:append') do |t|
+  if ENV['SPEC_PATH_PATTERN']
+    t.pattern = ENV['SPEC_PATH_PATTERN']
+  else
+    t.pattern = 'spec/acceptance/**/*_spec.rb'
+  end
+  t.rspec_opts = ["--format RspecApiDocumentation::ApiFormatter"]
+end
diff --git a/spec/support/rspec_api_documentation.rb b/spec/support/rspec_api_documentation.rb
new file mode 100644
index 00000000..af62a9a6
--- /dev/null
+++ b/spec/support/rspec_api_documentation.rb
@@ -0,0 +1,30 @@
+module RspecApiDocumentation
+  module Writers
+    class MarkdownWriter
+      INDEX_FILE_NAME = 'index'
+
+      def self.clear_docs(docs_dir)
+        nil #noop
+      end
+
+      # Write out the generated documentation
+      def write
+        if render_options.fetch(:index, true) && !ENV['SPEC_PATH_PATTERN']
+          File.open(configuration.docs_dir.join(index_file_name + '.' + extension), "w+") do |f|
+            f.write markup_index_class.new(index, configuration).render
+          end
+        end
+
+        if render_options.fetch(:examples, true) && ENV['SPEC_PATH_PATTERN']
+          index.examples.each do |example|
+            markup_example = markup_example_class.new(example, configuration)
+            FileUtils.mkdir_p(configuration.docs_dir.join(markup_example.dirname))
+            File.open(configuration.docs_dir.join(markup_example.dirname, markup_example.filename), "w+") do |f|
+              f.write markup_example.render
+            end
+          end
+        end
+      end
+    end
+  end
+end

then run

alias gendoc='RAILS_ENV=test bundle exec rails docs:generate:append'
gendoc SPEC_PATH_PATTERN='./spec/acceptance/chars_spec.rb' && gendoc

it would print

Generating API Docs
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}
  Chars
  GET /chars
    * 简繁体对照表
  PUT /chars
    * 整体更新简繁体对照表

Top 2 slowest examples (0.9137 seconds, 84.0% of total time):
  Chars GET /chars 简繁体对照表
    0.83632 seconds ./spec/acceptance/chars_spec.rb:18
  Chars PUT /chars 整体更新简繁体对照表
    0.07738 seconds ./spec/acceptance/chars_spec.rb:37

Finished in 1.09 seconds (files took 4.77 seconds to load)
2 examples, 0 failures

..............

and file changes

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   "doc/api/chars/\346\225\264\344\275\223\346\233\264\346\226\260\347\256\200\347\271\201\344\275\223\345\257\271\347\205\247\350\241\250.markdown"
	modified:   "doc/api/chars/\347\256\200\347\271\201\344\275\223\345\257\271\347\205\247\350\241\250.markdown"
	modified:   doc/api/index.markdown

@mzemel
Copy link

mzemel commented Feb 28, 2019

👋 Submitted this PR. Could anyone having problems try it and see if append_json now works?

#433

Thanks!

@knzconnor
Copy link

It's a loading/inheritance issue. If you change

AppendJsonIndex < JsonIndex

to

AppendJsonIndex < JSONIndex

it works. Maybe ruby version differences in some super logic.

@knzconnor
Copy link

knzconnor commented Nov 22, 2019

@oestrich any chance either of making that tiny change, or that a PR would get accepted for it? Maintaining a fork or a monkey patch for three letters case change is a bit bonkers.

EugZol added a commit to EugZol/rspec_api_documentation that referenced this issue Apr 8, 2020
@EugZol EugZol linked a pull request Apr 8, 2020 that will close this issue
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 a pull request may close this issue.

8 participants