openapi-to-postman icon indicating copy to clipboard operation
openapi-to-postman copied to clipboard

resolveRefs - AnyOf limited to 1 item

Open thim81 opened this issue 3 years ago • 3 comments

Hi,

I wonder why do you limit the schema building to limited to 1 type for anyOf[0] and OneOf[0]?

if (schema.anyOf) {
      return this.resolveRefs(schema.anyOf[0], parameterSourceOption, components, schemaResolutionCache, resolveFor,
        resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
    }
    if (schema.oneOf) {
      return this.resolveRefs(schema.oneOf[0], parameterSourceOption, components, schemaResolutionCache, resolveFor,
        resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
    }
    if (schema.allOf) {
      return this.resolveAllOf(schema.allOf, parameterSourceOption, components, schemaResolutionCache, resolveFor,
        resolveTo, stack, _.cloneDeep(seenRef), stackLimit);
    }

In case you have AnyOf field defined in OpenApi:

    CustomField:
      type: object
      required:
        - id
      additionalProperties: false
      properties:
        id:
          type: string
          example: 123456-ABC
        value:
          anyOf:
            - type: string
              example: Uses Postman and OpenApi
              nullable: true
            - type: number
              example: 10
              nullable: true
            - type: boolean
              example: true
              nullable: true
            - type: array
              items:
              type: string

Now only the type string is kept which results in a JSON that only validates for

{"custom_fields":{"type":"array","items":{"type":"object","required":["id"],"additionalProperties":false,"properties":{"id":{"type":"string","example":"custom_technologies"},"value":{"type":["string","null"],"example":"Uses Postman and OpenApi"}}}

My use-case is to re-use the schema for schema validation in an automatic pm.test generated based on OpenAPI. I want to understand better why only the 1st item is taken, so that potentially I can provide a PR.

thim81 avatar May 07 '21 21:05 thim81

@thim81 This module was created originally to convert OpenAPI spec into the collection, and while doing so schema was used only for generating data/value out of it. Meaning resolving oneOf and anyOf keywords means any value among multiple schemas would serve its purpose.

We already have a similar issue here and we are working on making changes to deref. resolveRefs() such that it can handle these keywords properly.

VShingala avatar May 11 '21 05:05 VShingala

@VShingala

I fully understand the initial purpose of the module. It has become an important tool in the OpenApi & Postman communities, so it makes sense to extend the use-cases, like I do with schema validation.

I have created a PR to bring support for resolving oneOf and anyOf https://github.com/postmanlabs/openapi-to-postman/pull/369 which does the trick for generation a valid schema. Perhaps this could be the 1st step towards bringing more support for resolving oneOf and anyOf?

thim81 avatar May 11 '21 10:05 thim81

@thim81 Yes I understand it. Although we will need more changes as in some of the code, we are sometimes using resolved JSON schema expecting there are no anyOf or oneOf keywords present if that makes sense.

I would be adding more changes to your contribution pretty soon. Thanks for the contribution..!

VShingala avatar May 11 '21 15:05 VShingala

The fix is now released (more info - https://github.com/postmanlabs/postman-app-support/issues/9616). @thim81 feel free to reopen this ticket if you are still facing this issue.

akshaydeo avatar Jul 03 '23 08:07 akshaydeo