Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on header mapping on v2 proxy for media files #100

Closed
jofunkillo opened this issue Nov 7, 2020 · 1 comment
Closed

Error on header mapping on v2 proxy for media files #100

jofunkillo opened this issue Nov 7, 2020 · 1 comment

Comments

@jofunkillo
Copy link

I'm working on a documents manager using CAP + freestyle app with an upload collection UI element. The project is totally working now, but to achieve it I had to patch the index.js on the cds-odata-v2-adapter-proxy.

My data structure is having some additional fields I want to fill together with the attachment:

entity DocumentVersion: cuid, managed {
    versionNo: Integer;    
    comment: String; 
    document: Association to one Document;
    filename: String;    
    mimetype : String @Core.IsMediaType;
    content: LargeBinary @Core.MediaType: mimetype;     
    filesize: Integer;    
}

On method onBeforeUploadStarts on my controller implementation I'm passing those fields (versionNo, comment, document_ID, filesize) to the header in order to get them correctly filled in the database entity entry:

var oVersionHeaderField = new sap.m.UploadCollectionParameter({
	name: "versionNo",
	value: "65"
});
oEvent.getParameters().addHeaderParameter(oVersionHeaderField);
		
var filesize = oEvent.getSource()._getFileUploader().oFileUpload.files[0].size;
var oFileSizeHeaderField = new sap.m.UploadCollectionParameter({
	name: "filesize",
	value: 123432 //filesize
});
oEvent.getParameters().addHeaderParameter(oFileSizeHeaderField);

Those two parameters are the example of the two problems I have found.

First one is having capital letters on the field name, on my example versionNo. The parameter is ignored, because parameters coming from the UI are lowercase converted and the following comparison is not working fine:

// Custom body
Object.keys(headers).forEach((name) => {
     if (definition.elements[name]) {
         body[name] = headers[name];
      }
});

definition.elements does not match with the lowercase version stored on the header, and thus the value of those parameters is ignored.

The second problem is related to the data types. I'm having deserialization issue, no matter how I try to store the value for the filesize. The assignment:

body[name] = headers[name];

is just not having into account this possibility and enter the value '123432' that causes later the deserialization error.

My fix for both issues is like this:

// Custom body
   Object.keys(customBody).forEach((name) => {
       for (const element in definition.elements) {
         if (name === element.toLowerCase()){
          if (definition.elements[element].type === "cds.Integer") {
            body[element] = parseInt(customBody[name],10);
          }
          else {
             body[element] = customBody[name];
          }
       }              
    };
});

Probaby, some other data types may need to be taken into account here.

@jofunkillo
Copy link
Author

jofunkillo commented Nov 25, 2020

Just to inform the library has been patched and all problems are solved. More info here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant