Update schemas for relationships

This commit is contained in:
Matt-Yorkley
2021-12-12 22:41:46 +00:00
committed by Maikel Linke
parent 2e59812bc1
commit bd9bed7323
3 changed files with 76 additions and 5 deletions

View File

@@ -90,10 +90,25 @@ class JsonApiSchema
relationships: {
type: :object,
properties: relationships.to_h do |name|
[name, { "$ref" => "#/components/schemas/relationship" }]
[
name,
relationship_schema(name)
]
end
}
}
end
def relationship_schema(name)
if is_singular?(name)
RelationshipSchema.schema(name)
else
RelationshipSchema.collection(name)
end
end
def is_singular?(name)
name.to_s.singularize == name.to_s
end
end
end

View File

@@ -1,12 +1,40 @@
# frozen_string_literal: true
class RelationshipSchema
def self.schema
def self.schema(resource_name = nil)
{
type: :object,
properties: {
data: {
type: [:object, :array]
type: :object,
properties: {
id: { type: :string },
type: { type: :string, example: resource_name }
}
},
links: {
type: :object,
properties: {
related: { type: :string }
}
}
}
}
end
def self.collection(resource_name = nil)
{
type: :object,
properties: {
data: {
type: :array,
items: {
type: :object,
properties: {
id: { type: :string },
type: { type: :string, example: resource_name }
}
}
},
links: {
type: :object,

View File

@@ -71,7 +71,21 @@ components:
type: object
properties:
enterprise:
"$ref": "#/components/schemas/relationship"
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
example: enterprise
links:
type: object
properties:
related:
type: string
meta:
type: object
links:
@@ -127,7 +141,21 @@ components:
type: object
properties:
enterprise:
"$ref": "#/components/schemas/relationship"
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
example: enterprise
links:
type: object
properties:
related:
type: string
meta:
type: object
properties: