visitSchema()

in packages/extensions/core/src/lib/plugins/tree-shaker/tree-shaker.ts [342:428]


  visitSchema(targetParent: AnyObject, originalNodes: Iterable<Node>) {
    const object = "object";
    const [requiredField, theNodes] = partition([...originalNodes], (each) => each.key === "required");
    const requiredProperties = new Array<string>();
    if (requiredField[0] !== undefined) {
      requiredProperties.push(...requiredField[0].value);
    }

    for (const { value, key, pointer, children } of theNodes) {
      switch (key) {
        case "properties":
          this.visitProperties(this.newObject(targetParent, key, pointer), children, requiredProperties);
          break;

        case "additionalProperties":
          // In AutoRest V2, AdditionalProperties are not dereferenced.
          if (!this.isSimpleTreeShake && typeof value === object) {
            // it should be a schema
            this.dereference(
              "/components/schemas",
              this.schemas,
              this.visitSchema,
              targetParent,
              key,
              pointer,
              value,
              children,
            );
          } else {
            // otherwise, just copy it across.
            this.clone(targetParent, key, pointer, value);
          }
          break;

        case "allOf":
        case "anyOf":
        case "oneOf":
          if (this.isSimpleTreeShake) {
            // this is the same behavior as AutoRest V2. Inlined allOf, anyOf, oneOf are not shaken
            this.clone(targetParent, key, pointer, value);
          } else {
            const polymorphicCollection = this.newArray(targetParent, key, pointer);
            for (const { value: itemVal, children: itemChildren, pointer: itemPointer, key: itemKey } of children) {
              this.dereference(
                "/components/schemas",
                this.schemas,
                this.visitSchema,
                polymorphicCollection,
                itemKey,
                itemPointer,
                itemVal,
                itemChildren,
              );
            }
          }

          break;
        case "not":
        case "items":
          this.dereference(
            "/components/schemas",
            this.schemas,
            this.visitSchema,
            targetParent,
            key,
            pointer,
            value,
            children,
            `${this.getNameHint(pointer)}Item`,
          );
          break;

        // everything else, just copy recursively.
        default:
          if (targetParent[key] && targetParent[key] === value) {
            // properties that are already there and the same...
            break;
          }
          this.clone(targetParent, key, pointer, value);
          break;
      }
    }

    if (requiredProperties.length > 0) {
      this.clone(targetParent, requiredField[0].key, requiredField[0].pointer, requiredProperties);
    }
  }