add tags to swagger

This commit is contained in:
Tanmay Deep Sharma
2025-05-20 16:09:44 +07:00
parent 285b79d488
commit 019a596a3e
6 changed files with 479 additions and 23375 deletions
+78 -37
View File
@@ -30,48 +30,89 @@ namespace :swagger do
base_path = Rails.root.join('swagger')
tag_groups_path = base_path.join('tag_groups')
Dir.chdir(tag_groups_path) do
# Build for platform
platform_yaml = YAML.safe_load(File.open('platform.yml'))
platform_build = JsonRefs.call(
platform_yaml,
resolve_local_ref: false,
resolve_file_ref: true,
logging: true
)
File.write('platform_swagger.json', JSON.pretty_generate(platform_build))
# Load the full swagger specification
full_spec = JSON.parse(File.read(base_path.join('swagger.json')))
# Load the tag groups from index.yml
swagger_index = YAML.safe_load(File.open(base_path.join('index.yml')))
tag_groups = swagger_index['x-tagGroups']
# Process each tag group
tag_groups.each do |tag_group|
group_name = tag_group['name']
tags = tag_group['tags']
# Build for application
application_yaml = YAML.safe_load(File.open('application.yml'))
application_build = JsonRefs.call(
application_yaml,
resolve_local_ref: false,
resolve_file_ref: true,
logging: true
)
File.write('application_swagger.json', JSON.pretty_generate(application_build))
# Create a copy of the full spec for this tag group
tag_spec = full_spec.dup
# Build for client
client_yaml = YAML.safe_load(File.open('client.yml'))
client_build = JsonRefs.call(
client_yaml,
resolve_local_ref: false,
resolve_file_ref: true,
logging: true
)
File.write('client_swagger.json', JSON.pretty_generate(client_build))
# Filter paths to only include those with operations tagged with any of our tags
filtered_paths = {}
# Build for others
others_yaml = YAML.safe_load(File.open('others.yml'))
others_build = JsonRefs.call(
others_yaml,
resolve_local_ref: false,
resolve_file_ref: true,
logging: true
)
File.write('other_swagger.json', JSON.pretty_generate(others_build))
full_spec['paths'].each do |path, path_item|
# Check if any operation in this path has a tag from our group
operations_with_our_tags = false
path_item.each do |method, operation|
next unless operation.is_a?(Hash) && operation['tags']
# Check if any of our tags are in the operation's tags
if (operation['tags'] & tags).any?
operations_with_our_tags = true
break
end
end
# If we found operations with our tags, add the path to our filtered paths
if operations_with_our_tags
filtered_paths[path] = path_item
end
end
# Replace the paths in our tag-specific spec
tag_spec['paths'] = filtered_paths
# Filter tags to only include those in our group
tag_spec['tags'] = full_spec['tags'].select { |tag| tags.include?(tag['name']) }
# Write the tag-specific spec to file
output_file = "#{group_name.downcase}_swagger.json"
output_file = "other_swagger.json" if group_name.downcase == 'others'
File.write(tag_groups_path.join(output_file), JSON.pretty_generate(tag_spec))
end
puts 'Tag-specific swagger files generated successfully.'
end
desc 'build swagger files and create symlinks in developer-docs'
task build_for_docs: :environment do
# First, build the swagger files
Rake::Task['swagger:build'].invoke
# Create directories in developer-docs if they don't exist
dev_docs_public = Rails.root.join('developer-docs', 'public')
FileUtils.mkdir_p(File.join(dev_docs_public, 'swagger', 'tag_groups'))
# Create symlinks to the swagger files
tag_groups_path = Rails.root.join('swagger', 'tag_groups')
# Symlink each JSON file
puts 'Creating symlinks for developer-docs...'
symlink_files = %w[platform_swagger.json application_swagger.json client_swagger.json other_swagger.json]
symlink_files.each do |file|
source = File.join(tag_groups_path, file)
target = File.join(dev_docs_public, 'swagger', 'tag_groups', file)
# Remove existing file or symlink
FileUtils.rm_f(target)
# Create a relative symbolic link
rel_path = Pathname.new(source).relative_path_from(Pathname.new(File.dirname(target)))
FileUtils.ln_sf(rel_path, target)
end
puts 'Symlinks created successfully.'
puts 'You can now run the Mintlify dev server to preview the documentation.'
end
end