oxidecomputer / typify

compiler from JSON Schema into idiomatic Rust types
Apache License 2.0
393 stars 57 forks source link

Fix decoding encoded string pattern #561

Closed Norlock closed 4 months ago

Norlock commented 4 months ago

Problem

If you pass a JSON schema with a regex pattern, you will need to encode it to avoid certain characters (e.g. backslash) to escape the JSON string.

An incorrect unescaped JSON string:

pattern: "[0-9A-Za-z\\-]*$"  

vs a correct escaped JSON string:

pattern: "[0-9A-Za-z%5c%5c-]*$"  

Solution

This PR makes sure it will decode the string value of the pattern field first before trying to validate if the regex is correct.

ahl commented 4 months ago

Thanks for this PR. I was unfamiliar with the requirement to url encode pattern strings, and I wasn't able to find a reference to that in the json schema docs. I tried this JSON schema validator https://www.jsonschemavalidator.net/ and it didn't seem to do the url decoding (it worked as expected with the pattern "^[0-9A-Za-z\\\\-]*$"). Can you share your rationale for this change? Thanks!

Norlock commented 4 months ago

You are right I got this from a stack overflow post and thought it was a correct way to escape but I assumed it a bit too quickly, :/ I will close it

ahl commented 4 months ago

Thanks for checking. And we appreciate the effort nonetheless!