glideapps / glideapps/quicktype

[C#] Multiple classes generated for shared $ref definition

Open
#2,376 2 comments 0 reactions 0 assignees View on GitHub
C# enhancement rendering
Dominant language
TypeScript
Stars
13.9k
Forks
1.2k
Avg merge
8h 53m
Merged PRs (30d)
369

Description

Where there is an object type defined in a definitions section, whether in the same file or in an external one, every usage of that shared object definitions gets a separate class named after the property that references it. This makes it really hard to do things like have a custom JSON converter for that type, because there's not a single class, there are lots of classes with identical fields but different names.

Schema
```json
{
"$schema": "https://json-schema.org/draft/2019-09/schema#",
"description": "Demo of multiple classes from one definition.",
"type": "object",
"properties": {
"foo": {
"type": "object",
"description": "The Foo",
"$ref": "#/definitions/corge"
},
"bar": {
"type": "object",
"description": "A high Bar",
"$ref": "#/definitions/corge"
},
"baz": {
"type": "object",
"description": "Bazalicious",
"$ref": "#/definitions/corge"
}
},
"definitions": {
"corge": {
"type": "object",
"description": "Corgeous",
"properties": {
"name": {
"type": "string"
},
"something": {
"type": "string"
}
}
}
}
}
```

C#
```
namespace Quux
{

///
/// Demo of multiple classes from one definition.
///
public partial class DefDemo
{
///
/// A high Bar
///
public Bar Bar { get; set; }

///
/// Bazalicious
///
public Baz Baz { get; set; }

///
/// The Foo
///
public Foo Foo { get; set; }
}

///
/// A high Bar
///
/// Corgeous
///
public partial class Bar
{
public string Name { get; set; }
public string Something { get; set; }
}

///
/// Bazalicious
///
/// A high Bar
///
/// Corgeous
///
public partial class Baz
{
public string Name { get; set; }
public string Something { get; set; }
}

///
/// The Foo
///
/// A high Bar
///
/// Corgeous
///
public partial class Foo
{
public string Name { get; set; }
public string Something { get; set; }
}
}
```

I'm assuming this is deliberate, but can we have an option to just have a single class that is reused?
```
///
/// Demo of multiple classes from one definition.
///
public partial class DefDemo
{
///
/// A high Bar
///
public Corge Bar { get; set; }

///
/// Bazalicious
///
public Corge Baz { get; set; }

///
/// The Foo
///
public Corge Foo { get; set; }
}

///
/// Corgeous
///
public partial class Corge
{
public string Name { get; set; }
public string Something { get; set; }
}
```

As an aside, what's going on with the comments? There are descriptions from multiple properties appearing in multiple places.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.