@@ -30,7 +30,9 @@ public enum TransferScheme: String {
3030}
3131
3232public protocol NamedMappable {
33- init ( name: String , jsonDictionary: JSONDictionary ) throws
33+ init ( name: String ,
34+ jsonDictionary: JSONDictionary ,
35+ topLevelSecurityRequirements: [ SecurityRequirement ] ? ) throws
3436}
3537
3638extension SwaggerSpec {
@@ -69,12 +71,20 @@ extension SwaggerSpec: JSONObjectConvertible {
6971
7072 public init ( jsonDictionary: JSONDictionary ) throws {
7173
72- func decodeNamed< T: NamedMappable > ( jsonDictionary: JSONDictionary , key: String ) throws -> [ T ] {
74+ func decodeNamed< T: NamedMappable > (
75+ jsonDictionary: JSONDictionary ,
76+ key: String ,
77+ topLevelSecurityRequirements: [ SecurityRequirement ] ?
78+ ) throws -> [ T ] {
7379 var values : [ T ] = [ ]
7480 if let dictionary = jsonDictionary [ key] as? [ String : Any ] {
7581 for (key, value) in dictionary {
7682 if let dictionary = value as? [ String : Any ] {
77- let value = try T ( name: key, jsonDictionary: dictionary)
83+ let value = try T (
84+ name: key,
85+ jsonDictionary: dictionary,
86+ topLevelSecurityRequirements: topLevelSecurityRequirements
87+ )
7888 values. append ( value)
7989 }
8090 }
@@ -91,13 +101,15 @@ extension SwaggerSpec: JSONObjectConvertible {
91101
92102 info = try jsonDictionary. json ( atKeyPath: " info " )
93103 servers = jsonDictionary. json ( atKeyPath: " servers " ) ?? [ ]
94- securityRequirements = jsonDictionary . json ( atKeyPath : " security " )
104+ securityRequirements = type ( of : self ) . getSecurityRequirements ( from : jsonDictionary )
95105 if jsonDictionary [ " components " ] != nil {
96106 components = try jsonDictionary. json ( atKeyPath: " components " )
97107 } else {
98108 components = Components ( )
99109 }
100- paths = try decodeNamed ( jsonDictionary: jsonDictionary, key: " paths " )
110+ paths = try decodeNamed ( jsonDictionary: jsonDictionary,
111+ key: " paths " ,
112+ topLevelSecurityRequirements: securityRequirements)
101113 operations = paths. reduce ( [ ] ) { $0 + $1. operations }
102114 . sorted ( by: { ( lhs, rhs) -> Bool in
103115 if lhs. path == rhs. path {
@@ -110,4 +122,15 @@ extension SwaggerSpec: JSONObjectConvertible {
110122 try resolver. resolve ( )
111123 self = resolver. spec
112124 }
125+
126+ private static func getSecurityRequirements(
127+ from jsonDictionary: JSONDictionary
128+ ) -> [ SecurityRequirement ] ? {
129+ guard let securityRequirements: [ SecurityRequirement ] =
130+ jsonDictionary. json ( atKeyPath: " security " ) else {
131+ return nil
132+ }
133+
134+ return securityRequirements. updatingRequiredFlag ( )
135+ }
113136}
0 commit comments