filterMediaTypes()

in packages/extensions/modelerfour/src/modeler/modelerfour.ts [1342:1421]


  filterMediaTypes(oai3Content: Record<string, MediaType> | undefined) {
    const mediaTypeGroups = this.bodyProcessor.groupMediaTypes(oai3Content);

    // filter out invalid combinations
    //if (length(mediaTypeGroups.keys()) > 0) {
    // because the oai2-to-oai3 conversion doesn't have good logic to know
    // which produces type maps to each operation response,
    // we have to go thru the possible combinations
    // and eliminate ones that don't make sense.
    // (ie, a binary media type should have a binary response type, a json or xml media type should have a <not binary> type ).
    for (const [knownMediaType, mt] of [...mediaTypeGroups.entries()]) {
      for (const fmt of mt) {
        if (this.interpret.isBinarySchema(fmt.schema.instance)) {
          // if the schema really says 'type: file', we have to accept all the formats
          // that were listed in the original 'produces' collection
          // because we *can't* infer that a json/xml/form media type means deserialize

          switch (knownMediaType) {
            case KnownMediaType.Json:
            case KnownMediaType.Xml:
            case KnownMediaType.Form:
              // it's been mis-categorized as a deserialization
              // but they said,"stream please"
              // then we have to move it to the binary bucket.
              // eslint-disable-next-line no-case-declarations
              let b = mediaTypeGroups.get(KnownMediaType.Binary);
              if (!b) {
                // we don't have a binary group at all.
                // let's just create one
                b = [];
                mediaTypeGroups.set(KnownMediaType.Binary, b);
              }
              b.push(fmt);
              // remove the current group
              mediaTypeGroups.delete(knownMediaType);
          }
        } else {
          switch (knownMediaType) {
            case KnownMediaType.Json:
            case KnownMediaType.Xml:
            case KnownMediaType.Form:
              if (!fmt.schema) {
                // is this a good check?
                throw new Error(
                  `Object Response ${knownMediaType}:${fmt.mediaType} has no schema for the response, and can't deserialize.`,
                );
              }
              // if the schema is binary, then it shouldn't be an object deserialization step. (oai2-to-oai3 upconversion ugly)
              if (this.interpret.isBinarySchema(fmt.schema.instance)) {
                // bad combo, remove.
                mediaTypeGroups.delete(knownMediaType);
                continue;
              }
              break;
            case KnownMediaType.Binary:
            case KnownMediaType.Text:
              if (!fmt.schema.instance) {
                // if we don't have a schema at all, should we infer a binary schema anyway?
                // dunno.
              }

              if (
                !(knownMediaType === KnownMediaType.Text && fmt.schema.instance?.type === JsonType.String) &&
                !this.interpret.isBinarySchema(fmt.schema.instance)
              ) {
                // bad combo, remove.
                mediaTypeGroups.delete(knownMediaType);
                continue;
              }
              break;

            default:
              throw new Error(`Not able to process media type ${fmt.mediaType} at this moment.`);
          }
        }
      }
    }
    // }
    return mediaTypeGroups;
  }