invopop / invopop/jsonschema

Use allOf for embedded types

Open
#167 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
952
Forks
136
PR merge metrics
No merged PRs in 30d

Description

Hello,

I have these types:

```go
package signals

type Addressable struct {
Address int `yaml:"address"`
}

type Unitable struct {
UnitOfMeasurement string `yaml:"uom"`
}
```

```go
package A

type EntryA struct {
signals.Addressable
signals.Unitable
Name string `yaml:"name"`
}
```

And I would like to generate a schema for `EntryA`. I would prefer this schema to look like this:

```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://dev.azure.com/Allseas/Innovations/_git/CAM_scl-code-gen.git/schemas/A/entry-a",
"$ref": "#/$defs/EntryA",
"$defs": {
"Addressable": {
"properties": {
"address": {
"type": "integer"
}
},
"required": [
"address"
]
},
"Unitable": {
"properties": {
"uom": {
"type": "string"
}
},
"required": [
"uom"
]
},
"EntryA": {
"allOf": [
{
"$ref": "#/$defs/Addressable"
},
{
"$ref": "#/$defs/Unitable"
},
{
"properties": {
"name": {
"type": "string"
}
},
"type": "object",
"additionalProperties": false,
"required": [
"name"
]
}
]
}
}
}
```

Currently, the schemas are merged and not reused:

```json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://dev.azure.com/Allseas/Innovations/_git/CAM_scl-code-gen.git/schemas/A/entry-a",
"$ref": "#/$defs/EntryA",
"$defs": {
"EntryA": {
"properties": {
"address": {
"type": "integer"
},
"uom": {
"type": "string"
},
"name": {
"type": "string"
}
},
"additionalProperties": false,
"type": "object",
"required": [
"address",
"uom",
"name"
]
}
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing how the generator handles embedded Go types and assembles the schema shown for EntryA. Compare the current merged output with the requested allOf structure, including reusable Addressable and Unitable definitions. Done means embedded types are represented through allOf references without losing the fields and required properties of EntryA.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.