Skip to content

Commit 8b8182b

Browse files
committed
updated to include that $ref is supported
1 parent 1564ed1 commit 8b8182b

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

_docs/schema/codegen/schema-codegen.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ public class MyObject
3333

3434
The code generation is currently quite basic. It will generate types for simple custom objects and any named array or dictionary type.
3535

36-
`$ref` is generally supported, even for recursive models like linked lists and binary trees.
36+
`$ref` is generally supported, even for recursive models like linked lists and binary trees, however there is no loop detection, so if you do something like this:
37+
38+
```json
39+
{
40+
"$ref": "#"
41+
}
42+
```
43+
44+
you'll just get a stack overflow exception. That's on you. Don't do that.
3745

3846
### Built-in types
3947

@@ -45,11 +53,13 @@ There is an exception to this behavior for arrays and dictionaries, which is exp
4553

4654
There is currently limited support for translating JSON Schema into code. However there is ongoing discussion for an official [JSON Schema code generation vocabulary](https://github.com/json-schema-org/vocab-idl). Please feel free to read up and join in on the effort there.
4755

56+
Currently, the class name is derived from the `title` keyword. There is an open issue in the repository above to discuss using this keyword. It's currently leaning toward the vocabulary defining a custom keyword, but as nothing has been decided yet, `title` is used here for now.
57+
4858
### Custom objects
4959

5060
Generating for custom objects is the real power behind code generation. Being able to read a schema produced by some other developer (e.g. from an OpenAPI document) and automatically create types can save developers a lot of time.
5161

52-
This library generates custom types for schemas that declare an `object` type and include `title` (for a type name) and `properties` without `additionalProperties`. It will also automatically generate types found nested in the schema. For example
62+
This library generates custom types for schemas that declare an `object` type and include `title` and `properties` without `additionalProperties`. It will also automatically generate types found nested in the schema. For example
5363

5464
```json
5565
{
@@ -85,10 +95,10 @@ public class Bar
8595

8696
There is some basic duplicate definition detection that serves two purposes:
8797

88-
1. It avoids creating multiple declarations for the same type. For example, if `Foo` had `Bar1` and `Bar2` properties, only one `Bar` declaration would be generated. (Again, `$ref` isn't supported yet, so the subschema would need to be repeated for `Bar1` and `Bar2`.)
98+
1. It avoids creating multiple declarations for the same type. For example, if `Foo` had `Bar1` and `Bar2` properties, only one `Bar` declaration would be generated. Ideally this kind of duplication should be defined in the schema using a `$ref`.
8999
2. It prevents creating multiple types with the same name. For example, if there are two subschemas with the same name that define two different types, an exception would be thrown indicating the name re-use.
90100

91-
Some basic string transformation occurs:
101+
For type and property naming, some basic string transformation occurs:
92102

93103
| Original | Transformed |
94104
|:-|:-|
@@ -113,7 +123,7 @@ When a schema declares an `array` type and includes an `items` keyword (in the s
113123
}
114124
```
115125

116-
produces no declaration, but using this schema appears as
126+
produces no declaration, but using this schema (e.g. to define object properties) appears as
117127

118128
```c#
119129
int[]
@@ -141,7 +151,7 @@ Dictionary<string, int>
141151

142152
### Including a name on basic types {#including-a-name}
143153

144-
When an array or dictionary schema has a `title` keyword, which supplies the name,
154+
When you have an array or dictionary schema with a `title` keyword
145155

146156
```json
147157
{
@@ -165,13 +175,13 @@ or
165175
}
166176
```
167177

168-
you now get a type declaration inheriting from `List<T>`:
178+
you now get a type declaration inheriting from `List<T>`
169179

170180
```c#
171181
public class MyIntArray : List<int> {}
172182
```
173183

174-
and
184+
and `Dictionary<TKey, TValue>`
175185

176186
```c#
177187
public class MyIntDictionary : Dictionary<string, int> {}

0 commit comments

Comments
 (0)