Git Product home page Git Product logo

Comments (9)

dktapps avatar dktapps commented on June 16, 2024 1

I just tested dev-master with my schema + data and it works.. I'll never understand this godawful mess of backslashes. Thank you for the fix.

from php-json-schema-model-generator.

dktapps avatar dktapps commented on June 16, 2024

Digging, seems like this is quite an inconvenient problem to solve. My solution would be to embed the patterns as base64 blobs instead of as bare strings, and then generate code like preg_match(base64_decode('AAAAAAA===')) instead, so that PHP string escaping doesn't get a chance to screw anything up.

from php-json-schema-model-generator.

wol-soft avatar wol-soft commented on June 16, 2024

Hi @dktapps

Thanks for the report! I'll have a look into it later.

from php-json-schema-model-generator.

wol-soft avatar wol-soft commented on June 16, 2024

After some testig I've adopted your idea of using encoded strings rather than the regular expression itself in the generated code. I've also added a test case including a regular expression with backslashes´and forward slashes to test escaping and the en-/decoding.

from php-json-schema-model-generator.

dktapps avatar dktapps commented on June 16, 2024

Additional escaping shouldn't be needed at all if you base64, no? It's only needed in the first place to prevent PHP interpreting it differently as a const string.

from php-json-schema-model-generator.

wol-soft avatar wol-soft commented on June 16, 2024

Yeah I thought the same way first but it's wrong (compare https://3v4l.org/YLRvS which matches a single backslash).

Also forward slashes must be escaped as they are used as delimiter of the regular expression.

from php-json-schema-model-generator.

dktapps avatar dktapps commented on June 16, 2024

In the sample I gave, the 4 backslashes are intended to match 1 backslash (JSON \\\\ resolves to \\ when decoded, passed to PCRE, which then turns it into literal \). I'm just wondering if that might break something.

Also forward slashes must be escaped as they are used as delimiter of the regular expression.

preg_quote() might be better for that (specifically intended for purpose), though I don't suppose it matters much.

from php-json-schema-model-generator.

wol-soft avatar wol-soft commented on June 16, 2024

Yeah escaping is always hard, in this case on three layers. The JSON Schema, the PHP code and the PCRE engine. Glad it works now as expected.

from php-json-schema-model-generator.

wol-soft avatar wol-soft commented on June 16, 2024

I've thought about it again and you are absolutely right.

Four backslashes in the JSON mean two backslashes in the regular expression due to JSON escaping. Two backslashes in the regular expression must match one backslash in the tested string due to PCRE escaping. In the current implementation four backslashes in the JSON match two backslashes in the tested string which is wrong. It's so obscure.

But the delimiter escaping must be kept 😄

Edit: changed in 7dbf81a

from php-json-schema-model-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.