|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +require 'erb' |
| 6 | +require 'fileutils' |
| 7 | +require 'ostruct' |
| 8 | +require 'tmpdir' |
| 9 | + |
| 10 | +module Rails |
| 11 | + module Generators |
| 12 | + class NamedBase |
| 13 | + class << self |
| 14 | + attr_accessor :_source_root, :_destination_root |
| 15 | + |
| 16 | + def desc(*) = nil |
| 17 | + |
| 18 | + def source_root(path = nil) |
| 19 | + self._source_root = path if path |
| 20 | + _source_root |
| 21 | + end |
| 22 | + |
| 23 | + def destination_root(path = nil) |
| 24 | + self._destination_root = path if path |
| 25 | + _destination_root |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def initialize(name) |
| 30 | + @name = name |
| 31 | + end |
| 32 | + |
| 33 | + def file_name |
| 34 | + @name.to_s.underscore |
| 35 | + end |
| 36 | + |
| 37 | + def class_path |
| 38 | + [] |
| 39 | + end |
| 40 | + |
| 41 | + def template(src, dest) |
| 42 | + src_path = File.join(self.class.source_root, src) |
| 43 | + out_path = File.join(self.class.destination_root, dest) |
| 44 | + FileUtils.mkdir_p(File.dirname(out_path)) |
| 45 | + File.write(out_path, ERB.new(File.read(src_path), trim_mode: '-').result(binding)) |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +require 'jsonapi/swagger/json' |
| 52 | +require 'jsonapi/swagger/resource' |
| 53 | +require 'jsonapi/swagger/resources/jsonapi_resource' |
| 54 | +require 'generators/jsonapi/swagger/swagger_generator' |
| 55 | + |
| 56 | +RSpec.describe Jsonapi::SwaggerGenerator do |
| 57 | + around do |example| |
| 58 | + Dir.mktmpdir('jsonapi-swagger-spec') do |dir| |
| 59 | + described_class.destination_root(dir) |
| 60 | + example.run |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + before do |
| 65 | + stub_const('JSONAPI', Module.new) |
| 66 | + stub_const('JSONAPI::Resource', Class.new) |
| 67 | + |
| 68 | + stub_const('User', Class.new) |
| 69 | + User.define_singleton_method(:columns) do |
| 70 | + [ |
| 71 | + OpenStruct.new(name: 'id', type: :integer, null: false, comment: 'ID'), |
| 72 | + OpenStruct.new(name: 'name', type: :string, null: true, comment: 'Name') |
| 73 | + ] |
| 74 | + end |
| 75 | + |
| 76 | + relation = OpenStruct.new(class_name: 'Company', table_name: 'companies') |
| 77 | + relation.define_singleton_method(:belongs_to?) { true } |
| 78 | + |
| 79 | + resource = Class.new(JSONAPI::Resource) do |
| 80 | + def self._attributes = { id: nil, name: nil } |
| 81 | + def self._relationships = { company: OpenStruct.new(class_name: 'Company', table_name: 'companies') } |
| 82 | + def self.sortable_fields = [:name] |
| 83 | + def self.creatable_fields = [:name] |
| 84 | + def self.updatable_fields = [:name] |
| 85 | + def self.filters = { name: {} } |
| 86 | + def self.mutable? = true |
| 87 | + end |
| 88 | + |
| 89 | + # Ensure relationship object has belongs_to? and table_name. |
| 90 | + resource._relationships[:company].define_singleton_method(:belongs_to?) { true } |
| 91 | + |
| 92 | + stub_const('UserResource', resource) |
| 93 | + |
| 94 | + Jsonapi::Swagger.config do |c| |
| 95 | + c.use_rswag = false |
| 96 | + c.base_path = '/api' |
| 97 | + c.file_path = 'v1/swagger.json' |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + it 'generates swagger JSON via template' do |
| 102 | + gen = described_class.new('User') |
| 103 | + gen.create_swagger_file |
| 104 | + |
| 105 | + out = File.join(described_class.destination_root, 'swagger', 'v1', 'swagger.json') |
| 106 | + expect(File.exist?(out)).to eq(true) |
| 107 | + |
| 108 | + json = JSON.parse(File.read(out)) |
| 109 | + expect(json['swagger']).to eq('2.0') |
| 110 | + expect(json['basePath']).to eq('/api') |
| 111 | + expect(json['paths']).to include('/users') |
| 112 | + expect(json['paths']).to include('/users/{id}') |
| 113 | + expect(json['paths']['/users']).to include('post') |
| 114 | + end |
| 115 | +end |
0 commit comments