Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JSON Schema] Customize names of properties #14

Open
masp opened this issue Feb 10, 2024 · 0 comments
Open

[JSON Schema] Customize names of properties #14

masp opened this issue Feb 10, 2024 · 0 comments

Comments

@masp
Copy link

masp commented Feb 10, 2024

» pkl --version
Pkl 0.25.1 (macOS 14.1, native)
org.json_schema.contrib.generate = 1.0.0

Context

I am hoping to use Pkl to define AWS Cloudformation templates for creating infrastructure as an alternative to defining them in JSON or using CDK. I saw the new JSON schema generator and received the following output:

Command

pkl eval package://pkg.pkl-lang.org/pkl-pantry/[email protected]#/generate.pkl -m cfnpkl -p source="cfn/aws-s3-bucket.json"

Error

The generation looks great, but it produces an output that looks like (unimportant fields omitted):

...
/// Configuration for the transfer acceleration state.
AccelerateConfiguration: Mapping<String, Any>?
...
class AccelerateConfiguration {
  /// Configures the transfer acceleration state for an Amazon S3 bucket.
  AccelerationStatus: "Enabled"|"Suspended" = Undefined()
}

Because in Cloudformation all fields are capitalized pascal case, there is a conflict between the type and the property name in the module. If I try to import the generated pkl file, I receive the following error on render:

test.pkl

import "cfn.pkl"

resources {
    ["MyBucket"] = new AwsS3Bucket {
        BucketName = "mybucket"
    }
}
Documents/pkl-test » pkl eval test.pkl
–– Pkl Error ––
Duplicate definition of member `AccelerateConfiguration`.

10 | AccelerateConfiguration: Mapping<String, Any>?
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at test#AwsS3Bucket (file:///Users/markasp/Documents/pkl-test/cfnpkl/AwsS3Bucket.pkl, line 10)

5 | ["MyBucket"] = new AwsS3Bucket {
                       ^^^^^^^^^^^
at test#resources["MyBucket"] (file:///Users/markasp/Documents/pkl-test/test.pkl, line 5)

106 | text = renderer.renderDocument(value)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.1/stdlib/base.pkl#L106)

There also seems to be a tangential bug where the property name is not associated with the type correctly. The JSON schema looks like:

"definitions": {
  "AccelerateConfiguration" : {
      "type" : "object",
      "additionalProperties" : false,
      "properties" : {
        "AccelerationStatus" : {
          "description" : "Configures the transfer acceleration state for an Amazon S3 bucket.",
          "type" : "string",
          "enum" : [ "Enabled", "Suspended" ]
        }
      },
      "required" : [ "AccelerationStatus" ]
    },
},
...
"properties": {
  "AccelerateConfiguration" : {
      "$ref" : "#/definitions/AccelerateConfiguration",
      "description" : "Configuration for the transfer acceleration state."
    },
}

Proposed Fix

Ideally, I'd like the property names to be lowercased and the class names to be capitalized like they are. This would make the definitions very ergonomic:

resources {
  ["MyBucket"] = new AwsS3Bucket {
    bucketName = "mybucket"
  }
}

and then I can define a converter to make it capitalized in the JSON output. Maybe a parameter on generate that allows it to transform key names in the output similar to the syntax for converters in rendering:

output {
  renderer = new JsonRenderer {
    converters {
      [Object] = (it) -> it.toMap().mapKeys((k, _) -> k.capitalize())
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant