question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question/bug: Deal with objects with byte arrays

See original GitHub issue

Hi,

We’ve got a bug with an object that has a JsonWebKey property.

The JsonWebKey payload returned from the external service seems to show byte arrays as strings (check the payload example). Autorest tries to deserialize the property with the Window.atob method and we get the following deserialization error:

Error: Error InvalidCharacterError: Failed to execute 'atob' on 'Window': 
The string to be decoded is not correctly encoded. occurred in deserializing the responseBody

We could mitigate the problem by nullyfing the key itself since we don’t need in the service that consumes the object. At this point, we have the following constraints:

  • We can’t modify the JsonWebKey (C#) itself since it comes from a MS library;
  • Maybe we’re missing something in our Swagger document;
  • Maybe there is a special switch that we are not aware of for the code generation with Autorest;

Thanks in advance for your help 😄

JsonWebKey payload example:
{"kty":"RSA","n":"3Ab3gTAExSETjhzhcqKI8vxaQguel5ICr-MQA_AlwxfoOd_tgpGbI9WEG3A7b9txjlNDrYD-5_GW9Y_ls5e2X-XUvrg9Bhn91j3kX7lqUssE4Ht0yk8Ua4i4fkNrPuPg9_RhBK4je4NetPChG9potyMMGDTMDfzimseZ6r-xuvMDwMmlvOtEH_vQBckLag3Tf11_wruVA697ZYqskfYPFG-zIQzmQRR6_-qBPmU6033xSWRgyZ6FeOB6lCW16O6UvLVOZnMwkS3IO6_q7RHLXT0_ucM1xIWs18X9S9-3bVg8inFKRNrUEbFRbt6O2rzzPZw8sRHiCcMBAj7SteqWOQ","e":"AQAB"}
Swagger definition (swagger.json)

image

JsonWebKey interface (Typescript)
**
 * @interface
 * An interface representing JsonWebKey.
 */
export interface JsonWebKey {
  /**
   * @member {string} [kid]
   */
  kid?: string;
  /**
   * @member {string} kty
   */
  kty: string;
  /**
   * @member {string[]} [keyOps]
   */
  keyOps?: string[];
  /**
   * @member {Uint8Array} [n]
   */
  n?: Uint8Array;
  /**
   * @member {Uint8Array} [e]
   */
  e?: Uint8Array;
  /**
   * @member {Uint8Array} [d]
   */
  d?: Uint8Array;
  /**
   * @member {Uint8Array} [dp]
   */
  dp?: Uint8Array;
  /**
   * @member {Uint8Array} [dq]
   */
  dq?: Uint8Array;
  /**
   * @member {Uint8Array} [qi]
   */
  qi?: Uint8Array;
  /**
   * @member {Uint8Array} [p]
   */
  p?: Uint8Array;
  /**
   * @member {Uint8Array} [q]
   */
  q?: Uint8Array;
  /**
   * @member {Uint8Array} [k]
   */
  k?: Uint8Array;
  /**
   * @member {Uint8Array} [keyHsm]
   */
  keyHsm?: Uint8Array;
}
JsonWebKey mapper (Typescript)
export const JsonWebKey = {
  serializedName: "JsonWebKey",
  type: {
    name: "Composite",
    className: "JsonWebKey",
    modelProperties: {
      kid: {
        serializedName: "kid",
        type: {
          name: "String"
        }
      },
      kty: {
        required: true,
        serializedName: "kty",
        type: {
          name: "String"
        }
      },
      keyOps: {
        serializedName: "key_ops",
        type: {
          name: "Sequence",
          element: {
            serializedName: "stringElementType",
            type: {
              name: "String"
            }
          }
        }
      },
      n: {
        serializedName: "n",
        type: {
          name: "ByteArray"
        }
      },
      e: {
        serializedName: "e",
        type: {
          name: "ByteArray"
        }
      },
      d: {
        serializedName: "d",
        type: {
          name: "ByteArray"
        }
      },
      dp: {
        serializedName: "dp",
        type: {
          name: "ByteArray"
        }
      },
      dq: {
        serializedName: "dq",
        type: {
          name: "ByteArray"
        }
      },
      qi: {
        serializedName: "qi",
        type: {
          name: "ByteArray"
        }
      },
      p: {
        serializedName: "p",
        type: {
          name: "ByteArray"
        }
      },
      q: {
        serializedName: "q",
        type: {
          name: "ByteArray"
        }
      },
      k: {
        serializedName: "k",
        type: {
          name: "ByteArray"
        }
      },
      keyHsm: {
        serializedName: "key_hsm",
        type: {
          name: "ByteArray"
        }
      }
    }
  }
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Eric-Bonneaucommented, Nov 20, 2019

If anyone encounters this issue using Swashbuckle in the future, you can introduce an ISchemaFilter for JsonWebKey. That way you can apply a format to the properties with base64 URL encoding after Swashbuckle has already generated the schema.

Something like this (note I’m using Swashbuckle.AspNetCore v3.0.0 and the API may be different in a later version):

public class JsonWebKeyFilter : ISchemaFilter
{
    public void Apply(Schema schema, SchemaFilterContext context)
    {
        if (context.SystemType == typeof(JsonWebKey))
        {
            schema.Properties["n"].Format = "base64url";
            // do the same thing for the other properties
        }
    }
}

The problem is that the JsonWebKey class has custom JSON converters for several properties and AutoRest doesn’t know about them. So if you introduce a filter like above, you can override the schema generated by Swashbuckle so that way AutoRest knows how to (de)serialize those properties.

0reactions
ffMathycommented, Jul 5, 2021

@RikkiGibson this is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Bytes, Bytearray - w3resource
Bytes, Bytearray. Python supports a range of types to store sequences. There are six sequence types: strings, byte sequences (bytes objects) ...
Read more >
Python Concepts/Bytes objects and Bytearrays - Wikiversity
6 Operations with methods on bytes objects. 6.1 Creating and using a translation table: 7 bytes objects and disk files; 8 bytearrays.
Read more >
How to deal with huge amount of byte arrays - Stack Overflow
The issue is: I'm receiving byte arrays and I immediately format them via pritHex method, which I think causes some delays and I...
Read more >
ArrayBuffer - JavaScript - MDN Web Docs
Chrome Edge ArrayBuffer Full support. Chrome7. Toggle history Full support. Edge12. Tog... @@species Full support. Chrome51. Toggle history Full support. Edge13. Tog... ArrayBuffer() constructor Full support....
Read more >
Python | bytearray() function - GeeksforGeeks
bytearray () method returns a bytearray object which is an array of given bytes. It gives a mutable sequence of integers in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found