From 38ca4d1c8385ea11ccbc7a0c841aff49accbcf17 Mon Sep 17 00:00:00 2001 From: David Jones Date: Wed, 12 Sep 2018 17:51:15 +0100 Subject: [PATCH] fix: Ensure input types are modelled by SwaggerGenerator (#1331) --- Sources/Kitura/SwaggerGenerator.swift | 45 +++++++++++++++++++-------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/Sources/Kitura/SwaggerGenerator.swift b/Sources/Kitura/SwaggerGenerator.swift index 0178e5c3e..fbcd2e620 100644 --- a/Sources/Kitura/SwaggerGenerator.swift +++ b/Sources/Kitura/SwaggerGenerator.swift @@ -598,6 +598,12 @@ struct SwaggerDocument: Encodable { let model = String(describing: name) var modelDefinition: SwaggerModel + // Check to see if we have already built this model + guard self.definitions[model] == nil else { + Log.debug("Already generated model \(model)") + return + } + // then build all it if let modelInfo = try? buildModel(typeInfo) { Log.debug("in addModel(model: \(model))") @@ -938,19 +944,32 @@ extension Router { func registerRoute(route: String, method: String, inputType: I.Type, outputType: O.Type, responseTypes: [SwaggerResponseType]) { Log.debug("Registering \(route) for \(method) method") - let typeInfo: TypeInfo + let inputTypeInfo: TypeInfo do { - typeInfo = try TypeDecoder.decode(outputType) + inputTypeInfo = try TypeDecoder.decode(inputType) } catch { - Log.debug("type decode error") + Log.debug("Failed to decode input type \(inputType)") return } + var outputTypeInfo: TypeInfo? = nil + if inputType != outputType { + do { + outputTypeInfo = try TypeDecoder.decode(outputType) + } catch { + Log.debug("Failed to decode output type \(outputType)") + return + } + } + // insert the path information into the document structure. swagger.addPath(path: route, method: method, id: nil, qParams: nil, inputType: "\(inputType)", responseList: responseTypes) // add model information into the document structure. - swagger.addModel(model: typeInfo) + swagger.addModel(model: inputTypeInfo) + if let outputTypeInfo = outputTypeInfo { + swagger.addModel(model: outputTypeInfo) + } // now walk all the unprocessed models and ensure they are processed. for unprocessed in Array(swagger.unprocessedTypes) { @@ -999,20 +1018,20 @@ extension Router { func registerRoute(route: String, method: String, id: Id.Type, inputType: I.Type, outputType: O.Type, responseTypes: [SwaggerResponseType]) { Log.debug("Registering \(route) for \(method) method") - let typeInfo1: TypeInfo + let inputTypeInfo: TypeInfo do { - typeInfo1 = try TypeDecoder.decode(inputType) + inputTypeInfo = try TypeDecoder.decode(inputType) } catch { - Log.debug("failed to decode input type") + Log.debug("Failed to decode input type \(inputType)") return } - var typeInfo2: TypeInfo? = nil + var outputTypeInfo: TypeInfo? = nil if inputType != outputType { do { - typeInfo2 = try TypeDecoder.decode(outputType) + outputTypeInfo = try TypeDecoder.decode(outputType) } catch { - Log.debug("failed to decode output type") + Log.debug("Failed to decode output type \(outputType)") return } } @@ -1021,9 +1040,9 @@ extension Router { swagger.addPath(path: route, method: method, id: "\(id)", qParams: nil, inputType: "\(inputType)", responseList: responseTypes) // add model information into the document structure. - swagger.addModel(model: typeInfo1) - if let typeInfo2 = typeInfo2 { - swagger.addModel(model: typeInfo2) + swagger.addModel(model: inputTypeInfo) + if let outputTypeInfo = outputTypeInfo { + swagger.addModel(model: outputTypeInfo) } // now walk all the unprocessed models and ensure they are processed.