You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _docs/schema/codegen/schema-codegen.md
+18-8
Original file line number
Diff line number
Diff line change
@@ -33,7 +33,15 @@ public class MyObject
33
33
34
34
The code generation is currently quite basic. It will generate types for simple custom objects and any named array or dictionary type.
35
35
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.
37
45
38
46
### Built-in types
39
47
@@ -45,11 +53,13 @@ There is an exception to this behavior for arrays and dictionaries, which is exp
45
53
46
54
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.
47
55
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
+
48
58
### Custom objects
49
59
50
60
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.
51
61
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
53
63
54
64
```json
55
65
{
@@ -85,10 +95,10 @@ public class Bar
85
95
86
96
There is some basic duplicate definition detection that serves two purposes:
87
97
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`.
89
99
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.
90
100
91
-
Some basic string transformation occurs:
101
+
For type and property naming, some basic string transformation occurs:
92
102
93
103
| Original | Transformed |
94
104
|:-|:-|
@@ -113,7 +123,7 @@ When a schema declares an `array` type and includes an `items` keyword (in the s
113
123
}
114
124
```
115
125
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
117
127
118
128
```c#
119
129
int[]
@@ -141,7 +151,7 @@ Dictionary<string, int>
141
151
142
152
### Including a name on basic types {#including-a-name}
143
153
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
145
155
146
156
```json
147
157
{
@@ -165,13 +175,13 @@ or
165
175
}
166
176
```
167
177
168
-
you now get a type declaration inheriting from `List<T>`:
178
+
you now get a type declaration inheriting from `List<T>`
0 commit comments