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

Representing objects with arbitrary keys and values #35

Open
gino-m opened this issue Jan 23, 2024 · 6 comments
Open

Representing objects with arbitrary keys and values #35

gino-m opened this issue Jan 23, 2024 · 6 comments

Comments

@gino-m
Copy link

gino-m commented Jan 23, 2024

Defining "type": "object" in JSON Schema always causes an open class to be generated, even when no properties are defined. How do we represent a plain Map, where property keys and values are determined at runtime?

Here's what I have now:

Example schema:

{
  "$id": "https://groundplatform.org/loi-document.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Location of Interest",
  "type": "object",
  "properties": {
    "foo": {
      "type": "object",
      "additionalProperties": true
    }
  }
}

Note: "additionalProperties": true is the default in JSON Schema, including it here for clarity.

The generated code:

data class LoiDocument(
    val foo: Foo? = null,
) {
    open class Foo
}
@gino-m gino-m changed the title Mapping of arbitrary data structures (additionalProperties: true) Representing object with arbitrary keys and values Jan 23, 2024
@gino-m gino-m changed the title Representing object with arbitrary keys and values Representing objects with arbitrary keys and values Jan 23, 2024
@gino-m
Copy link
Author

gino-m commented Jan 23, 2024

For posterity and FYI, here's how I worked around this:

Schema:

{
  "$id": "https://groundplatform.org/loi-document.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "foo": {
      "type": "object",
      "x-local-type": "map"
    }
  }
}

config.json:

{
  "customClasses": {
    "extension": {
      "x-local-type": {
        "map": "kotlin.collections.Map"
      }
    }
  }
}

The generated code:

import kotlin.collections.Map

data class LoiDocument(
    val foo: Map? = null,
) 

Unfortunately, generics aren't working in the class mapping since they cause the following invalid import to be generated:

import kotlin.collections.Map<String, Any>

I worked around this by defining a typealias:

Map.kt:

typealias Map = kotlin.collections.Map<String, Any>

And then referring to that in the mapping in config.json instead of Map<String, Any>.

Full solution in google/ground-android#2205

@pwall567
Copy link
Owner

Hi @gino-m , I often find myself having to respond to requests and suggestions from users by saying that their proposal sounds great, but it would be competing for my time with other equally worthy developments.

In this case, however, I can tell you that a solution for your issue has been under active development for a few weeks now, and I'm hopeful of having a working version within a week to ten days (yes, I'm aware of Hofstadter's Law).

Thank you for confirming the demand for this functionality; I'll report back here when I have a version for you to test. In the meantime, the use of custom classes and a typealias to Map<String, Any?> is exactly what I have been recommending to anyone who needs to represent an object with unknown property names.

@gino-m
Copy link
Author

gino-m commented Jan 25, 2024

Hi @pwall567, thanks for your prompt, thoughtful reply. It's encouraging to know that issues and FRs are being addressed. We'd be happy to test out a fix once it's available. Keep up the great work!

@pwall567
Copy link
Owner

pwall567 commented Feb 8, 2024

OK, as expected, it took longer than I expected, but version 0.100 has now been uploaded to Maven Central. This version includes support for additionalProperties and patternProperties, but it requires a configuration setting to enable this form of generation.

I hope this meets your needs, and I would welcome any feedback you may be able to give me.

@gino-m
Copy link
Author

gino-m commented Feb 8, 2024

@pwall567 This is great news! And timely, since we're just getting back to applying JSON Schema in groundplatform.org. Thank you for the fix and the update!

@gino-m
Copy link
Author

gino-m commented Feb 8, 2024

@os-micmec FYI

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

2 participants