Skip to content

Commit

Permalink
Merge pull request #114 from mattpolzin/dont-fail-on-operation-callbacks
Browse files Browse the repository at this point in the history
Ignore operation callbacks instead of failing to decode.
  • Loading branch information
mattpolzin authored Aug 1, 2020
2 parents 05258e4 + d5a3456 commit e0b5e64
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/OpenAPIKit/Operation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ extension OpenAPI.Operation {
case parameters
case requestBody
case responses
// case callbacks
case callbacks
case deprecated
case security
case servers
Expand All @@ -276,7 +276,7 @@ extension OpenAPI.Operation {
.parameters,
.requestBody,
.responses,
// .callbacks,
.callbacks,
.deprecated,
.security,
.servers
Expand Down Expand Up @@ -305,6 +305,8 @@ extension OpenAPI.Operation {
self = .requestBody
case "responses":
self = .responses
case "callbacks":
self = .callbacks
case "deprecated":
self = .deprecated
case "security":
Expand Down Expand Up @@ -334,6 +336,8 @@ extension OpenAPI.Operation {
return "requestBody"
case .responses:
return "responses"
case .callbacks:
return "callbacks"
case .deprecated:
return "deprecated"
case .security:
Expand Down
76 changes: 76 additions & 0 deletions Tests/OpenAPIKitCompatibilitySuite/SwaggerDocSamplesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,82 @@ components:
throw friendlyError
}
}

func test_callbackExample() throws {
let docString = commonBaseDocument + """
paths:
/streams:
post:
description: subscribes a client to receive out-of-band data
parameters:
- name: callbackUrl
in: query
required: true
description: |
the location where data will be sent. Must be network accessible
by the source server
schema:
type: string
format: uri
example: https://tonys-server.com
responses:
'201':
description: subscription successfully created
content:
application/json:
schema:
description: subscription information
required:
- subscriptionId
properties:
subscriptionId:
description: this unique identifier allows management of the subscription
type: string
example: 2531329f-fb09-4ef7-887e-84e648214436
callbacks:
# the name `onData` is a convenience locator
onData:
# when data is sent, it will be sent to the `callbackUrl` provided
# when making the subscription PLUS the suffix `/data`
'{$request.query.callbackUrl}/data':
post:
requestBody:
description: subscription payload
content:
application/json:
schema:
type: object
properties:
timestamp:
type: string
format: date-time
userData:
type: string
responses:
'202':
description: |
Your server implementation should return this HTTP status code
if the data was received successfully
'204':
description: |
Your server should return this HTTP status code if no longer interested
in further updates
"""

// test decoding
do {
let doc = try YAMLDecoder().decode(OpenAPI.Document.self, from: docString)

// test validating
try doc.validate()

// test dereferencing and resolving
_ = try doc.locallyDereferenced().resolved()
} catch let error {
let friendlyError = OpenAPI.Error(from: error)
throw friendlyError
}
}
}

fileprivate let commonBaseDocument = """
Expand Down
24 changes: 24 additions & 0 deletions Tests/OpenAPIKitTests/Operation/OperationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,30 @@ extension OperationTests {
XCTAssertEqual(operation.responses[200]?.reference, .component(named: "test"))
}

func test_doesNotFailDecodingCallbacks() {
let operationData =
"""
{
"responses" : {},
"callbacks" : {
"callback" : {
"{$request.query.queryUrl}" : {
"post" : {
"responses" : {
"200" : {
"description" : "callback successfully processed"
}
}
}
}
}
}
}
""".data(using: .utf8)!

XCTAssertNoThrow(try orderUnstableDecode(OpenAPI.Operation.self, from: operationData))
}

// Note that JSONEncoder for Linux Foundation does not respect order
func test_responseOrder_encode() throws {
let operation = OpenAPI.Operation(
Expand Down

0 comments on commit e0b5e64

Please sign in to comment.