openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [cpp-ue4] formParams not handled in json requests

Open leith-bartrich opened this issue 5 years ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating ue4 client code, formParams generated for a standard Django auth-token request endpoint do not generate usable code and instead erroneously print the error: "Form parameter ({{baseName}}) was ignored, cannot be used in JsonBody".

The correct behavior seems to instead be, to construct json object from the formParams and pass that as the body. This satisfies Django's standard server implementation of this standard Django api.

e.g.

the development HEAD version generates this request code:

// Default to Json Body request
	if (Consumes.Num() == 0 || Consumes.Contains(TEXT("application/json")))
	{
		UE_LOG(Logfpipelite_client, Error, TEXT("Form parameter (username) was ignored, cannot be used in JsonBody"));
		UE_LOG(Logfpipelite_client, Error, TEXT("Form parameter (password) was ignored, cannot be used in JsonBody"));
		UE_LOG(Logfpipelite_client, Error, TEXT("Form parameter (token) was ignored, cannot be used in JsonBody"));
	}

When it seems it should instead be constructing a json object from the formParams.

I have a pull request authored that fixes this behavior and will submit it shortly.

e.g.

my proposed fix (to be submitted shortly) generates this request code:

	// Default to Json Body request
	if (Consumes.Num() == 0 || Consumes.Contains(TEXT("application/json")))
	{
            // Form parameters
            FString JsonBody;
            JsonWriter Writer = TJsonWriterFactory<>::Create(&JsonBody);
            Writer->WriteObjectStart();
            Writer->WriteIdentifierPrefix(TEXT("username"));
            WriteJsonValue(Writer, Username);
            Writer->WriteIdentifierPrefix(TEXT("password"));
            WriteJsonValue(Writer, Password);
            if (Token.IsSet()){
                Writer->WriteIdentifierPrefix(TEXT("token"));
                WriteJsonValue(Writer, Token.GetValue());
            }
            Writer->WriteObjectEnd();
            Writer->Close();
            HttpRequest->SetHeader(TEXT("Content-Type"), TEXT("application/json; charset=utf-8"));
            HttpRequest->SetContentAsString(JsonBody);
	}

I have interpreted this as a template problem rather than a generator problem. The generator sends formParams to the template and not bodyParams. And Django, in turn, is happy receiving these as part of a Json object sent as the body.

It should be noted even this has issues, as I'm not using readOnly and writeOnly information here and I probably should be. The 'token' parameter is readOnly and should be skipped as this is in the request, rather than depend upon "IsSet".

I have included a sample openapi schema below.

An alternative interpretation might be that the generator should be instead passing a bodyParam to the template here. But I'm not expert enough in the translation of OpenAPI schemas to http behavior to know if it is properly converting to formParams here, or improperly doing so. So I have to assume that the generator is doing the right thing and that the template is at fault.

Before I submit the pull request however, I do want to try and verify (sanity check) that I'm correctly identifying the template as the issue rather than the generator.

openapi-generator version

development head: openapi-generator-project 5.3.0-SNAPSHOT

OpenAPI declaration file content or url

the following openapi declaration is generated by Django from a very basic (tutorial basically) codebase.

of note is the api-token-auth endpoint and AuthToken component.

This is an actual default Django generated REST schema. It is not custom. You can probably ignore the 'LegalEntity' endpoints. They're also fairly standard Django generated REST view code and illuminate other issues I'll likely submit separately.

openapi: 3.0.2
info:
  title: ''
  version: ''
paths:
  /orgbuilder/legal_entity/:
    get:
      operationId: listLegalEntitys
      description: ''
      parameters: []
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/LegalEntity'
          description: ''
      tags:
      - orgbuilder
    post:
      operationId: createLegalEntity
      description: ''
      parameters: []
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LegalEntity'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/LegalEntity'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/LegalEntity'
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LegalEntity'
          description: ''
      tags:
      - orgbuilder
  /orgbuilder/legal_entity/{id}/:
    get:
      operationId: retrieveLegalEntity
      description: ''
      parameters:
      - name: id
        in: path
        required: true
        description: A UUID string identifying this legal entity.
        schema:
          type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LegalEntity'
          description: ''
      tags:
      - orgbuilder
    put:
      operationId: updateLegalEntity
      description: ''
      parameters:
      - name: id
        in: path
        required: true
        description: A UUID string identifying this legal entity.
        schema:
          type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LegalEntity'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/LegalEntity'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/LegalEntity'
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LegalEntity'
          description: ''
      tags:
      - orgbuilder
    patch:
      operationId: partialUpdateLegalEntity
      description: ''
      parameters:
      - name: id
        in: path
        required: true
        description: A UUID string identifying this legal entity.
        schema:
          type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/LegalEntity'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/LegalEntity'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/LegalEntity'
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/LegalEntity'
          description: ''
      tags:
      - orgbuilder
    delete:
      operationId: destroyLegalEntity
      description: ''
      parameters:
      - name: id
        in: path
        required: true
        description: A UUID string identifying this legal entity.
        schema:
          type: string
      responses:
        '204':
          description: ''
      tags:
      - orgbuilder
  /orgbuilder/api-token-auth/:
    post:
      operationId: createAuthToken
      description: ''
      parameters: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/AuthToken'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/AuthToken'
          application/json:
            schema:
              $ref: '#/components/schemas/AuthToken'
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthToken'
          description: ''
      tags:
      - orgbuilder
components:
  schemas:
    LegalEntity:
      type: object
      properties:
        id:
          type: string
          format: uuid
          readOnly: true
        created_at:
          type: string
          format: date-time
          readOnly: true
        modified_at:
          type: string
          format: date-time
          readOnly: true
        short_name:
          type: string
          description: The short name of the item (used for filenames.)
          maxLength: 255
        long_name:
          type: string
          description: A longer, formal name for the item, that would show up in text.
          maxLength: 255
        description:
          type: string
          description: A short description of the item for informational purposes.
      required:
      - short_name
      - long_name
      - description
    AuthToken:
      type: object
      properties:
        username:
          type: string
          writeOnly: true
        password:
          type: string
          writeOnly: true
        token:
          type: string
          readOnly: true
      required:
      - username
      - password
Generation Details

my cli is:

openapi-generator generate -g cpp-ue4 -i ./openapi-schema.yml --additional-properties cppNamespace=fpipelite::client --additional-properties unrealModuleName=fpipelite_client

though yours probably would vary a bit.

Steps to reproduce
  • create a basic REST server in Django including the built in api-token-auth implementation.
  • export the api schema.
  • generate a cpp-ue4 client using openapi-generator.
  • review the generated code.
Related issues/PRs
Suggest a fix

leith-bartrich avatar Aug 17 '21 13:08 leith-bartrich