A platform for community discussion. Free, open, simple.
DEV: add api docs for discourse-calendar events index endpoint (#35400)
Kelvdaac912405c7
diff --git a/lib/tasks/api_docs.rake b/lib/tasks/api_docs.rakenew file mode 100644index 00000000000..b9479a9d409--- /dev/null+++ b/lib/tasks/api_docs.rake@@ -0,0 +1,34 @@+# frozen_string_literal: true++require "rspec/core/rake_task"++# Override the default rswag:specs:swaggerize task to include plugin API specs+namespace :rswag do+ namespace :specs do+ # Clear the existing task so we can redefine it+ Rake::Task["rswag:specs:swaggerize"].clear if Rake::Task.task_defined?("rswag:specs:swaggerize")++ desc "Generate Swagger JSON files from integration specs (including plugins)"+ RSpec::Core::RakeTask.new("swaggerize") do |t|+ # Automatically load plugins for API documentation+ ENV["LOAD_PLUGINS"] = "1" unless ENV["LOAD_PLUGINS"]++ # Include plugin API specs in the pattern+ t.pattern =+ ENV.fetch(+ "PATTERN",+ "spec/requests/**/*_spec.rb, spec/api/**/*_spec.rb, spec/integration/**/*_spec.rb, plugins/*/spec/requests/api/*_spec.rb",+ )++ additional_rspec_opts = ENV.fetch("ADDITIONAL_RSPEC_OPTS", "")++ t.rspec_opts = [additional_rspec_opts]++ if Rswag::Specs.config.rswag_dry_run+ t.rspec_opts += ["--format Rswag::Specs::SwaggerFormatter", "--dry-run", "--order defined"]+ else+ t.rspec_opts += ["--format Rswag::Specs::SwaggerFormatter", "--order defined"]+ end+ end+ end+enddiff --git a/plugins/discourse-calendar/spec/requests/api/events_spec.rb b/plugins/discourse-calendar/spec/requests/api/events_spec.rbnew file mode 100644index 00000000000..448b4668424--- /dev/null+++ b/plugins/discourse-calendar/spec/requests/api/events_spec.rb@@ -0,0 +1,218 @@+# frozen_string_literal: true+require "swagger_helper"++RSpec.describe "events" do+ before do+ Jobs.run_immediately!+ SiteSetting.calendar_enabled = true+ SiteSetting.discourse_post_event_enabled = true+ end++ path "/discourse-post-event/events.json" do+ get "List calendar events" do+ tags "Discourse Calendar - Events"+ operationId "listEvents"+ consumes "application/json"+ expected_request_schema = nil++ parameter name: :include_details,+ in: :query,+ required: false,+ schema: {+ type: :string,+ enum: %w[true false],+ },+ description: "Include detailed event information (creator, invitees, stats, etc.)"++ parameter name: :category_id,+ in: :query,+ required: false,+ schema: {+ type: :integer,+ },+ description: "Filter events by category ID"++ parameter name: :include_subcategories,+ in: :query,+ required: false,+ schema: {+ type: :string,+ enum: %w[true false],+ },+ description: "Include events from subcategories when filtering by category"++ parameter name: :post_id,+ in: :query,+ required: false,+ schema: {+ type: :integer,+ },+ description: "Filter to events associated with a specific post ID"++ parameter name: :attending_user,+ in: :query,+ required: false,+ schema: {+ type: :string,+ },+ description:+ "Filter to events where the specified user (username) has RSVP'd as going"++ parameter name: :before,+ in: :query,+ required: false,+ schema: {+ type: :string,+ format: "date-time",+ },+ description: "Return events starting before this date/time (ISO 8601 format)"++ parameter name: :after,+ in: :query,+ required: false,+ schema: {+ type: :string,+ format: "date-time",+ },+ description: "Return events starting after this date/time (ISO 8601 format)"++ parameter name: :order,+ in: :query,+ required: false,+ schema: {+ type: :string,+ enum: %w[asc desc],+ },+ description: "Sort order for events by start date (default: asc)"++ parameter name: :limit,+ in: :query,+ required: false,+ schema: {+ type: :integer,+ minimum: 1,+ maximum: 200,+ },+ description: "Maximum number of events to return (default: 200)"++ produces "application/json"++ response "200", "success response (basic)" do+ expected_response_schema = load_spec_schema("events_index_response")+ schema expected_response_schema++ fab!(:event) { Fabricate(:event, original_starts_at: 1.day.from_now) }++ it_behaves_like "a JSON endpoint", 200 do+ let(:expected_response_schema) { expected_response_schema }+ let(:expected_request_schema) { expected_request_schema }+ end+ end++ response "200", "success response (detailed)" do+ expected_response_schema = load_spec_schema("events_index_detailed_response")+ schema expected_response_schema++ fab!(:event) { Fabricate(:event, original_starts_at: 1.day.from_now) }++ let(:include_details) { "true" }++ it_behaves_like "a JSON endpoint", 200 do+ let(:expected_response_schema) { expected_response_schema }+ let(:expected_request_schema) { expected_request_schema }+ end+ end+ end+ end++ path "/discourse-post-event/events.ics" do+ get "Export calendar events in iCalendar format" do+ tags "Discourse Calendar - Events"+ operationId "exportEventsICS"+ consumes "application/json"+ expected_request_schema = nil++ parameter name: :category_id,+ in: :query,+ required: false,+ schema: {+ type: :integer,+ },+ description: "Filter events by category ID"++ parameter name: :include_subcategories,+ in: :query,+ required: false,+ schema: {+ type: :string,+ enum: %w[true false],+ },+ description: "Include events from subcategories when filtering by category"++ parameter name: :attending_user,+ in: :query,+ required: false,+ schema: {+ type: :string,+ },+ description:+ "Filter to events where the specified user (username) has RSVP'd as going"++ parameter name: :before,+ in: :query,+ required: false,+ schema: {+ type: :string,+ format: "date-time",+ },+ description: "Return events starting before this date/time (ISO 8601 format)"++ parameter name: :after,+ in: :query,+ required: false,+ schema: {+ type: :string,+ format: "date-time",+ },+ description: "Return events starting after this date/time (ISO 8601 format)"++ parameter name: :order,+ in: :query,+ required: false,+ schema: {+ type: :string,+ enum: %w[asc desc],+ },+ description: "Sort order for events by start date (default: asc)"++ parameter name: :limit,+ in: :query,+ required: false,+ schema: {+ type: :integer,+ minimum: 1,+ maximum: 200,+ },+ description: "Maximum number of events to return (default: 200)"++ produces "text/calendar"++ response "200", "iCalendar file" do+ schema type: :string, format: "binary"++ fab!(:event) { Fabricate(:event, original_starts_at: 1.day.from_now, name: "Test Event") }++ before { |example| submit_request(example.metadata) }++ it "returns iCalendar format" do+ expect(response.status).to eq(200)+ expect(response.content_type).to include("text/calendar")+ expect(response.body).to include("BEGIN:VCALENDAR")+ expect(response.body).to include("END:VCALENDAR")+ expect(response.body).to include("BEGIN:VEVENT")+ expect(response.body).to include("END:VEVENT")+ end+ end+ end+ end+enddiff --git a/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.json b/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.jsonnew file mode 100644index 00000000000..aea7c58b203--- /dev/null+++ b/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_detailed_response.json@@ -0,0 +1,231 @@+{+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "events": {+ "type": "array",+ "items": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "category_id": {+ "type": ["integer", "null"]+ },+ "name": {+ "type": ["string", "null"]+ },+ "recurrence": {+ "type": ["string", "null"]+ },+ "recurrence_until": {+ "type": ["string", "null"],+ "format": "date-time"+ },+ "starts_at": {+ "type": ["string", "null"],+ "format": "date-time"+ },+ "ends_at": {+ "type": ["string", "null"],+ "format": "date-time"+ },+ "rrule": {+ "type": "string"+ },+ "show_local_time": {+ "type": "boolean"+ },+ "timezone": {+ "type": ["string", "null"]+ },+ "duration": {+ "type": "string",+ "pattern": "^\\d{2}:\\d{2}:\\d{2}$"+ },+ "can_act_on_discourse_post_event": {+ "type": ["boolean", "null"]+ },+ "can_update_attendance": {+ "type": ["boolean", "null"]+ },+ "creator": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "username": {+ "type": "string"+ },+ "name": {+ "type": ["string", "null"]+ },+ "avatar_template": {+ "type": "string"+ }+ },+ "required": ["id", "username", "avatar_template"]+ },+ "custom_fields": {+ "type": ["object", "null"]+ },+ "is_closed": {+ "type": "boolean"+ },+ "is_expired": {+ "type": "boolean"+ },+ "is_ongoing": {+ "type": "boolean"+ },+ "is_private": {+ "type": "boolean"+ },+ "is_public": {+ "type": "boolean"+ },+ "is_standalone": {+ "type": "boolean"+ },+ "minimal": {+ "type": ["boolean", "null"]+ },+ "raw_invitees": {+ "type": ["array", "null"],+ "items": {+ "type": "string"+ }+ },+ "reminders": {+ "type": "array",+ "items": {+ "type": "object",+ "properties": {+ "value": {+ "type": "integer"+ },+ "unit": {+ "type": "string"+ },+ "period": {+ "type": "string",+ "enum": ["before", "after"]+ },+ "type": {+ "type": "string"+ }+ },+ "required": ["value", "unit", "period", "type"]+ }+ },+ "sample_invitees": {+ "type": "array",+ "items": {+ "type": "object"+ }+ },+ "should_display_invitees": {+ "type": "boolean"+ },+ "stats": {+ "type": "object"+ },+ "status": {+ "type": "string",+ "enum": ["public", "private", "standalone"]+ },+ "url": {+ "type": "string"+ },+ "description": {+ "type": ["string", "null"]+ },+ "location": {+ "type": ["string", "null"]+ },+ "watching_invitee": {+ "type": ["object", "null"]+ },+ "chat_enabled": {+ "type": ["boolean", "null"]+ },+ "channel": {+ "type": "object"+ },+ "max_attendees": {+ "type": ["integer", "null"]+ },+ "at_capacity": {+ "type": "boolean"+ },+ "post": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "post_number": {+ "type": "integer"+ },+ "url": {+ "type": "string"+ },+ "category_slug": {+ "type": "string"+ },+ "topic": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "title": {+ "type": "string"+ },+ "tags": {+ "type": "array",+ "items": {+ "type": "string"+ }+ },+ "tags_descriptions": {+ "type": "object"+ }+ },+ "required": ["id", "title", "tags", "tags_descriptions"]+ }+ },+ "required": ["id", "post_number", "url", "category_slug", "topic"]+ }+ },+ "required": [+ "id",+ "category_id",+ "starts_at",+ "ends_at",+ "show_local_time",+ "timezone",+ "can_act_on_discourse_post_event",+ "can_update_attendance",+ "is_closed",+ "is_expired",+ "is_ongoing",+ "is_private",+ "is_public",+ "is_standalone",+ "should_display_invitees",+ "status",+ "at_capacity",+ "post"+ ]+ }+ }+ },+ "required": ["events"]+}diff --git a/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_response.json b/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_response.jsonnew file mode 100644index 00000000000..72ffadb349c--- /dev/null+++ b/plugins/discourse-calendar/spec/requests/api/schemas/json/events_index_response.json@@ -0,0 +1,103 @@+{+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "events": {+ "type": "array",+ "items": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "category_id": {+ "type": ["integer", "null"]+ },+ "name": {+ "type": ["string", "null"]+ },+ "recurrence": {+ "type": ["string", "null"]+ },+ "recurrence_until": {+ "type": ["string", "null"],+ "format": "date-time"+ },+ "starts_at": {+ "type": ["string", "null"],+ "format": "date-time"+ },+ "ends_at": {+ "type": ["string", "null"],+ "format": "date-time"+ },+ "rrule": {+ "type": "string"+ },+ "show_local_time": {+ "type": "boolean"+ },+ "timezone": {+ "type": ["string", "null"]+ },+ "duration": {+ "type": "string",+ "pattern": "^\\d{2}:\\d{2}:\\d{2}$"+ },+ "post": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "post_number": {+ "type": "integer"+ },+ "url": {+ "type": "string"+ },+ "category_slug": {+ "type": "string"+ },+ "topic": {+ "type": "object",+ "additionalProperties": false,+ "properties": {+ "id": {+ "type": "integer"+ },+ "title": {+ "type": "string"+ },+ "tags": {+ "type": "array",+ "items": {+ "type": "string"+ }+ },+ "tags_descriptions": {+ "type": "object"+ }+ },+ "required": ["id", "title", "tags", "tags_descriptions"]+ }+ },+ "required": ["id", "post_number", "url", "category_slug", "topic"]+ }+ },+ "required": [+ "id",+ "category_id",+ "starts_at",+ "ends_at",+ "show_local_time",+ "timezone",+ "post"+ ]+ }+ }+ },+ "required": ["events"]+}diff --git a/spec/requests/api/schemas/schema_loader.rb b/spec/requests/api/schemas/schema_loader.rbindex 51da348bcea..c2d5f970dea 100644--- a/spec/requests/api/schemas/schema_loader.rb+++ b/spec/requests/api/schemas/schema_loader.rb@@ -9,7 +9,23 @@ module SpecSchemasenddef load- JSON.parse(File.read(File.join(__dir__, "json", "#{@filename}.json")))+ path = find_schema_file+ raise "Schema file not found: #{@filename}.json" unless path++ JSON.parse(File.read(path))+ end++ private++ def find_schema_file+ # Search plugin directories first+ plugin_paths =+ Dir.glob(Rails.root.join("plugins/*/spec/requests/api/schemas/json/#{@filename}.json"))+ return plugin_paths.first if plugin_paths.any?++ # Fall back to core+ core_path = File.join(__dir__, "json", "#{@filename}.json")+ File.exist?(core_path) ? core_path : nilendendenddiff --git a/spec/swagger_helper.rb b/spec/swagger_helper.rbindex 761b42bc8ac..c1df3b5b3ae 100644--- a/spec/swagger_helper.rb+++ b/spec/swagger_helper.rb@@ -4,9 +4,11 @@ require "json_schemer"# Require schema filesDir["./spec/requests/api/schemas/*.rb"].each { |file| require file }+Dir["./plugins/*/spec/requests/api/schemas/*.rb"].each { |file| require file }# Require shared spec examplesDir["./spec/requests/api/shared/*.rb"].each { |file| require file }+Dir["./plugins/*/spec/requests/api/shared/*.rb"].each { |file| require file }def load_spec_schema(name)SpecSchemas::SpecLoader.new(name).load