Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Intention behind properties on the same level with anyOf #805

Unanswered
kean asked this question in Q&A
Discussion options

Hi,

I'm working on an OpenAPI code generator and I'm using GitHub's spec as a primary example. I've covered all of the paths and schemas except for the following construct where properties and anyOf are used on the same level (see operationId: gists/update):

requestBody:
  required: true
  content:
    application/json:
      schema:
        properties:
          description:
            description: Description of the gist
            example: Example Ruby script
            type: string
          files:
            description: Names of files to be updated
            example:
              hello.rb:
                content: blah
                filename: goodbye.rb
            type: object
            additionalProperties:
              type: object
              nullable: true
              properties:
                content:
                  description: The new content of the file
                  type: string
                filename:
                  description: The new filename for the file
                  type: string
                  nullable: true
              anyOf:
              - required:
                - content
              - required:
                - filename
              - type: object
                maxProperties: 0
        anyOf:
        - required:
          - description
        - required:
          - files
        type: object
        nullable: true

If I put this schema in Swagger Editor, it fails to produce a comprehensible result.

Screen Shot 2021-12-18 at 4 13 57 PM

I'm not sure if that construct is allowed in OpenAPI. At least I couldn't find any examples of this outside of GitHub spec.

If you take a step back and think about the intention behind this schema, isn't it the same thing as the following:

requestBody:
  required: true
  content:
    application/json:
      schema:
        type: object
        properties:
          description:
            description: Description of the gist
            example: Example Ruby script
            type: string
            nullable: true
          files:
            description: Names of files to be updated
            example:
              hello.rb:
                content: blah
                filename: goodbye.rb
            type: object
            nullable: true
            additionalProperties:
              type: object
              properties:
                content:
                  description: The new content of the file
                  type: string
                filename:
                  description: The new filename for the file
                  type: string
              required:
                - content

It's a much simpler definition and this is what the code generator produces for me with the update schema:

public struct PatchRequest: Encodable {
    /// Description of the gist
    ///
    /// Example: "Example Ruby script"
    public var description: String?
    /// Names of files to be updated
    ///
    /// Example:
    ///
    /// {
    ///   "hello.rb" : {
    ///     "content" : "blah",
    ///     "filename" : "goodbye.rb"
    ///   }
    /// }
    public var files: [String: File]?

    public struct File: Encodable {
        /// The new content of the file
        public var content: String
        /// The new filename for the file
        public var filename: String?

        public init(content: String, filename: String? = nil) {
            self.content = content
            self.filename = filename
        }
    }

    public init(description: String? = nil, files: [String: File]? = nil) {
        self.description = description
        self.files = files
    }
}
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.