Gi60s / openapi-enforcer

Apache License 2.0
94 stars 23 forks source link

Schema with discriminator results in "Cannot read property 'staticData' of undefined" #113

Closed jobykoshy closed 3 years ago

jobykoshy commented 3 years ago

Getting issues in OAS3 document from openapi-enforcer @ 1.11.2

Unable to deserialize value
    at: tradeLegs
      at: 0
        Unable to determine deserialization schema because 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 because 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"

Gi60s commented 3 years ago

Hello, @jobykoshy. Thank you for the issue. I'm trying to replicate your error, but I don't think I have enough information. Could you simplify the examples so that they only contain the pertinent data. For example, this looks like it comes from the tradeLegs property so we probably don't need any of the other properties. Also, you did not include the schemas for InterestRateInstrument and ForeignExchangeInstrument that I'll also need to see to help troubleshoot this.

jobykoshy commented 3 years ago
         "tradeLegs": {
            "type": "array",
            "description": "The definition of trade financials by leg.",
            "items": {
              "oneOf": [
                {
                  "$ref": "#/components/schemas/InterestRateInstrument"
                },
                {
                  "$ref": "#/components/schemas/ForeignExchangeInstrument"
                }
              ],
              "discriminator": {
                "propertyName": "instrumentType",
                "mapping": {
                  "InterestRateInstrument": "#/components/schemas/InterestRateInstrument",
                  "ForeignExchangeInstrument": "#/components/schemas/ForeignExchangeInstrument"
                }
              }
            }
          }

      "InterestRateInstrument": {
        "type": "object",
        "title": "InterestRateInstrument",
        "description": "The financial description of fixed or floating Interest Rate instrument.",
        "properties": {
          "instrumentType": {
            "type": "string",
            "enum": [
              "InterestRateInstrument"
            ]
          },
          "instrumentId": {
            "type": "string",
            "example": "1233344",
            "description": "Identifier of trade financial instrument",
            "maxLength": 50
          },
          "currency": {
            "type": "string",
            "example": "EUR",
            "description": "Trade currency in format of ISO 4217.",
            "pattern": "[A-Z]{3,3}"
          },
          "crossCurrency": {
            "type": "string",
            "example": "USD",
            "description": "Cross/non-deliverable currency in format of ISO 4217.",
            "pattern": "[A-Z]{3,3}"
          }
        }
      },
      "ForeignExchangeInstrument": {
        "type": "object",
        "title": "ForeignExchangeInstrument",
        "description": "The financial description of foreign exchange instrument.",
        "properties": {
          "instrumentType": {
            "type": "string",
            "enum": [
              "ForeignExchangeInstrument"
            ]
          },
          "instrumentId": {
            "type": "string",
            "example": "1233366",
            "description": "Identifier of trade financial instrument.",
            "maxLength": 50
          },
          "currency1": {
            "type": "string",
            "example": "EUR",
            "description": "Base (main) currency in format of ISO 4217.",
            "pattern": "[A-Z]{3,3}"
          },
          "currency2": {
            "type": "string",
            "example": "EUR",
            "description": "Money currency in format of ISO 4217.",
            "pattern": "[A-Z]{3,3}"
          },
          "splitCurrency": {
            "type": "string",
            "example": "EUR",
            "description": "Split currency in format of ISO 4217.",
            "pattern": "[A-Z]{3,3}"
          }
        }
      }

Please see the updated sample. Hope this would help you to replicate the error.

Gi60s commented 3 years ago

I am unable to replicate the error using enforcer version 1.13.3. Have you tried updating?

Gi60s commented 3 years ago

Closing due to inactivity.