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

Generate deprecation annotations based on OpenAPI document #92

Merged
merged 8 commits into from
Jun 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -1451,3 +1451,13 @@ extension KeywordKind {
.try(hasPostfixQuestionMark: false)
}
}

extension Declaration {
/// Returns a new deprecated variant of the declaration if `shouldDeprecate` is true.
func deprecate(if shouldDeprecate: Bool) -> Self {
if shouldDeprecate {
return .deprecated(.init(), self)
}
return self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ extension FileTranslator {
func translateObjectStruct(
typeName: TypeName,
openAPIDescription: String?,
objectContext: JSONSchema.ObjectContext
objectContext: JSONSchema.ObjectContext,
isDeprecated: Bool
) throws -> Declaration {

let documentedProperties: [PropertyBlueprint] =
Expand Down Expand Up @@ -61,6 +62,7 @@ extension FileTranslator {
}
return PropertyBlueprint(
comment: comment,
isDeprecated: value.deprecated,
originalName: key,
typeUsage: propertyType,
associatedDeclarations: associatedDeclarations
Expand All @@ -86,6 +88,7 @@ extension FileTranslator {
return translateStructBlueprint(
StructBlueprint(
comment: comment,
isDeprecated: isDeprecated,
access: config.access,
typeName: typeName,
conformances: Constants.ObjectStruct.conformances,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ extension FileTranslator {
let objectDecl = try translateObjectStruct(
typeName: typeName,
openAPIDescription: overrides.userDescription ?? coreContext.description,
objectContext: objectContext
objectContext: objectContext,
isDeprecated: coreContext.deprecated
)
return [objectDecl]
case let .string(coreContext, _):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ extension FileTranslator {
guard let comment = blueprint.comment else {
return structDecl
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
}
return .commentable(comment, structDecl)
return .commentable(comment, structDecl.deprecate(if: blueprint.isDeprecated))
}

/// Returns a declaration of an initializer declared in a structure.
Expand Down Expand Up @@ -144,6 +144,7 @@ extension FileTranslator {
type: propertyTypeName
)
)
.deprecate(if: property.isDeprecated)
)
return property.associatedDeclarations + [propertyDecl]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ struct StructBlueprint {
/// A documentation comment for the structure.
var comment: Comment?

/// Whether the type should be annotated as deprecated.
var isDeprecated: Bool = false

/// An access modifier.
var access: AccessModifier?

Expand Down Expand Up @@ -103,6 +106,9 @@ struct PropertyBlueprint {
/// A documentation comment for the property.
var comment: Comment? = nil

/// Whether the type should be annotated as deprecated.
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
var isDeprecated: Bool = false

/// The original name of the property specified in the OpenAPI document.
var originalName: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extension TypesFileTranslator {
associatedDeclarations = []
}
return .init(
isDeprecated: parameter.parameter.deprecated,
originalName: parameter.name,
typeUsage: parameter.typeUsage,
associatedDeclarations: associatedDeclarations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extension TypesFileTranslator {
let function = FunctionDescription(signature: signature)
return .commentable(
operationComment,
.function(function)
.function(function).deprecate(if: description.operation.deprecated)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ extension TypesFileTranslator {
]
)
)
.deprecate(if: operation.endpoint.operation.deprecated)
)
return operationEnumDecl
}
Expand Down
13 changes: 13 additions & 0 deletions Tests/OpenAPIGeneratorReferenceTests/Resources/Docs/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ paths:
- name: My-Request-UUID
in: header
description: Request identifier
deprecated: true
schema:
format: uuid
type: string
Expand Down Expand Up @@ -116,6 +117,7 @@ paths:
/probe/:
post:
operationId: probe
deprecated: true
responses:
'204':
description: Ack
Expand Down Expand Up @@ -370,6 +372,17 @@ components:
- $ref: '#/components/schemas/MessagedExercise'
discriminator:
propertyName: kind
DeprecatedObject:
deprecated: true
type: object
properties: {}
additionalProperties: false
czechboy0 marked this conversation as resolved.
Show resolved Hide resolved
ObjectWithDeprecatedProperty:
type: object
properties:
message:
type: string
deprecated: true
responses:
ErrorBadRequest:
description: Bad request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public protocol APIProtocol: Sendable {
func createPet(_ input: Operations.createPet.Input) async throws -> Operations.createPet.Output
/// - Remark: HTTP `POST /probe/`.
/// - Remark: Generated from `#/paths//probe//post(probe)`.
func probe(_ input: Operations.probe.Input) async throws -> Operations.probe.Output
@available(*, deprecated) func probe(_ input: Operations.probe.Input) async throws
-> Operations.probe.Output
/// Update just a specific property of an existing pet. Nothing is updated if no request body is provided.
///
/// - Remark: HTTP `PATCH /pets/{petId}`.
Expand Down Expand Up @@ -577,6 +578,26 @@ public enum Components {
}
}
}
/// - Remark: Generated from `#/components/schemas/DeprecatedObject`.
@available(*, deprecated)
public struct DeprecatedObject: Codable, Equatable, Hashable, Sendable {
/// Creates a new `DeprecatedObject`.
public init() {}
public init(from decoder: Decoder) throws {
try decoder.ensureNoAdditionalProperties(knownKeys: [])
}
}
/// - Remark: Generated from `#/components/schemas/ObjectWithDeprecatedProperty`.
public struct ObjectWithDeprecatedProperty: Codable, Equatable, Hashable, Sendable {
/// - Remark: Generated from `#/components/schemas/ObjectWithDeprecatedProperty/message`.
@available(*, deprecated) public var message: Swift.String?
/// Creates a new `ObjectWithDeprecatedProperty`.
///
/// - Parameters:
/// - message:
public init(message: Swift.String? = nil) { self.message = message }
public enum CodingKeys: String, CodingKey { case message }
}
}
/// Types generated from the `#/components/parameters` section of the OpenAPI document.
public enum Parameters {
Expand Down Expand Up @@ -779,7 +800,7 @@ public enum Operations {
}
public var query: Operations.listPets.Input.Query
public struct Headers: Sendable, Equatable, Hashable {
public var My_Request_UUID: Swift.String?
@available(*, deprecated) public var My_Request_UUID: Swift.String?
/// Creates a new `Headers`.
///
/// - Parameters:
Expand Down Expand Up @@ -1006,7 +1027,7 @@ public enum Operations {
}
/// - Remark: HTTP `POST /probe/`.
/// - Remark: Generated from `#/paths//probe//post(probe)`.
public enum probe {
@available(*, deprecated) public enum probe {
public static let id: String = "probe"
public struct Input: Sendable, Equatable, Hashable {
public struct Path: Sendable, Equatable, Hashable {
Expand Down