`context` and `metadata` Map support

Ouverte
#496 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
45/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
javascript
Domaine
api

Piste de recherche

Commencez par lib/utils/index.js autour de la fonction encode_context référencée dans le rapport, puis reproduisez les deux assertions fournies pour les entrées Map et Object. Le travail est terminé lorsque context et metadata préservent les clés et les valeurs numériques et produisent les chaînes encodées attendues sans obliger les appelants à les convertir au préalable.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Bug report for Cloudinary NPM SDK

Describe the bug in a sentence or two.

The documentation states that context and metadata both support Maps when using this SDK to upload images. However, when attempting to use a Map or even a standard Object, this does not seem to be fully supported. Images are uploaded with missing or entirely omitted context and no error is thrown during the call.

Screen Shot 2021-06-15 at 5 31 12 PM

Issue Type (Can be multiple)

[ ] Build - Can’t install or import the SDK
[ ] Babel - Babel errors or cross browser issues
[ ] Performance - Performance issues
[x] Behaviour - Functions aren’t working as expected (Such as generate URL)
[x] Documentation - Inconsistency between the docs and behaviour
[ ] Incorrect Types - For typescript users who are having problems with our d.ts files
[ ] Other (Specify)

Steps to reproduce

The issue appears to be with the encode_context function which is used on both context and metadata options if provided:

https://github.com/cloudinary/cloudinary_npm/blob/4e3b0f820c3ff99aad6cccd6d3874aca95d25a50/lib/utils/index.js#L566-L571

In the case of actually providing a Map, the encode function fails to encode entirely and the result is an empty string. In the case of providing an Object, the encoding fails if any of the key/value pairs are a number. When a key value pair is a number only the value is encoded and not the key which leads to unexpected results.

The best way to test is with the following:

const { strict: assert } = require('assert')
const { encode_context: encode } = require('cloudinary/lib/utils')

const contextMap = new Map()
contextMap.set('storeNumber', 100148)
contextMap.set('vin', '5YJ3E1EA1JF045327')

// Fails: encoded context is an empty string
assert.equal(encode(contextMap), 'storeNumber=100148|vin=5YJ3E1EA1JF045327')

const contextObj = { storeNumber: 100148, vin: '5YJ3E1EA1JF045327' }

// Fails: encoded context is missing `storeNumber=`
assert.equal(encode(contextObj), 'storeNumber=100148|vin=5YJ3E1EA1JF045327')

The encoding only appears to work if an Object is supplied and all of the key/value pairs are first converted to a String.

In the meantime we've had to resort to converting our context argument to a string before passing it to the Cloudinary SDK so we ensure consistency, ex:

await cloudinary.uploader.upload(url, {
  async: true,
  context: Object.entries(context).map(val => val.join('=')).join('|')
})

Versions and Libraries (fill in the version numbers)

Cloudinary_NPM SDK version - 1.26.0
Node - 14.16.1
NPM - 6.14.13

Langage dominant
JavaScript
Étoiles
672
Forks
318
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

  • Aucun Dockerfile ni fichier Docker Compose
  • Propose un modèle de pull request
  • Aucun guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de cloudinary/cloudinary_npm

Toutes les issues de cloudinary/cloudinary_npm

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.