openapi-sourcetools 0.12.0 → 0.12.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 830a477c8647ad883e5a6619767f19cfae7c2d9b9baa100b0e10bdd911ef0fc1
4
- data.tar.gz: 9e4a89d44c344c0099f826bf08c459b2eaa448e4d8b76b982e7ee8b7146199b2
3
+ metadata.gz: 44a305843fc718a7b958b3afbaf42f6f215f5a23e23c5485799222b7030a1757
4
+ data.tar.gz: 68d37c654b903c17d8aa64d96fdcecbca2c3e7c56b2b5629e2e80f243d3993e1
5
5
  SHA512:
6
- metadata.gz: eef01e1c3095d747213533d8ceeb4d0ba4107970b6541632b92b9ffaa0a0202d583ec6934f2af4af3dbf0dff4d0344e053bbf9201d8128cd474406fe11a8b308
7
- data.tar.gz: 92fcbc4c9128e213ee4d67d7eebe69c5de9f5ef2576a0efdeae33b2f512a786d586e0eebe903fc932b43f59475ecbd780d925263adbd9f5039b113292db8ae97
6
+ metadata.gz: 274b22fbac33a899c3a2102772d2b3e6bd44aab656b67e74a1e65fbd07edcab86bc7fcde94e41184a51e3435ec7cbc5d826c407bbbbed322ff0e2eeec1168156
7
+ data.tar.gz: a41c84712831166ef6ab0b2233a9c491ea1d87b73d6f1d67bab2ef821a937d2216e99a471ed9210427cb7b8c9824160c257cc6ef856a971d4804812a9b9a5a5a
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2026 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require_relative '../lib/openapi/sourcetools/apiobjects'
8
+ require_relative '../lib/openapi/sourcetools/common'
9
+ require 'optparse'
10
+ require 'yaml'
11
+
12
+
13
+ def main
14
+ input_name = nil
15
+ output_name = nil
16
+ path = %w[components mediaTypes]
17
+ components = OpenAPISourceTools::ApiObjects::Components.new(path, 'MediaType')
18
+ replacer = OpenAPISourceTools::ApiObjects::ValueSubValueReplacer.new('content', components)
19
+
20
+ parser = OptionParser.new do |opts|
21
+ opts.summary_indent = ' '
22
+ opts.summary_width = 26
23
+ opts.banner = 'Usage: openapi-addmediatypes [options]'
24
+ opts.separator ''
25
+ opts.separator 'Options:'
26
+ opts.on('-i', '--input FILE', 'Read API spec from FILE, not stdin.') do |f|
27
+ input_name = f
28
+ end
29
+ opts.on('-o', '--output FILE', 'Output to FILE, not stdout.') do |f|
30
+ output_name = f
31
+ end
32
+ components.add_options(opts)
33
+ opts.on('-h', '--help', 'Print this help and exit.') do
34
+ $stdout.puts %(#{opts}
35
+
36
+ Loads API document in OpenAPI format and moves media types under components
37
+ and replaces the original with reference.
38
+
39
+ #{components.help}
40
+ )
41
+ exit 0
42
+ end
43
+ end
44
+ parser.order!
45
+
46
+ doc = OpenAPISourceTools::Common.load_source(input_name)
47
+ return 2 if doc.nil?
48
+
49
+ components.items = doc.dig(*path) || {}
50
+ replacer.replace(doc.dig('components', 'headers') || {})
51
+ replacer.replace(doc.dig('components', 'responses') || {})
52
+ replacer.replace(doc.dig('components', 'parameters') || {})
53
+ replacer.replace(doc.fetch('paths', {}))
54
+ OpenAPISourceTools::Common.bury(doc, path, components.items) unless components.items.empty?
55
+
56
+ OpenAPISourceTools::Common.dump_result(output_name, doc, 3)
57
+ end
58
+
59
+ exit(main) if File.basename(__FILE__) == File.basename($PROGRAM_NAME)
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2026 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require_relative '../lib/openapi/sourcetools/apiobjects'
8
+ require_relative '../lib/openapi/sourcetools/common'
9
+ require 'optparse'
10
+ require 'yaml'
11
+
12
+
13
+ def main
14
+ input_name = nil
15
+ output_name = nil
16
+ path = %w[components requestBodies]
17
+ components = OpenAPISourceTools::ApiObjects::Components.new(path, 'RequestBody')
18
+ replacer = OpenAPISourceTools::ApiObjects::ValueReplacer.new('requestBody', components)
19
+
20
+ parser = OptionParser.new do |opts|
21
+ opts.summary_indent = ' '
22
+ opts.summary_width = 26
23
+ opts.banner = 'Usage: openapi-addrequestbodies [options]'
24
+ opts.separator ''
25
+ opts.separator 'Options:'
26
+ opts.on('-i', '--input FILE', 'Read API spec from FILE, not stdin.') do |f|
27
+ input_name = f
28
+ end
29
+ opts.on('-o', '--output FILE', 'Output to FILE, not stdout.') do |f|
30
+ output_name = f
31
+ end
32
+ components.add_options(opts)
33
+ opts.on('-h', '--help', 'Print this help and exit.') do
34
+ $stdout.puts %(#{opts}
35
+
36
+ Loads API document in OpenAPI format and moves request body under components
37
+ and replaces the original with reference.
38
+
39
+ #{components.help}
40
+ )
41
+ exit 0
42
+ end
43
+ end
44
+ parser.order!
45
+
46
+ doc = OpenAPISourceTools::Common.load_source(input_name)
47
+ return 2 if doc.nil?
48
+
49
+ components.items = doc.dig(*path) || {}
50
+ replacer.replace(doc.dig('components', 'pathItems') || {})
51
+ replacer.replace(doc.fetch('paths', {}))
52
+ OpenAPISourceTools::Common.bury(doc, path, components.items) unless components.items.empty?
53
+
54
+ OpenAPISourceTools::Common.dump_result(output_name, doc, 3)
55
+ end
56
+
57
+ exit(main) if File.basename(__FILE__) == File.basename($PROGRAM_NAME)
@@ -56,7 +56,7 @@ def replace_inlines(obj, components, top_level_name = nil)
56
56
  end
