Skip to content

Commit

Permalink
fix: Ensure input types are modelled by SwaggerGenerator (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
djones6 committed Sep 12, 2018
1 parent 49d0fd7 commit 38ca4d1
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions Sources/Kitura/SwaggerGenerator.swift
Expand Up @@ -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))")
Expand Down Expand Up @@ -938,19 +944,32 @@ extension Router {
func registerRoute<I: Codable, O: Codable>(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) {
Expand Down Expand Up @@ -999,20 +1018,20 @@ extension Router {
func registerRoute<Id: Identifier, I: Codable, O: Codable>(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
}
}
Expand All @@ -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.
Expand Down

0 comments on commit 38ca4d1

Please sign in to comment.