Skip to content

Commit e585af2

Browse files
committed
Default float response examples to 0.0 when no example is provided
1 parent 1c9315d commit e585af2

3 files changed

Lines changed: 93 additions & 50 deletions

File tree

lib/open_api_import/get_examples.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ module LibOpenApiImport
3131
case val[:type].downcase
3232
when "string"
3333
example << " #{prop.to_sym}: \"#{format}\", "
34-
when "integer", "number"
34+
when "integer"
3535
example << " #{prop.to_sym}: 0, "
36+
when "number"
37+
format_name = format.to_s.downcase
38+
number_value = if %w[float double decimal].include?(format_name)
39+
"0.0"
40+
else
41+
"0"
42+
end
43+
example << " #{prop.to_sym}: #{number_value}, "
3644
when "boolean"
3745
example << " #{prop.to_sym}: true, "
3846
when "array"
@@ -59,13 +67,12 @@ module LibOpenApiImport
5967
else
6068
#todo: differ between response examples and data examples
6169
examplet = get_response_examples({ schema: val }, remove_readonly).join("\n")
62-
examplet = '[]' if examplet.empty?
70+
examplet = "[]" if examplet.empty?
6371
if type == :only_value
6472
example << examplet
6573
else
6674
example << " #{prop.to_sym}: " + examplet + ", "
67-
end
68-
75+
end
6976
end
7077
when "object"
7178
#todo: differ between response examples and data examples
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
swagger: "2.0"
2+
info:
3+
title: "Float API"
4+
version: "1.0.0"
5+
host: example.com
6+
basePath: /
7+
schemes:
8+
- https
9+
produces:
10+
- application/json
11+
paths:
12+
/resource:
13+
get:
14+
operationId: getResource
15+
responses:
16+
'200':
17+
description: Float response example
18+
schema:
19+
type: object
20+
properties:
21+
floatValue:
22+
type: number
23+
format: float
24+
description: Float field with float format
25+
integerValue:
26+
type: integer
27+
format: int32
28+
description: Integer field with int format
Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,61 @@
1-
require 'open_api_import'
1+
require "open_api_import"
22

33
RSpec.describe OpenApiImport do
4+
describe "#from" do
5+
it "adds response codes as keys on responses array" do
6+
file_name = "./spec/fixtures/v2.0/yaml/petstore-simple.yaml"
7+
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
8+
OpenApiImport.from file_name, create_method_name: :operation_id
9+
expect(File.exist?("#{file_name}.rb")).to eq true
10+
content = File.read("#{file_name}.rb")
11+
eval(content)
12+
req = Swagger::SwaggerPetstore::V1_0_0::Root.find_pets
13+
expect(req.key?(:responses)).to eq true
14+
expect(req[:responses].class).to eq Hash
15+
expect(req[:responses].keys).to eq [:'200', :'default']
16+
end
417

5-
describe '#from' do
6-
7-
it 'adds response codes as keys on responses array' do
8-
file_name = './spec/fixtures/v2.0/yaml/petstore-simple.yaml'
9-
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
10-
OpenApiImport.from file_name, create_method_name: :operation_id
11-
expect(File.exist?("#{file_name}.rb")).to eq true
12-
content = File.read("#{file_name}.rb")
13-
eval(content)
14-
req = Swagger::SwaggerPetstore::V1_0_0::Root.find_pets
15-
expect(req.key?(:responses)).to eq true
16-
expect(req[:responses].class).to eq Hash
17-
expect(req[:responses].keys).to eq [:'200', :'default']
18-
end
19-
20-
it 'adds the message for the response on responses array' do
21-
file_name = './spec/fixtures/v2.0/yaml/petstore-simple.yaml'
22-
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
23-
OpenApiImport.from file_name, create_method_name: :operation_id
24-
expect(File.exist?("#{file_name}.rb")).to eq true
25-
content = File.read("#{file_name}.rb")
26-
eval(content)
27-
req = Swagger::SwaggerPetstore::V1_0_0::Root.find_pets
28-
expect(req[:responses][:'200'].key?(:message)).to eq true
29-
expect(req[:responses][:'200'][:message]).to eq "pet response"
30-
end
31-
32-
it 'adds the data body for the response on responses array' do
33-
file_name = './spec/fixtures/v2.0/yaml/petstore-simple.yaml'
34-
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
35-
OpenApiImport.from file_name, create_method_name: :operation_id
36-
expect(File.exist?("#{file_name}.rb")).to eq true
37-
content = File.read("#{file_name}.rb")
38-
eval(content)
39-
req = Swagger::SwaggerPetstore::V1_0_0::Root.find_pets
40-
expect(req[:responses][:'200'].key?(:data)).to eq true
41-
data = [{
42-
name: "string",
43-
tag: "string",
44-
id: 0,
45-
}]
46-
expect(req[:responses][:'200'][:data]).to eq data
47-
end
48-
49-
18+
it "adds the message for the response on responses array" do
19+
file_name = "./spec/fixtures/v2.0/yaml/petstore-simple.yaml"
20+
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
21+
OpenApiImport.from file_name, create_method_name: :operation_id
22+
expect(File.exist?("#{file_name}.rb")).to eq true
23+
content = File.read("#{file_name}.rb")
24+
eval(content)
25+
req = Swagger::SwaggerPetstore::V1_0_0::Root.find_pets
26+
expect(req[:responses][:'200'].key?(:message)).to eq true
27+
expect(req[:responses][:'200'][:message]).to eq "pet response"
5028
end
5129

30+
it "adds the data body for the response on responses array" do
31+
file_name = "./spec/fixtures/v2.0/yaml/petstore-simple.yaml"
32+
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
33+
OpenApiImport.from file_name, create_method_name: :operation_id
34+
expect(File.exist?("#{file_name}.rb")).to eq true
35+
content = File.read("#{file_name}.rb")
36+
eval(content)
37+
req = Swagger::SwaggerPetstore::V1_0_0::Root.find_pets
38+
expect(req[:responses][:'200'].key?(:data)).to eq true
39+
data = [{
40+
name: "string",
41+
tag: "string",
42+
id: 0,
43+
}]
44+
expect(req[:responses][:'200'][:data]).to eq data
45+
end
5246

47+
it "uses 0.0 as default example for float numbers without explicit example" do
48+
file_name = "./spec/fixtures/v2.0/yaml/float_response.yaml"
49+
File.delete("#{file_name}.rb") if File.exist?("#{file_name}.rb")
50+
OpenApiImport.from file_name, create_method_name: :operation_id
51+
expect(File.exist?("#{file_name}.rb")).to eq true
52+
content = File.read("#{file_name}.rb")
53+
eval(content)
54+
req = Swagger::FloatApi::V1_0_0::Root.get_resource
55+
expect(req[:responses][:'200'][:data]).to eq({
56+
floatValue: 0.0,
57+
integerValue: 0,
58+
})
59+
end
60+
end
5361
end

0 commit comments

Comments
 (0)