Skip to content

Commit 88d5e26

Browse files
authored
Improve accuracy of data types for numbers (#30)
Now when we know we will return a float, we declare `"format": "float"` which is an optional hint. https://swagger.io/docs/specification/data-models/data-types/#numbers closes: #5
1 parent 19bda78 commit 88d5e26

File tree

6 files changed

+19
-20
lines changed

6 files changed

+19
-20
lines changed

lib/apia/open_api/helpers.rb

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def convert_type_to_open_api_data_type(type)
3838
end
3939
end
4040

41+
def generate_scalar_schema(type)
42+
schema = {
43+
type: convert_type_to_open_api_data_type(type)
44+
}
45+
schema[:format] = "float" if type.klass == Apia::Scalars::Decimal
46+
schema
47+
end
48+
4149
def generate_schema_ref(definition, id: nil, **schema_opts)
4250
id ||= generate_id_from_definition(definition.type.klass.definition)
4351
success = add_to_components_schemas(definition, id, **schema_opts)

lib/apia/open_api/objects/parameters.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def add_to_spec
3939
if @argument.type.enum? || @argument.type.object?
4040
items = generate_schema_ref(@argument)
4141
else
42-
items = { type: convert_type_to_open_api_data_type(@argument.type) }
42+
items = generate_scalar_schema(@argument.type)
4343
end
4444

4545
param = {
@@ -64,9 +64,7 @@ def add_to_spec
6464
param = {
6565
name: @argument.name.to_s,
6666
in: "query",
67-
schema: {
68-
type: convert_type_to_open_api_data_type(@argument.type)
69-
}
67+
schema: generate_scalar_schema(@argument.type)
7068
}
7169
param[:required] = true if @argument.required?
7270
add_to_parameters(param)
@@ -84,9 +82,7 @@ def generate_argument_set_params
8482
param = {
8583
name: "#{@argument.name}[#{child_arg.name}]",
8684
in: "query",
87-
schema: {
88-
type: convert_type_to_open_api_data_type(child_arg.type)
89-
}
85+
schema: generate_scalar_schema(child_arg.type)
9086
}
9187
description = []
9288
description << formatted_description(child_arg.description) if child_arg.description.present?

lib/apia/open_api/objects/request_body.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def add_to_spec
3838
if arg.type.argument_set? || arg.type.enum?
3939
items = generate_schema_ref(arg)
4040
else
41-
items = { type: convert_type_to_open_api_data_type(arg.type) }
41+
items = generate_scalar_schema(arg.type)
4242
end
4343

4444
@properties[arg.name.to_s] = {
@@ -47,10 +47,8 @@ def add_to_spec
4747
}
4848
elsif arg.type.argument_set? || arg.type.enum?
4949
@properties[arg.name.to_s] = generate_schema_ref(arg)
50-
else # scalar
51-
@properties[arg.name.to_s] = {
52-
type: convert_type_to_open_api_data_type(arg.type)
53-
}
50+
else
51+
@properties[arg.name.to_s] = generate_scalar_schema(arg.type)
5452
end
5553
end
5654

lib/apia/open_api/objects/response.rb

+2-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,8 @@ def generate_properties_for_field(field_name, field)
8181
build_properties_for_array(field_name, field, properties)
8282
elsif field.type.object? || field.type.enum?
8383
build_properties_for_object_or_enum(field_name, field, properties)
84-
else # scalar
85-
properties[field_name] = {
86-
type: convert_type_to_open_api_data_type(field.type)
87-
}
84+
else
85+
properties[field_name] = generate_scalar_schema(field.type)
8886
end
8987
properties[field_name][:nullable] = true if field.null?
9088
properties

lib/apia/open_api/objects/schema.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ def generate_schema_for_child(schema, child, all_properties_included)
127127
else # scalar
128128
schema[:type] = "object"
129129
schema[:properties] ||= {}
130-
schema[:properties][child.name.to_s] = {
131-
type: convert_type_to_open_api_data_type(child.type)
132-
}
130+
schema[:properties][child.name.to_s] = generate_scalar_schema(child.type)
133131
end
134132

135133
if child.try(:required?)

spec/support/fixtures/openapi.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,8 @@
781781
}
782782
},
783783
"as_decimal": {
784-
"type": "number"
784+
"type": "number",
785+
"format": "float"
785786
},
786787
"as_base64": {
787788
"type": "string"

0 commit comments

Comments
 (0)