Forum Discussion

HenrikHL's avatar
HenrikHL
Frequent Contributor
3 years ago

"Extending" objects using "allOf"

It often turns out that I need to modify examples in APIs when reusing objects defined in Domains. This I do by using the allOf: linking to the object using $ref and then modifying the example.

If I have an object called timestamp which contains a field called startDateTime with the value: 2000-01-01T00:00:00+00:00. I could modify the example by writing:

schemas:
  timestamp:
    type: object
    allOf:
      - $ref: 'https://api.swaggerhub.com/domains/.../components/schemas/timestamp'
      - type: object
        properties:
          startDateTime:
            example: 2019-11-12T07:41:00+08:30

SwaggerHub would then render the modified example-value in my timestamp - great!!

It is also possible to do the same thing when modifying the descriptions and/or adding deprecation, etc...

 

The problem comes when I want to modify an enum value. Here it seems that SwaggerHub "accumulates" all values instead of "replacing" them. So if I have a field called status (also on the timestamp object) with the enum values: STRT and END - and want to modify it by using above solution I would expect I could write:

schemas:
  timestamp:
    type: object
    allOf:
      - $ref: 'https://api.swaggerhub.com/domains/.../components/schemas/timestamp'
      - type: object
        properties:
          status:
            enum:
              - START
              - END

But what happens is that now Swaggerhub renderes 3 values: START, END and STRT (so an accumulation rather than a replacement).

Could someone from the Swagger team please verify that this is indeed the desired behavior? Or is this a bug?

If it is the desired behavior - then how do I reduce/modify an enumset in an already defined object?

 

2 Replies

  • HenrikHL's avatar
    HenrikHL
    Frequent Contributor

    Here is a screenshot example for trying to "reduce" the set without luck 😞

    In the above example I did "extend" the description and modify it to only contain valid values - but the enum is an accumulation of previous values and the ones in the red circle to the left 😞

    • Hi HenrikHL,

       

      It is not possible to override/replace enum values in JSONSchema using allOf. Composition in this case (allOf + enum) should result in an intersection, because any instance must be valid against each of the subschemas. In other words, allOf can be used to reduce a "parent" schema's enum but not override it or extend it.

       

      In your example, the enums do not have common values and this should result in an impossible schema that matches nothing. However there’s a bug in one of our Swagger-JS OSS components which is resulting in enum array merging.

       

      I’ve raised an issue for this on the Swagger-JS GitHub repo. Thanks for raising this!

       

      Cheers,

      Frank