57
57
  unless looks_like_type
58
58
  # Some kind of intermediate-level object.
59
- (obj.keys.sort! { |a, b| a.to_s <=> b.to_s }).each do |key|
59
+ obj.keys.sort! { |a, b| a.to_s <=> b.to_s }.each do |key|
60
60
  next if key == 'securitySchemes'
61
61
  return false unless replace_inlines(obj[key], components)
62
62
  end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Copyright © 2024-2026 Ismo Kärkkäinen
5
+ # Licensed under Universal Permissive License. See LICENSE.txt.
6
+
7
+ require_relative '../lib/openapi/sourcetools/common'
8
+ require_relative '../lib/openapi/sourcetools/config'
9
+ require_relative '../lib/openapi/sourcetools/gen'
10
+ require 'optparse'
11
+ include OpenAPISourceTools
12
+
13
+
14
+ def main
15
+ prefix = nil
16
+ keys = nil
17
+ read = false
18
+
19
+ parser = OptionParser.new do |opts|
20
+ opts.summary_indent = ' '
21
+ opts.summary_width = 26
22
+ opts.banner = 'Usage: openapi-generate-checkconfig [options]'
23
+ opts.separator ''
24
+ opts.separator 'Options:'
25
+ opts.on('-c', '--config NAME', 'Use config prefix NAME. (config:NAME)') do |c|
26
+ prefix = c
27
+ end
28
+ opts.on('-s', '--separator STRING', 'Use separator STRING. (separator:STRING)') do |s|
29
+ Gen.separator = s unless s.empty?
30
+ end
31
+ opts.on('-r', '--[no-]read', "Show if file is read by read_contents, default #{Common.yesno(read)}.") do |b|
32
+ read = b
33
+ end
34
+ opts.on('-k', '--keys KEYS', 'Show all values of KEYS.') do |ks|
35
+ keys = ks.split('/').reject(&:empty?) unless ks.empty?
36
+ keys = nil if !keys.nil? && keys.empty?
37
+ end
38
+ opts.on('-h', '--help', 'Print this help and exit.') do
39
+ $stdout.puts %(#{opts}
40
+ Displays configurations files that are found using specific prefix and an
41
+ optional separator character or string.
42
+
43
+ If KEYS, separated by "/", is given, the value of that key path is displayed.
44
+ That and read check require the configuration files to be YAML/JSON.
45
+ )
46
+ exit 0
47
+ end
48
+ end
49
+ parser.order!
50
+
51
+ return Common.aargh('Config must be given.', 1) if prefix.nil?
52
+ cfgs = ConfigLoader.find_files(name_prefix: prefix)
53
+ ConfigLoader.read_contents(cfgs) if read || !keys.nil?
54
+ root_dir, _p = ConfigLoader.prepare_prefix(prefix, Gen.wd)
55
+ drop = root_dir.size + 1
56
+ cfgs.each do |cfg|
57
+ ks = cfg.keys.join('/')
58
+ info = "#{cfg.root}: #{ks.empty? ? '(root)' : ks}, #{cfg.path[drop..]}"
59
+ ki = nil
60
+ unless keys.nil? || cfg.content.nil?
61
+ v = cfg.content.dig(*keys)
62
+ ki = v.to_s unless v.nil?
63
+ end
64
+ ri = read ? !cfg.content.nil? : nil
65
+ next if (read || !keys.nil?) && ki.nil? && ri.nil?
66
+ puts info
67
+ puts " #{ki}" unless ki.nil?
68
+ puts " File is #{ri ? '' : 'not '}read." unless ri.nil?
69
+ end
70
+ 0
71
+ end
72
+
73
+ exit(main) if File.basename(__FILE__) == File.basename($PROGRAM_NAME)
data/bin/openapi-order CHANGED
@@ -213,6 +213,7 @@ def default_order_data(order_key, regexp_prefix)
213
213
  - parameters
