Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to public the SwaggerDocument object of the router. #1483

Merged
merged 4 commits into from Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sources/Kitura/Router.swift
Expand Up @@ -92,8 +92,10 @@ public class Router {
/// FileResourceServer to serve the default
/// "Welcome to Kitura" page and related
/// assets.
public init(mergeParameters: Bool = false, enableWelcomePage: Bool = true) {
self.swagger = SwaggerDocument()
/// - Parameter apiDocument: Optional parameter that allows customization of the OpenAPI document
/// describing this Router.
public init(mergeParameters: Bool = false, enableWelcomePage: Bool = true, apiDocument: SwaggerDocument = SwaggerDocument()) {
djones6 marked this conversation as resolved.
Show resolved Hide resolved
self.swagger = apiDocument
self.mergeParameters = mergeParameters
self.fileResourceServer = enableWelcomePage ? FileResourceServer() : nil

Expand Down
8 changes: 5 additions & 3 deletions Sources/Kitura/SwaggerGenerator.swift
Expand Up @@ -348,7 +348,7 @@ enum SwiftType: String {
}
}

struct SwaggerDocument: Encodable {
public struct SwaggerDocument: Encodable {
// swagger document is the conatiner for all the openAPI information that we
// can gather from the Kitura server. Once data is written to the structures
// in SwaggerDocument, a call to toDocument() is used to write the document
Expand All @@ -373,9 +373,11 @@ struct SwaggerDocument: Encodable {
private var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy = .deferredToDate
private var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate

public init() {
public init(title: String = "Kitura Project",
description: String = "Generated by Kitura",
version: String = "1.0") {
self.swagger = "2.0"
self.info = SwaggerInfo(version: "1.0", description: "Generated by Kitura", title: "Kitura Project")
self.info = SwaggerInfo(version: version, description: description, title: title)
self.basePath = "/"
self.schemes = [] // The valid schemes will be populated later, at serialisation time
self.paths = [:]
Expand Down
35 changes: 35 additions & 0 deletions Tests/KituraTests/TestSwaggerGeneration.swift
Expand Up @@ -168,6 +168,7 @@ final class TestSwaggerGeneration: KituraTest, KituraTestSuite {
("testSwaggerVersion", testSwaggerVersion),
("testBasePath", testBasePath),
("testInfo", testInfo),
("testCustomInfo", testCustomInfo),
("testSwaggerDefinitions", testSwaggerDefinitions),
("testSwaggerContent", testSwaggerContent),
("testSwaggerQueryParams", testSwaggerQueryParams),
Expand Down Expand Up @@ -1019,6 +1020,40 @@ final class TestSwaggerGeneration: KituraTest, KituraTestSuite {
}
}
}

//
// Test that our swagger document defines the custom info section.
//
func testCustomInfo() {
let customTitle = "My Project"
let customDescription = "My project generated by Kitura"
let customVersion = "0.0.1"
let localRouter = Router(apiDocument: SwaggerDocument(title: customTitle,
description: customDescription,
version: customVersion))
guard let dict = getSwaggerDictionary(for: localRouter) else {
return XCTFail("Unable to get swagger dictionary")
}
if let info = dict["info"] as? [String: String] {
if let title = info["title"] {
XCTAssertTrue(title == customTitle, "title is incorrect")
} else {
XCTFail("title is missing")
}

if let desc = info["description"] {
XCTAssertTrue(desc == customDescription, "description is incorrect")
} else {
XCTFail("description is missing")
}

if let version = info["version"] {
XCTAssertTrue(version == customVersion, "version is incorrect")
} else {
XCTFail("version is missing")
}
}
}

//
// Test that our swagger document contains the expected paths
Expand Down