json-kotlin-schema-codegen icon indicating copy to clipboard operation
json-kotlin-schema-codegen copied to clipboard

Invalid code generated for `patternProperties` containing objects

Open gino-m opened this issue 1 year ago • 3 comments

For example:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "foo.schema.json",
  "type": "object",
  "patternProperties": {
    "abc": {
      "type": "object",
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}

Generated code:

    init {
        cg_map.keys.forEach { key ->
            if (!.matches(key))
                throw IllegalArgumentException("Unexpected field $key")
        }
    }

Compile error:

e: file:///...Foo.kt:18:18 Expecting an element

Running through Gradle plugin v0.102 with "additionalPropertiesOption": "strict" in config.

@pwall567 Any thoughts?

gino-m avatar Feb 21 '24 17:02 gino-m

Hi @gino-m , thank you for the bug report. You're testing using version 0.102, so you've obviously seen that there have already been a couple of bug fix releases for this new functionality.

I'm hopeful this will be another quick one; I'll let you know within 24 hours either way.

pwall567 avatar Feb 21 '24 23:02 pwall567

Thanks, @pwall567! I assume the type in patternProperties will only be checked at run-time, i.e., represented as an Any in generated code? If so I will probably still work around this by using additionalProperties instead, as it generated a strongly typed field.

gino-m avatar Feb 22 '24 15:02 gino-m

That's correct @gino-m , patternProperties can be checked only at at run time.

Version 0.103 should fix the problem you identified in this issue, but I agree, it may be preferable to use additionalProperties.

pwall567 avatar Feb 22 '24 20:02 pwall567