Question/bug: Deal with objects with byte arrays
See original GitHub issueHi,
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)
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:
- Created 5 years ago
- Reactions:1
- Comments:11 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If anyone encounters this issue using Swashbuckle in the future, you can introduce an
ISchemaFilter
forJsonWebKey
. 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):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.@RikkiGibson this is still an issue.