Skip to content

Conversation

@m-melgizin
Copy link
Contributor

@m-melgizin m-melgizin commented Jul 11, 2025

MetaDescriptor and JSON Schema Conversion

The implementation provides bi-directional conversion between MetaDescriptor and JSON Schema (as JsonObject) formats

Example Usage:

// Create a descriptor
val descriptor = MetaDescriptor {
    node("user") {
        value("name", ValueType.STRING) {
            required()
            description = "User full name"
        }
        value("age", ValueType.NUMBER) {
            default(18)
        }
    }
}
// Convert to JSON Schema
val schema = descriptor.toJsonSchema() 

Resulting JSON Schema representation:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "required": [
    "user"
  ],
  "properties": {
    "user": {
      "title": "user",
      "required": [
        "name"
      ],
      "properties": {
        "name": {
          "title": "name",
          "description": "User full name",
          "type": "string",
          "__indexKey__": "@index",
          "__multiple__": false,
          "__attributes__": {}
        },
        "age": {
          "title": "age",
          "type": "number",
          "default": 18,
          "__indexKey__": "@index",
          "__multiple__": false,
          "__attributes__": {}
        }
      },
      "__indexKey__": "@index",
      "__multiple__": false,
      "__attributes__": {}
    }
  },
  "__indexKey__": "@index",
  "__multiple__": false,
  "__attributes__": {}
}
// Convert back to descriptor
val restoredDescriptor = schema.toMetaDescriptor()

Related Issues:

closes #35
closes #KS-522

Copy link
Contributor

@SPC-code SPC-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add general MR description and at least one example of usage of schema.

sourceSets {
val commonTest by getting {
dependencies {
implementation("io.github.optimumcode:json-schema-validator:0.5.2")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move it to kscience.commonTest block.

import kotlinx.serialization.json.*
import space.kscience.dataforge.meta.*

public object MetaDescriptorJsonSchemaConverter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document all public classes, objects and methods.

import space.kscience.dataforge.meta.*

public object MetaDescriptorJsonSchemaConverter {
public fun convert(metaDescriptor: MetaDescriptor): JsonObject {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to give different names for forward and backward conversion

/**
* Convert [MetaDescriptor] to a JSON Schema [JsonObject]
*/
public fun MetaDescriptor.toJsonSchema(): JsonObject {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to make converter private or avoit namespace object at all?

}

// Assert
assertTrue(result.isSuccess, "Expected no exception but got ${result.exceptionOrNull()}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very strange test design. You can just run function. It will throw an exception if test fails.

node(name, childDescriptor.applyRequiredRestrictions())
}
}
} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aree there any test to actually test conversion correctness?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current tests already validate:

  1. Schema validity - testIsJsonSchemaValid checks that the generated JSON Schema is syntactically correct (via JsonSchema.fromJsonElement from library io.github.optimumcode:json-schema-validator)
  2. Round-trip conversion – testIsSerializationAndDeserializationWorksCorrect ensures descriptor → schema → descriptor produces an equivalent structure

If you’d like additional validation of specific schema properties, let me know - I can add targeted test cases.

@SPC-code SPC-code merged commit 1e86fe4 into SciProgCentre:dev Jul 14, 2025
0 of 2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants