oas_rails 0.10.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,10 +7,12 @@ module OasRails
7
7
  # @param klass [Class] The class for which the schema is built.
8
8
  # @return [Hash] The schema as a JSON-compatible hash.
9
9
  def build_incoming_schema(klass:, model_to_schema_class: EasyTalk)
10
- configure_common_settings(model_to_schema_class:)
11
- model_to_schema_class.configuration.excluded_columns = OasRails.config.excluded_columns_incoming
12
-
13
- model_to_schema_class::ActiveRecordSchemaBuilder.new(klass).build_schema_definition.as_json["schema"]
10
+ build_schema(
11
+ klass: klass,
12
+ model_to_schema_class: model_to_schema_class,
13
+ excluded_columns: OasRails.config.excluded_columns_incoming,
14
+ exclude_primary_key: true
15
+ )
14
16
  end
15
17
 
16
18
  # Builds a schema for a class when it is used as outgoing API data.
@@ -18,21 +20,37 @@ module OasRails
18
20
  # @param klass [Class] The class for which the schema is built.
19
21
  # @return [Hash] The schema as a JSON-compatible hash.
20
22
  def build_outgoing_schema(klass:, model_to_schema_class: EasyTalk)
21
- configure_common_settings(model_to_schema_class:)
22
- model_to_schema_class.configuration.excluded_columns = OasRails.config.excluded_columns_outgoing
23
- model_to_schema_class.configuration.exclude_primary_key = false
24
-
25
- model_to_schema_class::ActiveRecordSchemaBuilder.new(klass).build_schema_definition.as_json["schema"]
23
+ build_schema(
24
+ klass: klass,
25
+ model_to_schema_class: model_to_schema_class,
26
+ excluded_columns: OasRails.config.excluded_columns_outgoing,
27
+ exclude_primary_key: false
28
+ )
26
29
  end
27
30
 
28
31
  private
29
32
 
33
+ # Builds a schema with the given configuration.
34
+ #
35
+ # @param klass [Class] The class for which the schema is built.
36
+ # @param model_to_schema_class [Class] The schema builder class.
37
+ # @param excluded_columns [Array] Columns to exclude from the schema.
38
+ # @param exclude_primary_key [Boolean] Whether to exclude the primary key.
39
+ # @return [Hash] The schema as a JSON-compatible hash.
40
+ def build_schema(klass:, model_to_schema_class:, excluded_columns:, exclude_primary_key:)
41
+ configure_common_settings(model_to_schema_class: model_to_schema_class)
42
+ model_to_schema_class.configuration.excluded_columns = excluded_columns
43
+ model_to_schema_class.configuration.exclude_primary_key = exclude_primary_key
44
+
45
+ definition = model_to_schema_class::ActiveRecordSchemaBuilder.new(klass).build_schema_definition
46
+ EasyTalk::Builders::ObjectBuilder.new(definition).build.as_json
47
+ end
48
+
30
49
  # Configures common settings for schema building.
31
50
  #
32
51
  # Excludes associations and foreign keys from the schema.
33
52
  def configure_common_settings(model_to_schema_class: EasyTalk)
34
53
  model_to_schema_class.configuration.exclude_associations = true
35
- model_to_schema_class.configuration.exclude_foreign_keys = true
36
54
  end
37
55
  end
38
56
  end
@@ -13,8 +13,10 @@ module OasRails
13
13
  :possible_default_responses,
14
14
  :response_body_of_default,
15
15
  :http_verbs,
16
- :use_model_names
17
- attr_reader :servers, :tags, :security_schema
16
+ :use_model_names,
17
+ :rapidoc_theme
18
+
19
+ attr_reader :servers, :tags, :security_schema, :include_mode
18
20
 
19
21
  def initialize
20
22
  @info = Spec::Info.new
@@ -35,6 +37,8 @@ module OasRails
35
37
  @http_verbs = [:get, :post, :put, :patch, :delete]
36
38
  @response_body_of_default = "Hash{ success: !Boolean, message: String }"
37
39
  @use_model_names = false
40
+ @rapidoc_theme = :rails
41
+ @include_mode = :all
38
42
  end
39
43
 
40
44
  def security_schema=(value)
@@ -62,6 +66,13 @@ module OasRails
62
66
  def excluded_columns_outgoing
63
67
  []
64
68
  end
69
+
70
+ def include_mode=(value)
71
+ valid_modes = [:all, :with_tags, :explicit]
72
+ raise ArgumentError, "include_mode must be one of #{valid_modes}" unless valid_modes.include?(value)
73
+
74
+ @include_mode = value
75
+ end
65
76
  end
66
77
 
67
78
  DEFAULT_SECURITY_SCHEMES = {
@@ -6,5 +6,11 @@ module OasRails
6
6
  inflect.acronym 'YARD'
7
7
  end
8
8
  end
9
+
10
+ config.app_middleware.use(
11
+ Rack::Static,
12
+ urls: ["/oas-rails-assets"],
13
+ root: OasRails::Engine.root.join("public")
14
+ )
9
15
  end
10
16
  end
@@ -45,41 +45,14 @@ module OasRails
45
45
  route.gsub('(.:format)', '').gsub(/:\w+/) { |match| "{#{match[1..]}}" }
46
46
  end
47
47
 
48
- # THIS CODE IS NOT IN USE BUT CAN BE USEFULL WITH GLOBAL TAGS OR AUTH TAGS
49
- # def get_controller_comments(controller_path)
50
- # YARD.parse_string(File.read(controller_path))
51
- # controller_class = YARD::Registry.all(:class).first
52
- # if controller_class
53
- # class_comment = controller_class.docstring.all
54
- # method_comments = controller_class.meths.map do |method|
55
- # {
56
- # name: method.name,
57
- # comment: method.docstring.all
58
- # }
59
- # end
60
- # YARD::Registry.clear
61
- # {
62
- # class_comment: class_comment,
63
- # method_comments: method_comments
64
- # }
65
- # else
66
- # YARD::Registry.clear
67
- # nil
68
- # end
69
- # rescue StandardError
70
- # nil
71
- # end
72
- #
73
- # def get_controller_comment(controller_path)
74
- # get_controller_comments(controller_path)&.dig(:class_comment) || ''
75
- # rescue StandardError
76
- # ''
77
- # end
78
-
79
48
  private
80
49
 
81
50
  def extract_host_routes
82
- valid_routes.map { |r| OasRoute.new_from_rails_route(rails_route: r) }
51
+ routes = valid_routes.map { |r| OasRoute.new_from_rails_route(rails_route: r) }
52
+
53
+ routes.select! { |route| route.docstring.tags.any? } if OasRails.config.include_mode == :with_tags
54
+ routes.select! { |route| route.docstring.tags.any? { |t| t.tag_name == "oas_include" } } if OasRails.config.include_mode == :explicit
55
+ routes
83
56
  end
84
57
 
85
58
  def valid_routes
@@ -1,3 +1,3 @@
1
1
  module OasRails
2
- VERSION = "0.10.1"
2
+ VERSION = "0.12.0"
3
3
  end
data/lib/oas_rails.rb CHANGED
@@ -90,7 +90,8 @@ module OasRails
90
90
  'Endpoint Tags' => [:tags],
91
91
  'Summary' => [:summary],
92
92
  'No Auth' => [:no_auth],
93
- 'Auth methods' => [:auth, :with_types]
93
+ 'Auth methods' => [:auth, :with_types],
94
+ 'OAS Include' => [:oas_include]
94
95
  }
95
96
  yard_tags.each do |tag_name, (method_name, handler)|
96
97
  ::YARD::Tags::Library.define_tag(tag_name, method_name, handler)