Skip to content

Commit b3c2b09

Browse files
Add tests for responses with examples (#166)
### Motivation The OpenAPI specification allows for specifying examples along side a schema in a content map[^1]. The example can be an object with some defined fields, but none of these fields are mandatory in OpenAPI 3.0.3[^2]. However, it looks like we're failing to parse documents that don't have the `value` field. Specifically we can't parse this spec unless we add the `value` field: ```diff responses: MyResponse: description: Some response content: application/json: schema: type: string examples: application/json: summary: "a hello response" + value: "hello" ``` This is used in the wild—notably in the Github API. This appears to be because the model used in OpenAPIKit expects `value` to be non-optional[^3]. ### Modifications Add some tests, one with an `XCTExpectFailure`, to track this. ### Result We have tests that we can use to track this incompatibility with such OpenAPI documents. ### Test Plan This patch is tests only. [^1]: https://spec.openapis.org/oas/v3.0.3#mediaTypeObject [^2]: https://spec.openapis.org/oas/v3.0.3#exampleObject [^3]: https://github.com/mattpolzin/OpenAPIKit/blob/main/Sources/OpenAPIKit/Example.swift#L19 --------- Signed-off-by: Si Beaumont <beaumont@apple.com>
1 parent 3c0fcab commit b3c2b09

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Tests/OpenAPIGeneratorReferenceTests/SnippetBasedReferenceTests.swift

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,85 @@ final class SnippetBasedReferenceTests: XCTestCase {
772772
)
773773
)
774774
}
775+
776+
func testResponseWithExampleWithSummaryAndValue() throws {
777+
try self.assertResponsesTranslation(
778+
"""
779+
responses:
780+
MyResponse:
781+
description: Some response
782+
content:
783+
application/json:
784+
schema:
785+
type: string
786+
examples:
787+
application/json:
788+
summary: "a hello response"
789+
value: "hello"
790+
""",
791+
"""
792+
public enum Responses {
793+
public struct MyResponse: Sendable, Equatable, Hashable {
794+
public struct Headers: Sendable, Equatable, Hashable { public init() {} }
795+
public var headers: Components.Responses.MyResponse.Headers
796+
@frozen public enum Body: Sendable, Equatable, Hashable {
797+
case json(Swift.String)
798+
}
799+
public var body: Components.Responses.MyResponse.Body
800+
public init(
801+
headers: Components.Responses.MyResponse.Headers = .init(),
802+
body: Components.Responses.MyResponse.Body
803+
) {
804+
self.headers = headers
805+
self.body = body
806+
}
807+
}
808+
}
809+
"""
810+
)
811+
}
812+
813+
func testResponseWithExampleWithOnlyValue() throws {
814+
// This test currently throws because the parsing of ExampleObject is too strict:
815+
// https://github.com/mattpolzin/OpenAPIKit/issues/286.
816+
XCTAssertThrowsError(
817+
try self.assertResponsesTranslation(
818+
"""
819+
responses:
820+
MyResponse:
821+
description: Some response
822+
content:
823+
application/json:
824+
schema:
825+
type: string
826+
examples:
827+
application/json:
828+
summary: "a hello response"
829+
""",
830+
"""
831+
public enum Responses {
832+
public struct MyResponse: Sendable, Equatable, Hashable {
833+
public struct Headers: Sendable, Equatable, Hashable { public init() {} }
834+
public var headers: Components.Responses.MyResponse.Headers
835+
@frozen public enum Body: Sendable, Equatable, Hashable {
836+
case json(Swift.String)
837+
}
838+
public var body: Components.Responses.MyResponse.Body
839+
public init(
840+
headers: Components.Responses.MyResponse.Headers = .init(),
841+
body: Components.Responses.MyResponse.Body
842+
) {
843+
self.headers = headers
844+
self.body = body
845+
}
846+
}
847+
}
848+
"""
849+
)
850+
) { error in
851+
XCTAssert(error is DecodingError)
852+
}
853+
}
775854
}
776855

777856
extension SnippetBasedReferenceTests {

0 commit comments

Comments
 (0)