mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Update schemas for relationships
This commit is contained in:
committed by
Maikel Linke
parent
2e59812bc1
commit
bd9bed7323
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user