Add support for additional_properties#92
Open
olivier-thatch wants to merge 1 commit into
Open
Conversation
Danger ReportErrors
Markdowns* [#92](https://github.com/ruby-grape/grape-swagger-entity/pull/92) Add support for `additional_properties` - [@olivier-thatch](https://github.com/olivier-thatch). |
8c4669b to
53102be
Compare
additional_properties
53102be to
1b86322
Compare
numbata
reviewed
Jul 8, 2026
| type | ||
| end | ||
|
|
||
| def parse_additional_properties(value) |
Collaborator
There was a problem hiding this comment.
Probably, we should route additional_properties values through the same primitive type mapping as regular documented attributes before emitting the schema.
The current implementation handles String, but formatted primitives can currently emit invalid Swagger types. For example:
additional_properties: Floatcan become:
{ additionalProperties: { type: 'float' } }but should be:
{ additionalProperties: { type: 'number', format: 'float' } }Same idea for Date, DateTime, Time, BigDecimal, Integer, etc. One possible direction:
# Here untested pseudo-code that looks like a ruby
def document_data_type(documentation, data_type)
type = primitive_data_type_schema(data_type) || { type: data_type }
type[:format] = documentation[:format] if documentation.key?(:format)
values = documentation[:values]
values = values.call if values.is_a?(Proc)
type[:enum] = values if values.is_a?(Array)
if documentation.key?(:additional_properties)
additional_properties = parse_additional_properties(documentation[:additional_properties])
type[:additionalProperties] = additional_properties unless additional_properties.nil?
end
type
end
def parse_additional_properties(value)
case value
when String, Symbol
if direct_model_type?(value)
name = GrapeSwagger::Entity::Helper.model_name(value, endpoint)
{ '$ref' => "#/definitions/#{name}" }
else
data_type = GrapeSwagger::DocMethods::DataType.call(value.to_s)
primitive_data_type_schema(data_type) || { type: data_type }
end
when Class
data_type = GrapeSwagger::DocMethods::DataType.call(value)
if (schema = primitive_data_type_schema(data_type))
schema
elsif value <= Grape::Entity
name = GrapeSwagger::Entity::Helper.model_name(value, endpoint)
{ '$ref' => "#/definitions/#{name}" }
else
{ type: data_type }
end
when true, false, Hash
value
end
end
def primitive_data_type_schema(data_type)
return unless GrapeSwagger::DocMethods::DataType.primitive?(data_type)
type, format = GrapeSwagger::DocMethods::DataType.mapping(data_type)
{ type: type }.tap { |schema| schema[:format] = format if format }
endThis would also avoid treating any non-primitive class as an entity, which can make PORO classes go through the entity parser path and fail.
Could we add specs for Float, Date, DateTime, string aliases like 'Boolean' / 'Hash', symbol aliases like :string, an entity class, and a non-entity PORO class?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add support for
additional_properties, similar to what already exists for params in grape-swagger (cf. docs).Code is from Claude. I reviewed it, it looks sane to me.
Closes #83.