cyclosproject / ng-openapi-gen

An OpenAPI 3.0 codegen for Angular
MIT License
397 stars 132 forks source link

application/x-www-form-urlencoded not generated correctly #70

Closed elonmallin closed 4 years ago

elonmallin commented 4 years ago

I have an endpoint that should use application/x-www-form-urlencoded but the data is sent as a json string with the content type being application/x-www-form-urlencoded.

I got this function generated:

/**
   * This method provides access to the full `HttpResponse`, allowing access to response headers.
   * To access only the response body, use `authAccessToken()` instead.
   *
   * This method sends `application/x-www-form-urlencoded` and handles request body of type `application/x-www-form-urlencoded`.
   */
  authAccessToken$Response(params?: {
    body?: { 'grant_type': string, 'scope'?: string, 'client_id'?: string, 'client_secret'?: string }
  }): Observable<StrictHttpResponse<TokenAuthenticateResult>> {

    const rb = new RequestBuilder(this.rootUrl, AuthService.AuthAccessTokenPath, 'post');
    if (params) {

      rb.body(params.body, 'application/x-www-form-urlencoded');
    }
    return this.http.request(rb.build({
      responseType: 'json',
      accept: 'application/json'
    })).pipe(
      filter((r: any) => r instanceof HttpResponse),
      map((r: HttpResponse<any>) => {
        return r as StrictHttpResponse<TokenAuthenticateResult>;
      })
    );
  }

But the problem seems to be in: rb.body which has this content:

body(value: any, contentType = 'application/json'): void {
    if (value instanceof Blob) {
      this._bodyContentType = value.type;
    } else {
      this._bodyContentType = contentType;
    }
    if ((this._bodyContentType || '').startsWith('multipart/form-data')) {
      // Handle multipart form data
      const formData = new FormData();
      if (value != null) {
        for (const key of Object.keys(value)) {
          const val = value[key];
          if (val instanceof Array) {
            for (const v of val) {
              const toAppend = this.formDataValue(v);
              if (toAppend !== null) {
                formData.append(key, toAppend);
              }
            }
          } else {
            const toAppend = this.formDataValue(val);
            if (toAppend !== null) {
              formData.set(key, toAppend);
            }
          }
        }
      }
      this._bodyContent = formData;
    } else {
      // The body is the plain content
      this._bodyContent = value;
    }
  }

Probably there needs to be a check for application/x-www-form-urlencoded also?

elonmallin commented 4 years ago

This is the requestBody of that method in the apoen api json.

"requestBody": {
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "required": [
                  "grant_type"
                ],
                "type": "object",
                "properties": {
                  "grant_type": {
                    "type": "string"
                  },
                  "scope": {
                    "type": "string"
                  },
                  "client_id": {
                    "type": "string"
                  },
                  "client_secret": {
                    "type": "string"
                  }
                }
              },
              "encoding": {
                "grant_type": {
                  "style": "form"
                },
                "scope": {
                  "style": "form"
                },
                "client_id": {
                  "style": "form"
                },
                "client_secret": {
                  "style": "form"
                }
              }
            }
          }
        },

It's generated from Swashbuckle.Swaggergen for C#.

luisfpg commented 4 years ago

application/x-www-form-urlencoded is not currently implemented. Will be for version 0.8.0.

elonmallin commented 4 years ago

Okey, nice =)