krakend-openapi-importer 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1a7b6aa237ddcad0132e8c293cd42181382f5f4e32cbf174b18c9760abb5120
4
- data.tar.gz: 243242837fbc0ebca42f8e311eeb8da9a7a1aed32fc4b1f2d7ceddf9f70db7c7
3
+ metadata.gz: eccd5c3cd0254e656a85647f09b55dc6ab23a2cbe12df54f300e9f55b6232680
4
+ data.tar.gz: 1c00cf1e660f5f2d0c56eb82eea50300448b433f6396476ec2c5d0b212720703
5
5
  SHA512:
6
- metadata.gz: 9b6f24df2079fd9131a46a1cb2e19af2e17c4eba7a5b47db4b1fbccc57ed9c3d6b707663918117bf5f0d3b09bde7e8e122476eab8b8cfa2be6e8142af162ef1f
7
- data.tar.gz: 67ec817c14e10bb62d087b7038750100fad0f3f219e32028d5531b054c2b74263df2ecdde38fb75fbdf6d35e196fa69bbb510c1f7a007b04eedc787e6471b9b4
6
+ metadata.gz: 9fd2c84e3b98e8953a26b730804179c3b567ec0e3b70e242e53ab4d8d9fc52123894c174a82c382e405fbd09368c460b989c68bd27c107b2d2fd432dacafd466
7
+ data.tar.gz: b10e0267cd7a62694bfe94ce853fc5f648d3a88adacc667855d6fafb012084e7ece04a399960d0cdf8e531bfd27317fbebc4c16e9ff67ada60f0f74f7433f025
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- krakend-openapi-importer (0.2.0)
4
+ krakend-openapi-importer (0.3.0)
5
5
  thor (~> 1.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Import endpoints from OpenAPI spec to KrakenD endpoint configuration. Supports OpenAPI v3.0 and up.
4
4
 
5
- [![Ruby](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml/badge.svg)](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml)
5
+ [![Ruby](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml/badge.svg)](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml) [![Gem Version](https://badge.fury.io/rb/krakend-openapi-importer.svg)](https://badge.fury.io/rb/krakend-openapi-importer)
6
6
 
7
7
  In case you have a different version of OpenAPI you can use https://github.com/LucyBot-Inc/api-spec-converter to convert to the v3.0.
8
8
 
@@ -34,12 +34,15 @@ Example config
34
34
  ```yaml
35
35
  ---
36
36
  all_roles: ['admin', 'guest'] # all available roles for JWT validator
37
- pretty: false
37
+ pretty: true
38
+ output: 'output.json'
38
39
  defaults:
39
40
  endpoint:
40
41
  output_encoding: "no-op" # act like a no-op proxy
41
42
  input_headers: [ "*" ]
42
43
  input_query_strings: [ "*" ]
44
+ backend:
45
+ - encoding: "no-op"
43
46
  plugins:
44
47
  auth_validator:
45
48
  alg: 'RS256'
@@ -17,9 +17,9 @@ module KrakendOpenAPI
17
17
  end
18
18
 
19
19
  def execute
20
- paths = OA3Reader.new.read(@spec).paths
21
- endpoints = OA3ToKrakendTransformer.new(@importer_config).transform_paths(paths)
22
- KrakendWriter.new(@importer_config).write(endpoints)
20
+ paths = OA3Reader.new(@spec).paths
21
+ endpoints = OA3ToKrakendTransformer.new(paths, @importer_config).transform_paths
22
+ KrakendWriter.new(endpoints, @importer_config).write
23
23
  end
24
24
  end
25
25
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KrakendOpenAPI
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -6,22 +6,27 @@ require_relative '../readers/yaml_reader'
6
6
  module KrakendOpenAPI
7
7
  # Reads OpenAPI spec files
8
8
  class OA3Reader
9
- attr_reader :data
9
+ def initialize(path)
10
+ @path = path
11
+ end
12
+
13
+ def paths
14
+ read unless @data
15
+ @data['paths']
16
+ end
10
17
 
11
- def read(path)
12
- if ['.json'].include?(File.extname(path))
13
- @data = JsonReader.new(path).read
14
- elsif %w[.yaml .yml].include?(File.extname(path))
15
- @data = YamlReader.new(path).read
18
+ private
19
+
20
+ def read
21
+ if ['.json'].include?(File.extname(@path))
22
+ @data = JsonReader.new(@path).read
23
+ elsif %w[.yaml .yml].include?(File.extname(@path))
24
+ @data = YamlReader.new(@path).read
16
25
  else
17
26
  raise StandardError, 'OA3Reader does not support this format'
18
27
  end
19
28
 
20
29
  self
21
30
  end
22
-
23
- def paths
24
- data['paths']
25
- end
26
31
  end
27
32
  end
@@ -5,14 +5,17 @@ require_relative './jwt_validator_transformer'
5
5
  module KrakendOpenAPI
6
6
  # Transforms OpenAPI paths to KrakenD endpoints
7
7
  class OA3ToKrakendTransformer
8
- def initialize(importer_config)
8
+ def initialize(paths, importer_config)
9
+ @paths = paths
9
10
  @importer_config = importer_config
10
11
  end
11
12
 
12
- def transform_paths(paths)
13
- paths.map { |path, methods| transform_path(path, methods) }.flatten
13
+ def transform_paths
14
+ @paths.map { |path, methods| transform_path(path, methods) }.flatten
14
15
  end
15
16
 
17
+ private
18
+
16
19
  def transform_path(path, methods)
17
20
  methods.map { |method, operation| transform_method(path, method, operation) }
18
21
  end
@@ -32,7 +35,7 @@ module KrakendOpenAPI
32
35
  output_encoding: @importer_config['defaults']['endpoint']['output_encoding'],
33
36
  input_headers: @importer_config['defaults']['endpoint']['input_headers'],
34
37
  input_query_strings: @importer_config['defaults']['endpoint']['input_query_strings'],
35
- backend: [{ url_pattern: path }]
38
+ backend: [{ url_pattern: path, encoding: @importer_config['defaults']['backend'][0]['encoding'] }]
36
39
  }
37
40
 
38
41
  extra_config = plugins.each_with_object({}) do |plugin, memo|
@@ -5,18 +5,20 @@ require 'json'
5
5
  module KrakendOpenAPI
6
6
  # Writes KrakenD configuration to a file
7
7
  class KrakendWriter
8
- def initialize(importer_config)
8
+ def initialize(endpoints, importer_config)
9
+ @endpoints = endpoints
9
10
  @importer_config = importer_config
10
- @file_path = "#{Dir.pwd}/output.json"
11
+ @output = @importer_config['output'] || 'output.json'
12
+ @file_path = "#{Dir.pwd}/#{@output}"
11
13
  end
12
14
 
13
- def write(endpoints)
15
+ def write
14
16
  pretty_output = !!@importer_config['pretty'] # rubocop:disable Style/DoubleNegation
15
17
  json_generate = pretty_output ? ->(obj) { JSON.pretty_generate(obj) } : ->(obj) { JSON.dump(obj) }
16
18
  File.write(@file_path, json_generate.call({
17
19
  '$schema': 'https://www.krakend.io/schema/v3.json',
18
20
  version: 3,
19
- endpoints: endpoints
21
+ endpoints: @endpoints
20
22
  }))
21
23
  end
22
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: krakend-openapi-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Semenenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-12-29 00:00:00.000000000 Z
11
+ date: 2023-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor