papinet / papiNet-API

papiNet is a global paper, forest products and bioproducts industry e-Business initiative.
http://www.papinet.org/
Apache License 2.0
9 stars 3 forks source link

“oneOf” for product types Paper, Pulp, etc #60

Closed larsolofsson closed 1 year ago

larsolofsson commented 1 year ago

Would the following construct be valid in a json schema to allow only for Paper or Pulp in a customer article? I think it would also be good to specify the productType that is also communicated in ListCustomerArticles.

          productType:
            type: string
            enum:
              - Paper
              - Pulp

          oneOf:
            type: object
            required: paper
              properties: 
                paper:
                   $ref: '#/components/schemas/Paper'

            type: object
            required: pulp
              properties: 
                pulp:
                   $ref: '#/components/schemas/Pulp'

Please also see attached email from Bengt.

Bengt's email, Re oneOf for Paper, Pulp etc.pdf

patricekrakow commented 1 year ago

Yes! The oneOf is working as expected, see examples below...

We don't need to have the productType, it would be a bit redundant.

Be careful that in case of fully specified seller-product or customer-article the UUID does not need to be completed with specific properties!

patricekrakow commented 1 year ago

The following example JSON schema shows how it works, it can be tested with https://www.jsonschemavalidator.net/:

{
  "$schema": "http://json-schema.org/draft-00/schema#",
  "type": "object",
  "required": [
    "name"
  ],
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "oneOf": [
    {
      "required": [ "paper"],
      "properties": {
        "paper": {
          "$ref": "#/Paper"
        }
      }
    },
    {
      "required": [ "pulp"],
      "properties": {
        "pulp": {
          "$ref": "#/Pulp"
        }
      }
    }
  ],
  "Paper": {
    "type": "object"
  },
  "Pulp": {
    "type": "object"
  }
}

It is mandatory to have at least the property "paper" or "pulp" and the definitions (types) of them are globally defined.

In the GetSellerProductById and GetCustomerArticleById schemas, I must use the above construct with oneOf and required. But, for the CheckAvailabilityOfSellerProductById and CheckAvailabilityOfCustomerArticleById schemas, I must use anyOf without the required. Unfortunately, that will allow the wrong construct of having both paper and pulp properties.

patricekrakow commented 1 year ago

See Issue #71.

patricekrakow commented 1 year ago

Fixed in https://github.com/papinet/papiNet-API/commit/7328d62b841083d679ce958e06f31df86b03162e.