-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaRepresentable.swift
More file actions
73 lines (65 loc) · 2.06 KB
/
Copy pathSchemaRepresentable.swift
File metadata and controls
73 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// SchemaRepresentable.swift
// feather-openapi
//
// Created by Tibor Bödecs on 2026. 01. 23..
//
import OpenAPIKit30
/// Describes an OpenAPI schema with common properties.
public protocol SchemaRepresentable:
OpenAPISchemaRepresentable,
Identifiable,
ReferencedSchemaMapRepresentable,
// shared properties
RequiredProperty,
TitleProperty,
DescriptionProperty,
NullableProperty
{
/// Indicates whether the schema is deprecated.
var deprecated: Bool? { get }
}
extension SchemaRepresentable {
/// Creates a reference wrapper for this schema.
/// - Parameter required: Whether the reference is required.
/// - Returns: A schema reference.
public func reference(
required: Bool = true
) -> SchemaReference<Self> {
.init(self, required: required)
}
/// Default deprecated flag is `nil`.
public var deprecated: Bool? { nil }
/// Referenced schemas directly used by this schema.
public var referencedSchemaMap:
OrderedDictionary<SchemaID, OpenAPISchemaRepresentable>
{
[:]
}
/// Collects all referenced schemas transitively.
/// - Returns: An ordered dictionary of all referenced schemas.
public func allReferencedSchemaMap() -> OrderedDictionary<
SchemaID, OpenAPISchemaRepresentable
> {
var results = OrderedDictionary<SchemaID, OpenAPISchemaRepresentable>()
var visited = Set<SchemaID>()
collectReferencedSchemaMap(into: &results, visited: &visited)
return results
}
fileprivate func collectReferencedSchemaMap(
into results:
inout OrderedDictionary<SchemaID, OpenAPISchemaRepresentable>,
visited: inout Set<SchemaID>
) {
for (id, schema) in referencedSchemaMap
where visited.insert(id).inserted {
results[id] = schema
if let schema = schema as? SchemaRepresentable {
schema.collectReferencedSchemaMap(
into: &results,
visited: &visited
)
}
}
}
}