processParameters()

in packages/extensions/modelerfour/src/modeler/modelerfour.ts [2033:2140]


  processParameters(httpOperation: OpenAPI.HttpOperation, operation: Operation, pathItem: OpenAPI.PathItem) {
    const parameters = Object.values(httpOperation.parameters ?? {}).map((x) => ({
      ...dereference<OpenAPI.Parameter>(this.input, x),
      original: x,
    }));

    for (const pp of parameters) {
      const parameter = pp.instance;
      if (this.isParameterSpecialHeader(parameter)) {
        if (operation.specialHeaders === undefined) {
          operation.specialHeaders = [];
        }
        operation.specialHeaders.push(parameter.name);
        continue;
      }
      if (this.isParameterIgnoredHeader(parameter)) {
        continue;
      }
      if (parameter.content) {
        this.session.error(
          `Parameter '${parameter.name}' in '${parameter.in}' has content.<mediaType> which is not supported right now. Use schema instead. See https://github.com/Azure/autorest/issues/4303`,
          ["Modelerfour/ParameterContentNotSupported"],
          parameter,
        );
        continue;
      }
      this.use(parameter.schema, (name, schema) => {
        if (this.apiVersionMode !== "none" && this.interpret.isApiVersionParameter(parameter)) {
          return this.processApiVersionParameter(parameter, operation, pathItem);
        }

        // Not an APIVersion Parameter
        const implementation = pp.fromRef
          ? "method" === parameter["x-ms-parameter-location"]
            ? ImplementationLocation.Method
            : ImplementationLocation.Client
          : "client" === parameter["x-ms-parameter-location"]
            ? ImplementationLocation.Client
            : ImplementationLocation.Method;

        const preferredName = this.interpret.getPreferredName(parameter, schema["x-ms-client-name"] || parameter.name);
        if (implementation === ImplementationLocation.Client) {
          // check to see of it's already in the global parameters
          const p = this.codeModel.findGlobalParameter((each) => each.language.default.name === preferredName);
          if (p) {
            return operation.addParameter(p);
          }
        }
        let parameterSchema = this.processSchema(name || "", schema);

        // Track the usage of this schema as an input with media type
        this.trackSchemaUsage(parameterSchema, { usage: [SchemaContext.Input] });

        if (parameter.in === ParameterLocation.Header && "x-ms-header-collection-prefix" in parameter) {
          const dictionarySchema = this.codeModel.schemas.add(
            new DictionarySchema(
              parameterSchema.language.default.name,
              parameterSchema.language.default.description,
              parameterSchema,
            ),
          );
          this.trackSchemaUsage(dictionarySchema, { usage: [SchemaContext.Input] });
          parameterSchema = dictionarySchema;
        }

        const description = pp.original.description || this.interpret.getDescription("", parameter);
        /* regular, everyday parameter */
        const newParam = operation.addParameter(
          new Parameter(preferredName, description, parameterSchema, {
            required: parameter.required ? true : undefined,
            implementation,
            extensions: this.interpret.getExtensionProperties(parameter),
            deprecated: this.interpret.getDeprecation(parameter),
            nullable: (parameter as any).nullable || schema.nullable,
            protocol: {
              http: new HttpParameter(
                parameter.in,
                parameter.style
                  ? {
                      style: <SerializationStyle>(<unknown>parameter.style),
                      explode: parameter.explode,
                    }
                  : undefined,
              ),
            },
            language: {
              default: {
                serializedName: parameter.name,
              },
            },
            clientDefaultValue: this.interpret.getClientDefault(parameter, schema),
          }),
        );

        // if allowReserved is present, add the extension attribute too.
        if (parameter.allowReserved) {
          newParam.extensions = newParam.extensions ?? {};
          newParam.extensions["x-ms-skip-url-encoding"] = true;
        }

        if (implementation === ImplementationLocation.Client) {
          this.codeModel.addGlobalParameter(newParam);
        }

        return newParam;
      });
    }
  }