214
214
  - '*'
215
215
  - additionalOperations
216
+ - re:^x-openapi-sourcetools.*$
216
217
  '*':
217
218
  #{order_key}: &OperationObject
218
219
  - '$ref'
@@ -240,17 +240,11 @@ module OpenAPISourceTools
240
240
  pk = @parts[k]
241
241
  ppk = pp[k]
242
242
  if pk.key?('fixed')
243
- if ppk.key?('fixed')
244
- c = pk['fixed'] <=> ppk['fixed']
245
- else
246
- return -1
247
- end
243
+ return -1 unless ppk.key?('fixed')
244
+ c = pk['fixed'] <=> ppk['fixed']
248
245
  else
249
- if ppk.key?('fixed')
250
- return 1
251
- else
252
- c = pk.fetch('parameter', '') <=> ppk.fetch('parameter', '')
253
- end
246
+ return 1 if ppk.key?('fixed')
247
+ c = pk.fetch('parameter', '') <=> ppk.fetch('parameter', '')
254
248
  end
255
249
  return c unless c.zero?
256
250
  end
@@ -10,7 +10,7 @@ module OpenAPISourceTools
10
10
  # Common methods used in programs and elsewhere gathered into one place.
11
11
  module Common
12
12
  def self.aargh(message, return_value = nil)
13
- message = message.map(&:to_s).join("\n") if message.is_a? Array
13
+ message = message.join("\n") if message.is_a? Array
14
14
  $stderr.puts message
15
15
  return_value
16
16
  end
@@ -8,11 +8,11 @@ require_relative 'gen'
8
8
  require 'find'
9
9
  require 'yaml'
10
10
 
11
+ # Gem namespace.
11
12
  module OpenAPISourceTools
12
13
  # Configuration file find and load convenience functions.
13
14
  # See the first 3 methods. The rest are intended to be internal helpers.
14
15
  module ConfigLoader
15
-
16
16
  # A function to find all files with a given prefix.
17
17
  # Prefix is taken from Gen.config if nil.
18
18
  # Returns an array of ConfigFileInfo objects.
@@ -43,9 +43,10 @@ module OpenAPISourceTools
43
43
 
44
44
  # Maps an array of ConfigFileInfo objects to an array of their contents.
45
45
  def self.contents_array(config_file_infos)
46
- config_file_infos.map(&:content).reject(&:nil?)
46
+ config_file_infos.map(&:content).compact
47
47
  end
48
48
 
49
+ # The result with data about the config file.
49
50
  class ConfigFileInfo
50
51
  include Comparable
51
52
 
@@ -10,6 +10,7 @@ require_relative 'loaders'
10
10
  require_relative 'gen'
11
11
 
12
12
 
13
+ # Gem namespace.
13
14
  module OpenAPISourceTools
14
15
  def self.executable_bits_on(mode)
15
16
  mode = mode.to_s(8).chars
@@ -82,7 +82,7 @@ module OpenAPISourceTools
82
82
  d = name.downcase
83
83
  if d.start_with?(YAML_PREFIX)
84
84
  name = name.slice(YAML_PREFIX.size...name.size)
85
- elsif (YAML_EXTS.index { |s| d.end_with?(s) }).nil?
85
+ elsif YAML_EXTS.index { |s| d.end_with?(s) }.nil?
86
86
  return false
