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

Add tests for responses with examples #166

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

simonjbeaumont
Copy link
Collaborator

@simonjbeaumont simonjbeaumont commented Aug 2, 2023

Motivation

The OpenAPI specification allows for specifying examples along side a schema in a content map1. The example can be an object with some defined fields, but none of these fields are mandatory in OpenAPI 3.0.32.

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:

  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-optional3.

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.

Footnotes

  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

@czechboy0
Copy link
Contributor

Interesting – @mattpolzin, is it intentional that the value property on Example is non-optional?

Here's a link for 3.0.0-alpha.9 and OpenAPIKit30: https://github.com/mattpolzin/OpenAPIKit/blob/3.0.0-alpha.9/Sources/OpenAPIKit30/Example.swift#L20

@mattpolzin
Copy link

mattpolzin commented Aug 2, 2023

Looks like an oversight to me. I'm a bit surprised that an example with no "example" is useful, but I'm no less beholden to the spec!

Tracked as bug here.

@simonjbeaumont simonjbeaumont marked this pull request as ready for review August 3, 2023 10:53
@simonjbeaumont simonjbeaumont enabled auto-merge (squash) August 3, 2023 10:53
@simonjbeaumont simonjbeaumont merged commit b3c2b09 into apple:main Aug 3, 2023
@czechboy0 czechboy0 added the semver/none No version bump required. label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver/none No version bump required.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants