private async visitResponse()

in packages/libs/oai2-to-oai3/src/converter.ts [1156:1228]


  private async visitResponse(
    responseTarget: MappingTreeObject<oai3.Response>,
    responseValue: OpenAPI2OperationResponse,
    responseName: string,
    responsesFieldMembers: () => Iterable<Node>,
    sourcePointer: string,
    produces: string[],
  ) {
    // NOTE: The previous converter patches the description of the response because
    // every response should have a description.
    // So, to match previous behavior we do too.
    if (responseValue.description === undefined || responseValue.description === "") {
      const sc = statusCodes.find((e) => {
        return e.code === responseName;
      });

      responseTarget.__set__("description", { value: sc ? sc.phrase : "", sourcePointer });
    } else {
      responseTarget.__set__("description", { value: responseValue.description, sourcePointer });
    }

    if (responseValue.schema) {
      if (produces.length === 0 || (produces.length === 1 && produces[0] === "*/*")) {
        throw new Error(
          `Operation response '${sourcePointer}' produces type couldn't be resolved. Operation is probably is missing a produces field and there isn't any global value. Please add "produces": [<contentType>]"`,
        );
      }

      responseTarget.__set__("content", this.newObject(sourcePointer));
      const content = responseTarget.content!;
      for (const mimetype of produces) {
        content.__set__(mimetype, this.newObject(sourcePointer));
        content[mimetype]!.__set__("schema", this.newObject(sourcePointer));
        for (const { key, value, childIterator } of responsesFieldMembers()) {
          if (key === "schema") {
            await this.visitSchema(content[mimetype]!.schema as any, value, childIterator);
          }
        }
        if (responseValue.examples && responseValue.examples[mimetype]) {
          const example: any = {};
          example.value = responseValue.examples[mimetype];
          content[mimetype]!.__set__("examples", this.newObject(sourcePointer));
          content[mimetype]!.examples!.__set__("response", { value: example, sourcePointer });
        }
      }

      // examples outside produces
      for (const mimetype in responseValue.examples) {
        if (responseTarget.content?.[mimetype] === undefined) {
          this.logger.trackWarning({
            code: "Oai2ToOai3/InvalidResponseExamples",
            message: `Response examples has mime-type '${mimetype}' which is not define in the local or global produces. Example will be ignored.`,
            source: [{ path: appendJsonPointer(sourcePointer, "examples", mimetype), document: this.originalFilename }],
          });
        }
      }
    }

    if (responseValue.headers) {
      responseTarget.__set__("headers", this.newObject(sourcePointer));
      for (const h in responseValue.headers) {
        responseTarget.headers!.__set__(h, this.newObject(sourcePointer));
        await this.visitHeader(responseTarget.headers![h]! as any, responseValue.headers[h], sourcePointer);
      }
    }

    // copy extensions
    for (const { key, pointer: fieldPointer, value } of responsesFieldMembers()) {
      if (isExtensionKey(key) && responseTarget[key] === undefined) {
        responseTarget.__set__(key, { value: value, sourcePointer: fieldPointer });
      }
    }
  }