function getRequestSchemas()

in functions/readonly-in-response-schema.js [13:82]


function getRequestSchemas(oasDoc) {
  /* eslint-disable object-curly-newline,object-curly-spacing */
  const getOps = ({put, post, patch}) => [put, post, patch];
  const topLevelRequestSchemas = Object.values(oasDoc.paths || {})
    .flatMap(getOps).filter(Boolean)
    .flatMap(({parameters}) => parameters?.filter(({in: location}) => location === 'body') || [])
    .flatMap(({schema}) => (schema ? [schema] : []))
    .filter(({$ref}) => $ref && $ref.match(/^#\/definitions\//))
    .map(({$ref}) => $ref.replace(/^#\/definitions\//, ''));
  /* eslint-enable object-curly-newline,object-curly-spacing */

  requestSchemas = new Set();

  // Now that we have the top-level response schemas, we need to find all the
  // schemas that are referenced by those schemas. We do this by iterating
  // over the schemas until we find no new schemas to add to the set.
  const schemasToProcess = [...topLevelRequestSchemas];
  while (schemasToProcess.length > 0) {
    const schemaName = schemasToProcess.pop();
    requestSchemas.add(schemaName);
    const schema = oasDoc.definitions[schemaName];
    if (schema) {
      if (schema.properties) {
        // eslint-disable-next-line no-restricted-syntax
        for (const property of Object.values(schema.properties)) {
          if (property.$ref && property.$ref.match(/^#\/definitions\//)) {
            const ref = property.$ref.replace(/^#\/definitions\//, '');
            if (!requestSchemas.has(ref) && !schemasToProcess.includes(ref)) {
              schemasToProcess.push(ref);
            }
          }
          if (property.items && property.items.$ref && property.items.$ref.match(/^#\/definitions\//)) {
            const ref = property.items.$ref.replace(/^#\/definitions\//, '');
            if (!requestSchemas.has(ref) && !schemasToProcess.includes(ref)) {
              schemasToProcess.push(ref);
            }
          }
          if (property.additionalProperties && property.additionalProperties.$ref && property.additionalProperties.$ref.match(/^#\/definitions\//)) {
            const ref = property.additionalProperties.$ref.replace(/^#\/definitions\//, '');
            if (!requestSchemas.has(ref) && !schemasToProcess.includes(ref)) {
              schemasToProcess.push(ref);
            }
          }
        }
      }
      if (schema.allOf) {
        // eslint-disable-next-line no-restricted-syntax
        for (const element of schema.allOf) {
          if (element.$ref && element.$ref.match(/^#\/definitions\//)) {
            const ref = element.$ref.replace(/^#\/definitions\//, '');
            if (!requestSchemas.has(ref) && !schemasToProcess.includes(ref)) {
              schemasToProcess.push(ref);
            }
          }
        }
      }
      if (schema.discriminator) {
        // Check all the schemas in the document and add any that "allOf" this schema
        // into schemasToProcess
        const schemaRef = `#/definitions/${schemaName}`;
        // eslint-disable-next-line no-restricted-syntax
        for (const [key, value] of Object.entries(oasDoc.definitions)) {
          if (value.allOf?.some((elem) => elem.$ref === schemaRef)) {
            schemasToProcess.push(key);
          }
        }
      }
    }
  }
}