Gi60s / openapi-enforcer

Apache License 2.0
94 stars 23 forks source link

Example with discriminator results in "Cannot read property 'staticData' of undefined" #85

Closed phillim1 closed 4 years ago

phillim1 commented 4 years ago

Using openapi-enforcer @ 1.10.5 with the below file gives a warning as follows:

"WARNED - API schema warnings found by openapi-enforcer for swagger.162457205.json [ EnforcerException: One or more warnings exist in the OpenApi definition at: components > schemas > Pets > example Invalid value Discriminator property "petType" as "Cat" did not map to a schema ]"

If I then add mapping as follows

        mapping:
          Dog: '#/components/schemas/Dog'
          Cat: '#/components/schemas/Cat'

then I receive a failure as follows: _"FAILED - API schema errors found by openapi-enforcer for swagger.163231602.json [ EnforcerException: One or more errors exist in the OpenApi definition at: components > schemas Unexpected error encountered: TypeError: Cannot read property 'staticData' of undefined at runDeserialize (<...>/node_modules/openapi-enforcer/src/schema/deserialize.js:110:47) at runDeserialize (<...>/node_modules/openapi-enforcer/src/schema/deserialize.js:65:43) at Schema.deserialize (<...>/node_modules/openapi-enforcer/src/enforcers/Schema.js:52:24) at deserializeAndValidate (<...>/node_modules/openapi-enforcer/src/enforcers/Schema.js:789:31) at Schema.init (<...>/node_modules/openapi-enforcer/src/enforcers/Schema.js:230:27) at build (<...>/node_modules/openapi-enforcer/src/super.js:162:46) at new Schema (eval at createConstructor (<...>/node_modules/openapi-enforcer/src/super.js:37:15)- at runChildValidator (<...>/node_modules/openapi-enforcer/src/definition-validator.js:378:20) at <...>/nodemodules/openapi-enforcer/src/definition-validator.js:136:44 at Array.forEach () ]"

Please can you confirm whether this is an issue with openapi-enforcer or the syntax of my file.

openapi: 3.0.0
info:
  title: ''
  version: ''
paths:
  /pets:
    get:
      responses:
        '200':
          description: The list of pets retrieved.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pets'
components:
  schemas:
    Pets:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: petType

      example:
        petType: Cat
        birthDate: '2020-09-09'
        huntingSkill: fast
    Cat:
      type: object
      additionalProperties: false
      required:
        - petType
      properties:
        petType:
          type: string
        birthDate:
          type: string
        huntingSkill:
          type: string
    Dog:
      type: object
      additionalProperties: false
      required:
        - petType
      properties:
        petType:
          type: string
        birthDate:
          type: string
          format: date
        packSize:
          type: integer
          minimum: 1
Gi60s commented 4 years ago

At first glance I think you're doing this correctly. I'll dig into it.

Gi60s commented 4 years ago

It was a bug. The problem came down to the library attempting to validate your example before fully resolving your discriminator references.

Thanks for the issue. It's now fixed and published to NPM as version 1.10.8.

jobykoshy commented 3 years ago

Getting issues from almost similar OAS3 document from openapi-enforcer @ 1.11.2 Unable to deserialize value' ' at: tradeLegs' ' at: 0' ' Unable to determine deserialization schema beca use too many schemas match. Use of a discriminator or making your schemas more specific would help this problem.' ' at: 1' ' Unable to determine deserialization schema beca use too many schemas match. Use of a discriminator or making your schemas more specific would help this problem.'

The OAS3 schema is ,

"TradeDetails": {
                                  "type": "object",
                                  "title": "TradeDetails",
                                  "description": "Detailed version of the trade data.",
                                  "properties": {
                                    "tradeId": {
                                      "type": "string",
                                      "description": "The reference identifier of the trade.",
                                      "example": "500560B",
                                      "maxLength": 256
                                    },
                                    "tradeType": {
                                      "type": "string",
                                      "example": "SWAP",
                                      "description": "Financial type of the deal.",
                                      "maxLength": 50
                                    },
                                    "tradeStatus": {
                                      "type": "string",
                                      "example": "PRE-TRADE",
                                      "description": "Status of the deal such as verified, done, pre-trade, pre-deal.",
                                      "maxLength": 50
                                    },
                                    "legalEntityId": {
                                      "type": "string",
                                      "description": "Identifier of the legal entity, institution/business that owns the transactions.",
                                      "example": "0010107588B",
                                      "maxLength": 256
                                    },
                                    "anonymizedCptyId": {
                                      "type": "string",
                                      "description": "Anonymized Identifier of the counterparty with which the trade is executed.",
                                      "example": "0010107588B",
                                      "maxLength": 256
                                    },
                                    "collateralCurrency": {
                                      "type": "string",
                                      "example": "EUR",
                                      "description": "Collateral currency in format ISO 4217.",
                                      "pattern": "[A-Z]{3,3}"
                                    },
                                    "csaId": {
                                      "type": "string",
                                      "description": "Identifier of the counterparty CSA.",
                                      "example": "0010107588B_CSA",
                                      "maxLength": 256
                                    },
                                    "tradingBookName": {
                                      "$ref": "#/components/schemas/TradingBook"
                                    },
                                    "nettingSetId": {
                                      "type": "string",
                                      "description": "Identifier of Netting set.",
                                      "example": "Netting Set 1",
                                      "maxLength": 256
                                    },
                                    "tradeLegs": {
                                      "type": "array",
                                      "description": "The definition of trade financials by leg.",
                                      "items": {
                                        "oneOf": [
                                          {
                                            "$ref": "#/components/schemas/InterestRateInstrument"
                                          },
                                          {
                                            "$ref": "#/components/schemas/ForeignExchangeInstrument"
                                          }
                                        ],
                                        "discriminator": {
                                          "propertyName": "instrumentType"
                                        }
                                      }
                                    }
                                  }

Validating Response is,

{
    "id": "6011a1d6d80b3b60df9d8b89",
    "tradeId": "18237HM",
    "tradeType": "SWAP",
    "tradeStatus": "VER",
    "legalEntityId": "MGCCOLL",
    "anonymizedCptyId": "e00f5ba5-0d3b-4ced-bcf2-ccc29861aa52",
    "collateralCurrency": "",
    "csaId": "",
    "tradingBookName": {
        "businessUnit": "MGCCOLL",
        "desk": "MGCDESK",
        "book": "ECBOOK"
    },
    "nettingSetId": "MGCCOLLSB",
    "tradeLegs": [
        {
            "instrumentType": "InterestRateInstrument",
            "instrumentId": "000009QC",
            "currency": "USD",
            "direction": "SELL",
            "effectiveDate": "2013-01-22",
            "maturityDate": "2019-01-22",
            "notional": 51000000,
            "fixedRate": 0,
            "minRate": null,
            "maxRate": null,
            "index": "LIBOR",
            "term": "12M",
            "basis": "A360",
            "periodSchedule": [
                {
                    "periodId": null,
                    "periodStart": null,
                    "periodEnd": null,
                    "fixingDate": null,
                    "fxFixingDate": null,
                    "payDate": "2013-01-22",
                    "paymentType": "notional-exchange",
                    "notional": null,
                    "notionalChange": 51000000,
                    "rate": 0,
                    "spread": 0,
                    "periodDetails": null
                }
             ],
            "instrumentDetails": null
        },
        {
            "instrumentType": "InterestRateInstrument",
            "instrumentId": "000009QD",
            "currency": "EUR",
            "direction": "BUY",
            "effectiveDate": "2013-01-22",
            "maturityDate": "2019-01-22",
            "notional": 37488973.831226,
            "fixedRate": 0.05,
            "minRate": null,
            "maxRate": null,
            "index": "FIXED",
            "term": "",
            "basis": "30/360",
            "periodSchedule": [
                {
                    "periodId": null,
                    "periodStart": null,
                    "periodEnd": null,
                    "fixingDate": null,
                    "fxFixingDate": null,
                    "payDate": "2013-01-22",
                    "paymentType": "notional-exchange",
                    "notional": null,
                    "notionalChange": -37488973.831226,
                    "rate": 0,
                    "spread": 0,
                    "periodDetails": null
                }
            ],
            "instrumentDetails": null
        }
    ]
}

after updating with mapping

 "mapping": {
                  "InterestRateInstrument": "#/components/schemas/InterestRateInstrument",
                  "ForeignExchangeInstrument": "#/components/schemas/ForeignExchangeInstrument"
                }

it shows

TypeError: Cannot read property 'staticData' of undefined"