Skip to content

Unable to encode multiple OpenAPIObjectContainer values into a single anyOf #808

@schweizerm

Description

@schweizerm

Description

When encoding of one of my Entities, I always end up in an error. In the end I always end up here (with value being the TEST_ENUM of type ConfigurationType.Value1Payload):

    public func encodeToSingleValueContainer<T: Encodable>(_ value: T) throws {
        var container = singleValueContainer()
        try container.encode(value) // Thread 1: EXC_BREAKPOINT
    }

I noticed that this only happens when I use the combination of two attributes which both come down to some anyOf structure. This is just a minimal running example, my real code has way more attributes and it's only those two interfering.
In my code example below, if I either remove the configurationType OR the detailsType from my input json (or if I set one of their values to null), the code just runs fine.

Not sure if this is a bug or if I'm doing something wrong, but since the decoding works as expected, I think it's an undesired behaviour?

Reproduction

#openapi.yaml
openapi: 3.0.1
info:
  title: API Reference
  version: "1.0"
paths:
  /whatever/{subActionUuid}:
    put:
      operationId: updateSubAction
      parameters:
        - name: subActionUuid
          in: path
          required: true
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SubAction'
        required: true
      responses:
        default:
          description: SubAction
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SubAction'
components:
  schemas:
    SubAction:
      type: object
      properties:
        uuid:
          type: string
        subActionDetails:
          $ref: '#/components/schemas/SubActionDetails'
        configurationType:
          $ref: '#/components/schemas/ConfigurationType'
    SubActionDetails:
      required:
        - detailsType
      anyOf:
        - oneOf:
            - $ref: '#/components/schemas/EmptySubActionDetails'
          discriminator:
            propertyName: detailsType
            mapping:
              EMPTY: '#/components/schemas/EmptySubActionDetails'
        - type: object
    EmptySubActionDetails:
      type: object
      allOf:
        - $ref: '#/components/schemas/SubActionDetailsSchema'
    SubActionDetailsSchema:
      type: object
      properties:
        uuid:
          type: string
        detailsType:
          type: string
      required:
        - type
    ConfigurationType:
      anyOf:
        - type: string
          enum:
            - TEST_ENUM
        - type: string
      x-enumAsRef: false
#openapi-generator-config.yml
generate:
  - types
  - client

accessModifier: public
import Foundation

// note: setting configurationType or detailsType to null will prevent that the error from being thrown.
let input = """
    { 
      "uuid": "uuid",
      "configurationType": "TEST_ENUM",
      "subActionDetails": {
         "uuid": "uuid",
         "detailsType":"EMPTY"
      }
    }
    """


let decoded = try! JSONDecoder().decode(Components.Schemas.SubAction.self, from: input.data(using: .utf8)!)
let encoded = try! JSONEncoder().encode(decoded) # fails here 
let result = String(data: encoded, encoding: .utf8)
print(result!)

Package version(s)

openapi-generator: 1.10.2
openapi-runtime: 1.8.2
openapi-urlsession: 1.10

Expected behavior

The encoding should not throw any error, even when both attributes are set.

Environment

swift: 6.1.2

Additional information

I'm bad with swift debugging, I couldn't see any helpful output from the stack trace, but if you want me to, I can provide it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/runtimeAffects: the runtime library.kind/bugFeature doesn't work as expected.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions