Skip to content

Support for conditionally dereferencing with getReference/findReference class methods #837

Open
@huypham50

Description

My current logic looks like this

const oasNormalizer = new OASNormalize(url)
const validatedSchema = await oasNormalizer.validate()
const schema = new Oas(validatedSchema);
await schema.dereference()

// skipping some traverse logic

let reqBodySchema: MediaTypeObject['schema'] = undefined
if (reqBodySchema) {
  if ('type' in reqBodySchema) {
    console.log('type:', reqBodySchema.type)
  } else if ('$ref' in reqBodySchema) { // we should not need to do this because the schema has been dereferenced
    console.log('ref:', reqBodySchema.$ref)
  }
}

At this point, since the schema has been dereferenced, should we narrow the types so we don't have to do type narrowing? Something like this:

// dereferencedSchema returns a narrower type of schema that removes RefObject from `X | RefObject`
const dereferencedSchema = await schema.dereference()

Also, is there a way conditionally dereference schema? Something like this:

if ('$ref' in reqBodySchema) {
  const ref = reqBodySchema.$ref // ref is a string
  const refSchema1 = schema.getReference(ref) // return schema or throw error
  const refSchema2 = schema.findReference(ref) // return schema or undefined
}

Let me know if I am doing anything that is not standard. New to this (great) library. Also recommendations for great Typescript libraries for the OpenAPI / JSONSchema ecosystem would be greatly appreciated!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions