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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Kitura/Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public class Router {
// MARK: Swagger

/// Contains the structures needed for swagger document generation
var swagger: SwaggerDocument
public var swagger: SwaggerDocument
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that it introduces an ordering problem. Routes are added to the swagger document each time you call router.get() et. al, and so if you were to reassign router.swagger after registering some routes, those routes would no longer appear in the document.

Perhaps you could leave this internal, and instead add an additional parameter eg. apiDocument: SwaggerDocument = SwaggerDocument() to router.init?


/// Returns the current in-memory representation of Codable routes as a
/// Swagger document in JSON format, or nil if the document cannot be
Expand Down
8 changes: 5 additions & 3 deletions Sources/Kitura/SwaggerGenerator.swift
Original file line number Diff line number Diff line change
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
36 changes: 36 additions & 0 deletions Tests/KituraTests/TestSwaggerGeneration.swift
Original file line number Diff line number Diff line change
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,41 @@ 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()
localRouter.swagger = 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