Skip to content

Schema array type with anyOf and discriminator fails validation #402

Closed

Description

@rwrife

I'm trying to validate an OpenAPI document that contains a schema object that is of type 'array' that can be an 'anyOf' that uses a discriminator to know which object goes into the array and it's failing validation. The document appears to be a valid OAS3 document and the Swagger UI has no issues rendering it, so I'm guessing it's an issue with OpenAPI.NET.

I'm getting validation errors similar to:

Composite Schema ChildTypeA must contain property specified in the discriminator type. [#/components/schemas/SampleResponse/properties/suggestedActions/items/anyOf]
Composite schema ChildTypeA must contain property specified in the discriminator type in the required field list. [#/components/schemas/SampleResponse/properties/suggestedActions/items/anyOf]

...for this document

openapi: 3.0.0
info:
  description: Service to test discriminator
  version: 1.0.0
  title: Test Service
servers:
  - url: 'https://localhost'
paths:
  /test:
    post:
      description: Sample post of SampleRequest
      requestBody:
        description: Returns the SampleResponse with multiple types
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SampleRequest'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SampleResponse'
components:
  schemas:
    SampleRequest:
      type: object
      required:
        - id
      properties:
        id:
          type: string
          description: Random ID
          example: '1513776099991'
    BaseType:
      type: object
      required:
        - type
      properties:
        type:
          type: string
          description: Suggested object type
    ChildTypeA:
      allOf:
        - $ref: '#/components/schemas/BaseType'
          properties:
            type:
              type: string
              enum:
                - typeA
      type: object
    ChildTypeB:
      allOf:
        - $ref: '#/components/schemas/BaseType'
          properties:
            type:
              type: string
              enum:
                - typeB
      type: object
    SampleResponse:
      type: object
      properties:
        suggestedActions:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/ChildTypeA'
              - $ref: '#/components/schemas/ChildTypeB'
            discriminator:
              propertyName: type

Activity

  1. PerthCharern commented on Apr 28, 2019

    @PerthCharern
    Contributor

    I think currently that rule only looks at the properties in the schema itself without delving into the properties in the anyOf/allOf. This is a bug in our validator.

  2. ma499 commented on Feb 27, 2020

    @ma499

    Are there any plans to fix this bug? This validator is now being used by Azure API Management when deploying APIs, and we are no longer able to deploy updates to our existing API to production because of this problem.

  3. Robzilla commented on Feb 27, 2020

    @Robzilla

    Yes reported last November. Pretty big impact for me too. Apparently a fix is rolling out next week. See #429
    Bit scary when something this big stops deployments for a few months.

  4. fabich commented on Apr 15, 2020

    @fabich

    PerthCharern any updates on this?

  5. added this to the Backlog milestone on Apr 26, 2020
  6. ma499 commented on May 13, 2020

    @ma499

    Is this actually a bug? I just read the relevant section of the latest Open API 3.0.3 specification which indicates that a discriminator field is actually required to be a 'required' field.

    To support polymorphism, the OpenAPI Specification adds the discriminator field. When used, the discriminator will be the name of the property that decides which schema definition validates the structure of the model. As such, the discriminator field MUST be a required field. There are two ways to define the value of a discriminator for an inheriting instance.

    This is not new, the same is specified for Open API 2.0.

    Therefore, having read the specification, and thinking about the semantics of a discriminator field, I would argue that OpenAPI.NET's behaviour is actually correct and the official OpenAPI/Swagger parsers are the ones that actually have the bug as they are not strict enough to identify this problem.

    I realise I hadn't understood the issue properly and this is indeed a bug in the parser that it is not traversing the anyOf link to look in the child schemas for the required discriminator field.

  7. himanigulati commented on Jan 14, 2021

    @himanigulati

    Is this issue still Open? We are dealing with the similar problem.

  8. amrith92 commented on Jan 29, 2021

    @amrith92

    himanigulati Yes, I think this is still open.

  9. darrelmiller commented on Jan 30, 2021

    @darrelmiller
    Member

    Ok, I must be doing something wrong. I'm trying the above sample in the OpenAPIWorkbench tool from the master branch and I am not seeing the error...
    image

  10. darrelmiller commented on Mar 15, 2021

    @darrelmiller
    Member

    I verified this again and I am not seeing any errors. Please re-open if you find this is still a problem.

  11. cnwta3 commented on Aug 18, 2021

    @cnwta3

    I can still reproduce the error with the following reproducer specification.

    openapi: 3.0.0
    info:
      description: Service to test discriminator
      version: 1.0.0
      title: Test Service
    servers:
      - url: 'https://localhost'
    paths:
      /test:
        post:
          description: Sample post
          requestBody:
            required: true
            content:
              application/json:
                schema:
                  $ref: '#/components/schemas/Parent'
          responses:
            '200':
              description: OK
              content:
                application/json:
                  schema:
                    $ref: '#/components/schemas/Parent'
    components:
      schemas:
        Parent:
          type: object
          oneOf:
             - $ref: '#/components/schemas/ChildA'
             - $ref: '#/components/schemas/ChildB'
          discriminator:
             propertyName: type
        ChildA:
          properties:
            type:
              type: string
          type: object
        ChildB:
          properties:
            type:
             type: string
          type: object
    

    I get the following error:

    "Schema Parent must contain property specified in the discriminator type in the required field list."

    The specification validates correctly in Swagger Editor.

  12. 2 remaining items

  13. added
    priority:p2Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days
    on Apr 26, 2022
  14. danehorak-callaway commented on Jun 17, 2022

    @danehorak-callaway

    Can we get an update on the status of this fix? When can we expect a fix to be released?

  15. added
    priority:p1High priority but not blocking. Causes major but not critical loss of functionality SLA <=7days
    and removed
    priority:p2Medium. Generally has a work-around and a smaller sub-set of customers is affected. SLA <=30 days
    on Jun 28, 2022
  16. baywet commented on Jun 28, 2022

    @baywet
    Member

    Hi everyone,
    We just hit that issue recently with our internal tooling. After a quick chat with Darrel (@darrelmiller) here is the course of actions.

    This code block needs improvements:

    When the validation rule doesn't find the discriminator field in the current schema's required fields, it should recursively look into the anyOf/oneOf/allOf schemas (if any) before returning an error.

    Ping Carol Kigoonya (@CarolKigoonya) to bump the priority of this one given the current impact.

    Additionally for implementers, #383 provides additional testing cases.

  17. julealgon commented on Oct 1, 2025

    @julealgon

    I see this was apparently fixed several years ago, but I'm seeing the exact same issue today while attempting to import an OpenApi spec into Azure API Management:

    Parsing error(s): Schema KpiDataResponse must contain property specified in the discriminator $type in the required field list. [#/components/schemas/KpiDataResponse/discriminator]

    Image

    This just happened as I switched our OpenApi generation in the API from Swashbuckle's library, to latest Microsoft.AspNetCore.OpenApi.

    In our case, this is due to an inheritance chain that generates this section:

          "KpiDataResponse": {
            "type": "object",
            "anyOf": [
              {
                "$ref": "#/components/schemas/KpiDataResponseGetWarrantyPayRevenueV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetVehicleMileageAgeV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetUsedDealVolumeV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetUsedDealsByCustomerSegmentV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTriggersMarketingV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTradeInDealsV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTotalRepairOrderRevenueV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTotalGrossProfitsUsedDealsV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTotalGrossProfitsNewDealsV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTimeToTradeInResaleV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTimeToServiceResponseV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTimeToReturnServiceV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetTimeToFirstServiceV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetThreeYearServiceLifetimeVisitsV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetThreeYearServiceLifetimeSpendV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetServiceTriggerAppointmentConversionV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetServicesCompletedByCategoryV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetServiceRetentionV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetServiceOpportunitiesInPositiveEquityV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetServiceEmailCollectionV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetSalesToServiceSchedulingV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetSalesResponseV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetSalesEmailCollectionV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetRevenueOfServiceV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetRepairOrderVolumeV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetRepairOrderRevenueByVehicleAgeV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetRepairOrderLaborHoursV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetRepairOrderAppointmentsBySourceV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetPurchaseToServiceLoyaltyV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetPrintBudgetV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetPrintBudgetsV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetNumberOfCarsSoldV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetNewDealVolumeV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetNewDealsByCustomerSegmentV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetMonthsToRepurchaseV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetMarketingCustomersV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetLaneCheckInsV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetFiveYearRepurchaseRateV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetDailyMilesDrivenV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetCustomerPayRevenueV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetCustomerHealthV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetCapacityUtilizationV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetCampaignStatusV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetCampaignDrilldownV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetApprovedUpsellRevenueV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetAppointmentEmailCollectionV1ProcedureCallResponse"
              },
              {
                "$ref": "#/components/schemas/KpiDataResponseGetActiveVehiclesByMakeModelYearV1ProcedureCallResponse"
              }
            ],
            "discriminator": {
              "propertyName": "$type"
            }
          },

    Can someone clarify whether or not this is a bug on the parsing side (on Azure API Management), or a bug in the generation side?

    This is currently making it impossible for us to update our API in APIM.

  18. baywet commented on Oct 1, 2025

    @baywet
    Member

    If the type property is defined on all the anyOf entries somehow, it's a bug on the parsing side.
    If it's not defined on all the anyOf entries, it's a bug on the generation side.

  19. julealgon commented on Oct 1, 2025

    @julealgon

    If the type property is defined on all the anyOf entries somehow, it's a bug on the parsing side. If it's not defined on all the anyOf entries, it's a bug on the generation side.

    Vincent Biret (@baywet) that $type property does not exist in any of the actual .NET models (not in the abstract base, and not in any of the concrete ones). It was automatically generated by Microsoft.AspNetCore.OpenApi to model the hierarchy as a set of anyOf entries.

    I assume I should go ahead and open a Bug on the library then?

  20. baywet commented on Oct 1, 2025

    @baywet
    Member
  21. julealgon commented on Oct 2, 2025

    @julealgon

    Thanks Vincent Biret (@baywet) . While creating a smaller repro to post the issue on the aspnetcore repo, I realized that the reason we were getting that structure was due to a custom DefaultJsonTypeInfoResolver implementation, and that when removing that customization, we get a valid document again.

    I'll look into that customization on our side more closely before opening the issue as it may very well just be an oversight on our side and not on the library side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority:p1High priority but not blocking. Causes major but not critical loss of functionality SLA <=7daystype:bugA broken experience

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions