apple / pkl-pantry

Shared Pkl packages
Apache License 2.0
229 stars 31 forks source link

[JSON Schema Contrib] Schema generator produces duplicated class types #20

Closed tbinna closed 2 weeks ago

tbinna commented 4 months ago

I attempted to generate a pkl schema from a JSON schema as explained here. Strangely, the output contained duplicated class definitions, which caused the pkl eval process to fail with

–– Pkl Error ––
Duplicate definition of member `Target`.

544 | class Target {
      ^^^^^^^^^^^^

Reproduction

Generate a pkl schema from the JSON schema:

 pkl eval package://pkg.pkl-lang.org/pkl-pantry/org.json_schema.contrib@1.0.0#/generate.pkl -m . -p source="https://raw.githubusercontent.com/toolsplus/pkl-forge/main/out/manifest-schema.json"

then run

pkl eval ManifestSchema.pkl

I believe the issue is L225 in the snippet below. existingTypeNames has type Set<Type>, and the contains function compares the set entries to a string, which always returns false.

https://github.com/apple/pkl-pantry/blob/4f8f939c35ff1fdc04b0897db4b53c05ff234186/packages/org.json_schema.contrib/internal/ModuleGenerator.pkl#L224-L245

Solution

Update L225 as follows:

if (existingTypeNames.map((type) -> type.name).contains(utils.pascalCase(candidateName)))

I tested this successfully with the reproduction case.

jstrachan commented 2 months ago

can this be closed now?

holzensp commented 2 weeks ago

Interesting; PR was marked by OP. Closing this, assuming that's correct.