Skip to content

[Generator] Generate server variables #348

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

Merged
merged 2 commits into from
Oct 26, 2023
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
16 changes: 10 additions & 6 deletions Sources/_OpenAPIGeneratorCore/Renderer/TextBasedRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,17 @@ struct TextBasedRenderer: RendererProtocol {
case .nil: write("nil")
case .array(let items):
writer.writeLine("[")
writer.nextLineAppendsToLastLine()
for (item, isLast) in items.enumeratedWithLastMarker() {
renderExpression(item)
if !isLast {
writer.nextLineAppendsToLastLine()
writer.writeLine(", ")
if !items.isEmpty {
writer.withNestedLevel {
for (item, isLast) in items.enumeratedWithLastMarker() {
renderExpression(item)
if !isLast {
writer.nextLineAppendsToLastLine()
writer.writeLine(",")
}
}
}
} else {
writer.nextLineAppendsToLastLine()
}
writer.writeLine("]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extension FileTranslator {
/// - Returns: A `Declaration` representing the translated struct.
func translateStructBlueprintInitializer(typeName: TypeName, properties: [PropertyBlueprint]) -> Declaration {

let comment: Comment = .doc(properties.initializerComment(typeName: typeName.shortSwiftName))
let comment: Comment = properties.initializerComment(typeName: typeName.shortSwiftName)

let decls: [(ParameterDescription, String)] = properties.map { property in
(
Expand Down Expand Up @@ -145,19 +145,11 @@ fileprivate extension Array where Element == PropertyBlueprint {
/// the properties contained in the current array.
/// - Parameter typeName: The name of the structure type.
/// - Returns: A comment string describing the initializer.
func initializerComment(typeName: String) -> String {
var components: [String] = ["Creates a new `\(typeName)`."]
if !isEmpty {
var parameterComponents: [String] = []
parameterComponents.append("- Parameters:")
for parameter in self {
parameterComponents.append(
" - \(parameter.swiftSafeName):\(parameter.comment?.firstLineOfContent.map { " \($0)" } ?? "")"
)
}
components.append("")
components.append(parameterComponents.joined(separator: "\n"))
}
return components.joined(separator: "\n")
func initializerComment(typeName: String) -> Comment {
Comment.functionComment(
abstract: "Creates a new `\(typeName)`.",
parameters: map { ($0.swiftSafeName, $0.comment?.firstLineOfContent) }
)! // This force-unwrap is safe as the method never returns nil when
// a non-nil abstract is provided.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,30 @@ extension Comment {
}
)
}

/// Returns a documentation comment for a function with the provided
/// parameters.
/// - Parameters:
/// - abstract: The documentation of the function.
/// - parameters: The parameters.
/// - Returns: A documentation comment for the function.
static func functionComment(abstract: String?, parameters: [(name: String, comment: String?)]) -> Comment? {
guard !parameters.isEmpty else { return abstract.map { .doc($0) } }
var components: [String] = abstract.map { [$0] } ?? []
var parameterComponents: [String] = []
parameterComponents.append("- Parameters:")
for (name, comment) in parameters {
let parameterComment: String
if let comment {
parameterComment = Comment.doc(comment).firstLineOfContent.map { " \($0)" } ?? ""
} else {
parameterComment = ""
}
parameterComponents.append(" - \(name):\(parameterComment)")
}
components.append("")
components.append(parameterComponents.joined(separator: "\n"))
return .doc(components.joined(separator: "\n"))
}
}

extension ComponentDictionaryLocatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,33 @@ extension TypesFileTranslator {
/// declare the method under.
func translateServer(index: Int, server: OpenAPI.Server) -> Declaration {
let methodName = "\(Constants.ServerURL.propertyPrefix)\(index+1)"
let parameters: [ParameterDescription] = server.variables.map { (key, value) in
.init(label: key, type: .init(TypeName.string), defaultValue: .literal(value.default))
}
let variableInitializers: [Expression] = server.variables.map { (key, value) in
let allowedValuesArg: FunctionArgumentDescription?
if let allowedValues = value.enum {
allowedValuesArg = .init(
label: "allowedValues",
expression: .literal(.array(allowedValues.map { .literal($0) }))
)
} else {
allowedValuesArg = nil
}
return .dot("init")
.call(
[
.init(label: "name", expression: .literal(key)),
.init(label: "value", expression: .identifierPattern(key)),
] + (allowedValuesArg.flatMap { [$0] } ?? [])
)
}
let methodDecl = Declaration.commentable(
server.description.flatMap { .doc($0) },
.functionComment(abstract: server.description, parameters: server.variables.map { ($0, $1.description) }),
.function(
accessModifier: config.access,
kind: .function(name: methodName, isStatic: true),
parameters: [],
parameters: parameters,
keywords: [.throws],
returnType: .identifierType(TypeName.url),
body: [
Expand All @@ -41,7 +62,7 @@ extension TypesFileTranslator {
.init(
label: "validatingOpenAPIServerURL",
expression: .literal(.string(server.urlTemplate.absoluteString))
)
), .init(label: "variables", expression: .literal(.array(variableInitializers))),
])
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,19 @@ final class Test_TextBasedRenderer: XCTestCase {
.array([.literal(.nil)]),
renderedBy: TextBasedRenderer.renderLiteral,
rendersAs: #"""
[nil]
[
nil
]
"""#
)
try _test(
.array([.literal(.nil), .literal(.nil)]),
renderedBy: TextBasedRenderer.renderLiteral,
rendersAs: #"""
[nil, nil]
[
nil,
nil
]
"""#
)
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ servers:
- url: https://example.com/api
description: Example Petstore implementation service
- url: /api
- url: https://{subdomain}.example.com:{port}/{basePath}
description: A custom domain.
variables:
subdomain:
default: test
description: A subdomain name.
port:
enum:
- '443'
- 8443
default: '443'
basePath:
default: v1
description: The base API path.
paths:
/pets:
summary: Work with pets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ public struct Client: APIProtocol {
serializer: { input in
let path = try converter.renderedPath(
template: "/pets/{}",
parameters: [input.path.petId]
parameters: [
input.path.petId
]
)
var request: HTTPTypes.HTTPRequest = .init(
soar_path: path,
Expand Down Expand Up @@ -539,7 +541,9 @@ public struct Client: APIProtocol {
serializer: { input in
let path = try converter.renderedPath(
template: "/pets/{}/avatar",
parameters: [input.path.petId]
parameters: [
input.path.petId
]
)
var request: HTTPTypes.HTTPRequest = .init(
soar_path: path,
Expand Down
Loading