Skip to content

Commit

Permalink
Add ref support to Swagger::Property
Browse files Browse the repository at this point in the history
  • Loading branch information
erdnaxeli authored and MathiusD committed Dec 19, 2022
1 parent c94b4cd commit 8bc78a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/swagger/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ module Swagger
end

private def build_property(property : Property) : Objects::Property
if property.type == "array"
if ref = property.ref
Objects::Property.use_reference(ref)
elsif property.type == "array"
if items = property.items
if items.is_a?(String)
prop_items = Objects::Schema.use_reference(items)
Expand Down
13 changes: 10 additions & 3 deletions src/swagger/objects/property.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ module Swagger::Objects
struct Property
include JSON::Serializable

getter type : String
def self.use_reference(name : String)
new(ref: "#/components/schemas/#{name}")
end

getter type : String? = nil
getter items : Schema? = nil
getter description : String? = nil
getter default : (String | Int32 | Int64 | Float64 | Bool)? = nil
getter example : (String | Int32 | Int64 | Float64 | Bool)? = nil
getter required : Bool? = nil

def initialize(@type : String, @description : String? = nil, @items : Schema? = nil,
@[JSON::Field(key: "$ref")]
getter ref : String? = nil

def initialize(@type : String? = nil, @description : String? = nil, @items : Schema? = nil,
@default : (String | Int32 | Int64 | Float64 | Bool)? = nil,
@example : (String | Int32 | Int64 | Float64 | Bool)? = nil,
@required : Bool? = nil)
@required : Bool? = nil, @ref : String? = nil,)
end
end
end
3 changes: 2 additions & 1 deletion src/swagger/property.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ module Swagger
property default_value
property example
property required
property ref

def initialize(@name : String, @type : String = "string", @format : String? = nil,
@items : (Object | String)? = nil, @description : String? = nil,
@default_value : (String | Int32 | Int64 | Float64 | Bool)? = nil,
@example : (String | Int32 | Int64 | Float64 | Bool)? = nil,
@required : Bool? = nil)
@required : Bool? = nil, @ref : String? = nil)
end
end
end

0 comments on commit 8bc78a8

Please sign in to comment.