87
87
  end
88
88
  n, _sep, f = name.partition(':')
@@ -140,9 +140,9 @@ module OpenAPISourceTools
140
140
  method(:req_loader),
141
141
  method(:eval_loader),
142
142
  method(:ruby_loader),
143
+ method(:config_loader),
143
144
  method(:yaml_loader),
144
145
  method(:bin_loader),
145
- method(:config_loader),
146
146
  method(:separator_loader)
147
147
  ]
148
148
  end
@@ -153,7 +153,7 @@ module OpenAPISourceTools
153
153
  - #{Loaders::EVAL_PREFIX}code : runs code to add gem tasks again.
154
154
  - ruby_file#{Loaders::RUBY_EXT} : changes to Ruby file directory and requires the file.
155
155
  - #{Loaders::YAML_PREFIX}name:filename : Loads YAML file into Gen.d.name.
156
- - name:filename.{#{(Loaders::YAML_EXTS.map { |s| s[1...s.size] }).join('|')}} : Loads YAML file into Gen.d.name.
156
+ - name:filename.{#{Loaders::YAML_EXTS.map { |s| s[1...s.size] }.join('|')}} : Loads YAML file into Gen.d.name.
157
157
  - #{Loaders::BIN_PREFIX}name:filename : Loads binary file into Gen.d.name.
158
158
  - #{Loaders::CONFIG_PREFIX}name : Sets Gen.config for next gem/Ruby file configuration loading.
159
159
  - #{Loaders::SEPARATOR_PREFIX}string : Sets Gen.separator to string.
@@ -7,8 +7,8 @@ require_relative 'common'
7
7
  require 'erb'
8
8
 
9
9
 
10
+ # Gem namespace.
10
11
  module OpenAPISourceTools
11
-
12
12
  # Required interface for tasks, with default implementation for some methods.
13
13
  module TaskInterface
14
14
  def generate(context_binding)
@@ -3,9 +3,10 @@
3
3
  # Copyright © 2025-2026 Ismo Kärkkäinen
4
4
  # Licensed under Universal Permissive License. See LICENSE.txt.
5
5
 
6
+ # Gem namespace.
6
7
  module OpenAPISourceTools
7
8
  NAME = 'openapi-sourcetools'
8
- VERSION = '0.12.0'
9
+ VERSION = '0.12.1'
9
10
 
10
11
  def self.info(separator = ': ')
11
12
  "#{NAME}#{separator}#{VERSION}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openapi-sourcetools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismo Kärkkäinen
@@ -37,7 +37,9 @@ description: |-
37
37
  email: ismokarkkainen@icloud.com
38
38
  executables:
39
39
  - openapi-addheaders
40
+ - openapi-addmediatypes
40
41
  - openapi-addparameters
42
+ - openapi-addrequestbodies
41
43
  - openapi-addresponses
42
44
  - openapi-addschemas
43
45
  - openapi-addsecurityschemes
@@ -45,6 +47,7 @@ executables:
45
47
  - openapi-clearrefs
46
48
  - openapi-frequencies
47
49
  - openapi-generate
50
+ - openapi-generate-checkconfig
48
51
  - openapi-merge
49
52
  - openapi-modifypaths
50
53
  - openapi-order
@@ -55,7 +58,9 @@ extra_rdoc_files: []
55
58
  files:
56
59
  - LICENSE.txt
57
60
  - bin/openapi-addheaders
61
+ - bin/openapi-addmediatypes
58
62
  - bin/openapi-addparameters
63
+ - bin/openapi-addrequestbodies
59
64
  - bin/openapi-addresponses
60
65
  - bin/openapi-addschemas
61
66
  - bin/openapi-addsecurityschemes
@@ -63,6 +68,7 @@ files:
63
68
  - bin/openapi-clearrefs
64
69
  - bin/openapi-frequencies
65
70
  - bin/openapi-generate
71
+ - bin/openapi-generate-checkconfig
66
72
  - bin/openapi-merge
67
73
  - bin/openapi-modifypaths
68
74
  - bin/openapi-order
@@ -100,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
106
  - !ruby/object:Gem::Version
101
107
  version: '0'
102
108
  requirements: []
103
- rubygems_version: 3.6.9
109
+ rubygems_version: 4.0.21
104
110
  specification_version: 4
105
111
  summary: Tools for creating source code from API specification.
106
112
  test_files: []