Skip to content

Commit 94e9601

Browse files
authored
feat: add json schema input processor (#261)
1 parent 8d0ed78 commit 94e9601

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1315
-95
lines changed

docs/generators/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ All available generators, across languages and inputs:
2020

2121
| **Inputs** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`headers`](./headers.md) | [`types`](./types.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`models`](./models.md) | [`custom`](./custom.md) |
2222
|---|---|---|---|---|---|---|---|---|
23-
| AsyncAPI | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
24-
| OpenAPI | ✔️ | ✔️ | ✔️ | ✔️ ||| ✔️ | ✔️ |
23+
| AsyncAPI |||||||||
24+
| OpenAPI |||||||||
25+
| JSON Schema |||||||||
2526

2627
| **Languages** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`headers`](./headers.md) | [`types`](./types.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`models`](./models.md) | [`custom`](./custom.md) |
2728
|---|---|---|---|---|---|---|---|---|
28-
| TypeScript | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
29+
| TypeScript | | | | | | | | |

docs/generators/models.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export default {
1919
};
2020
```
2121

22-
The `models` preset provides native integration with [AsyncAPI Modelina](https://modelina.org) for generating TypeScript models directly from AsyncAPI and OpenAPI documents. This generator exposes Modelina's full capabilities, giving you complete control over model generation.
22+
The `models` preset provides native integration with [AsyncAPI Modelina](https://modelina.org) for generating TypeScript models directly from AsyncAPI, OpenAPI, and JSON Schema documents. This generator exposes Modelina's full capabilities, giving you complete control over model generation.
2323

24-
This is supported through the following inputs: `asyncapi`, `openapi`
24+
This is supported through the following inputs: `asyncapi`, `openapi`, `jsonschema`
2525

2626
It supports the following languages; [`typescript`](#typescript)
2727

@@ -161,6 +161,35 @@ export default {
161161
};
162162
```
163163

164+
### JSON Schema Input
165+
166+
```js
167+
export default {
168+
inputType: 'jsonschema',
169+
inputPath: 'user-schema.json',
170+
language: 'typescript',
171+
generators: [
172+
{
173+
preset: 'models',
174+
options: {
175+
modelType: 'class',
176+
enumType: 'enum'
177+
},
178+
renderers: [
179+
{
180+
class: {
181+
additionalContent: ({ content, model }) => {
182+
return `${content}\n\n // Custom validation method\n public validate(): boolean {\n return true;\n }`;
183+
}
184+
}
185+
}
186+
],
187+
outputPath: './src/models'
188+
}
189+
]
190+
};
191+
```
192+
164193
## Languages
165194

166195
### TypeScript

docs/generators/payloads.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Each language has a set of constraints which means that some typed model types a
2929

3030
| | Circular models | Enums | Tuples | Arrays | Nested Arrays | Dictionaries | Json Serialization | Validation |
3131
|---|---|---|---|---|---|---|---|---|
32-
| **TypeScript** | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
32+
| **TypeScript** | | | | | | | | |
3333

3434
### TypeScript
3535

docs/inputs/asyncapi.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ There is a lot of overlap with existing tooling, however the idea is to form the
1717

1818
| **Presets** | AsyncAPI |
1919
|---|---|
20-
| [`payloads`](../generators/payloads.md) | ✔️ |
21-
| [`parameters`](../generators/parameters.md) | ✔️ |
22-
| [`headers`](../generators/headers.md) | ✔️ |
23-
| [`types`](../generators/types.md) | ✔️ |
24-
| [`channels`](../generators/channels.md) | ✔️ |
25-
| [`client`](../generators/client.md) | ✔️ |
26-
| [`custom`](../generators/custom.md) | ✔️ |
20+
| [`payloads`](../generators/payloads.md) | |
21+
| [`parameters`](../generators/parameters.md) | |
22+
| [`headers`](../generators/headers.md) | |
23+
| [`types`](../generators/types.md) | |
24+
| [`channels`](../generators/channels.md) | |
25+
| [`client`](../generators/client.md) | |
26+
| [`custom`](../generators/custom.md) | |
2727

2828
## Basic AsyncAPI Document Structure
2929

docs/inputs/jsonschema.md

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# JSON Schema
6+
7+
JSON Schema input support enables you to generate TypeScript models directly from JSON Schema documents. This is particularly useful when you have standalone JSON Schema files that define your data structures.
8+
9+
## Supported Generators
10+
11+
The JSON Schema input type supports the following generators:
12+
13+
| Preset | JSON Schema |
14+
|-----------|---------|
15+
| [`models`](../generators/models.md) ||
16+
| [`custom`](../generators/custom.md) ||
17+
18+
## Configuration
19+
20+
### Basic Configuration
21+
22+
```js
23+
export default {
24+
inputType: 'jsonschema',
25+
inputPath: './user-schema.json',
26+
language: 'typescript',
27+
generators: [
28+
{
29+
preset: 'models',
30+
outputPath: './src/models'
31+
}
32+
]
33+
};
34+
```
35+
36+
### Advanced Configuration with Modelina Options
37+
38+
```js
39+
export default {
40+
inputType: 'jsonschema',
41+
inputPath: './complex-schema.json',
42+
language: 'typescript',
43+
generators: [
44+
{
45+
preset: 'models',
46+
outputPath: './src/models',
47+
options: {
48+
modelType: 'class',
49+
enumType: 'enum',
50+
mapType: 'record',
51+
rawPropertyNames: false,
52+
useJavascriptReservedKeywords: false
53+
},
54+
renderers: [
55+
{
56+
class: {
57+
property: ({ content, property }) => {
58+
return `/** ${property.property.description || 'Auto-generated property'} */\n${content}`;
59+
}
60+
}
61+
}
62+
]
63+
}
64+
]
65+
};
66+
```
67+
68+
## Examples
69+
70+
### Simple User Schema
71+
72+
**Input: `user-schema.json`**
73+
```json
74+
{
75+
"$schema": "http://json-schema.org/draft-07/schema#",
76+
"title": "User",
77+
"type": "object",
78+
"properties": {
79+
"id": {
80+
"type": "string",
81+
"format": "uuid"
82+
},
83+
"name": {
84+
"type": "string"
85+
},
86+
"email": {
87+
"type": "string",
88+
"format": "email"
89+
},
90+
"age": {
91+
"type": "integer",
92+
"minimum": 0
93+
}
94+
},
95+
"required": ["id", "name", "email"]
96+
}
97+
```
98+
99+
**Configuration: `codegen.mjs`**
100+
```js
101+
export default {
102+
inputType: 'jsonschema',
103+
inputPath: './user-schema.json',
104+
language: 'typescript',
105+
generators: [
106+
{
107+
preset: 'models',
108+
outputPath: './src/models'
109+
}
110+
]
111+
};
112+
```
113+
114+
**Generated Output: `src/models/User.ts`**
115+
```typescript
116+
export class User {
117+
private _id: string;
118+
private _name: string;
119+
private _email: string;
120+
private _age?: number;
121+
122+
constructor(input: {
123+
id: string,
124+
name: string,
125+
email: string,
126+
age?: number,
127+
}) {
128+
this._id = input.id;
129+
this._name = input.name;
130+
this._email = input.email;
131+
this._age = input.age;
132+
}
133+
134+
get id(): string { return this._id; }
135+
get name(): string { return this._name; }
136+
get email(): string { return this._email; }
137+
get age(): number | undefined { return this._age; }
138+
139+
public marshal(): string {
140+
return JSON.stringify({
141+
id: this.id,
142+
name: this.name,
143+
email: this.email,
144+
age: this.age,
145+
});
146+
}
147+
148+
public static unmarshal(json: string): User {
149+
const obj = JSON.parse(json);
150+
return new User(obj);
151+
}
152+
}
153+
```
154+
155+
### Complex Schema with Definitions
156+
157+
**Input: `complex-schema.json`**
158+
```json
159+
{
160+
"$schema": "http://json-schema.org/draft-07/schema#",
161+
"definitions": {
162+
"Address": {
163+
"type": "object",
164+
"properties": {
165+
"street": { "type": "string" },
166+
"city": { "type": "string" },
167+
"zipCode": { "type": "string" }
168+
},
169+
"required": ["street", "city", "zipCode"]
170+
}
171+
},
172+
"type": "object",
173+
"properties": {
174+
"person": {
175+
"type": "object",
176+
"properties": {
177+
"name": { "type": "string" },
178+
"address": { "$ref": "#/definitions/Address" }
179+
},
180+
"required": ["name"]
181+
}
182+
}
183+
}
184+
```
185+
186+
This will generate both `Person` and `Address` classes with proper type relationships.
187+
188+
## File Format Support
189+
190+
### JSON Format
191+
```json
192+
{
193+
"$schema": "http://json-schema.org/draft-07/schema#",
194+
"type": "object",
195+
"properties": {
196+
"example": { "type": "string" }
197+
}
198+
}
199+
```
200+
201+
### YAML Format
202+
```yaml
203+
$schema: "http://json-schema.org/draft-07/schema#"
204+
type: object
205+
properties:
206+
example:
207+
type: string
208+
```

docs/inputs/openapi.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ Input support; `openapi`
1212

1313
| **Presets** | OpenAPI |
1414
|---|---|
15-
| [`payloads`](../generators/payloads.md) | ✔️ |
16-
| [`parameters`](../generators/parameters.md) | ✔️ |
17-
| [`headers`](../generators/headers.md) | ✔️ |
18-
| [`types`](../generators/types.md) | ✔️ |
19-
| [`channels`](../generators/channels.md) | |
20-
| [`client`](../generators/client.md) | |
21-
| [`custom`](../generators/custom.md) | ✔️ |
15+
| [`payloads`](../generators/payloads.md) | |
16+
| [`parameters`](../generators/parameters.md) | |
17+
| [`headers`](../generators/headers.md) | |
18+
| [`types`](../generators/types.md) | |
19+
| [`channels`](../generators/channels.md) | |
20+
| [`client`](../generators/client.md) | |
21+
| [`custom`](../generators/custom.md) | |
2222

2323
## Basic Usage
2424

docs/protocols/amqp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar_position: 99
77

88
| **Languages** | Publish exchange | Publish queue | Subscribe queue | Subscribe exchange |
99
|---|---|---|---|---|
10-
| TypeScript | ✔️ | ✔️ | ✔️ | |
10+
| TypeScript | | | | |
1111

1212
All of this is available through [AsyncAPI](../inputs/asyncapi.md).
1313

docs/protocols/eventsource.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ sidebar_position: 99
77

88
| **Languages** | [client](#client) | [server](#server) |
99
|---|---|---|
10-
| TypeScript | ✔️ | ✔️ |
10+
| TypeScript | | |
1111

1212
All of this is available through [AsyncAPI](../inputs/asyncapi.md).
1313

docs/protocols/http_client.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@ All of this is available through [AsyncAPI](../inputs/asyncapi.md). Require HTTP
1414

1515
| **Feature** | Is supported? |
1616
|---|---|
17-
| Download | |
18-
| Upload | |
19-
| Offset based Pagination | |
20-
| Cursor based Pagination | |
21-
| Page based Pagination | |
22-
| Time based Pagination | |
23-
| Keyset based Pagination | |
24-
| Retry with backoff | |
25-
| OAuth2 Authorization code | ✔️ |
26-
| OAuth2 Implicit | ✔️ |
27-
| OAuth2 password | ✔️ |
28-
| OAuth2 Client Credentials | ✔️ |
29-
| Username/password Authentication | ✔️ |
30-
| Bearer Authentication | ✔️ |
31-
| Basic Authentication | ✔️ |
32-
| API Key Authentication | ✔️ |
33-
| XML Based API | |
34-
| JSON Based API | ✔️ |
35-
| POST | ✔️ |
36-
| GET | ✔️ |
37-
| PATCH | ✔️ |
38-
| DELETE | ✔️ |
39-
| PUT | ✔️ |
40-
| HEAD | ✔️ |
41-
| OPTIONS | ✔️ |
17+
| Download | |
18+
| Upload | |
19+
| Offset based Pagination | |
20+
| Cursor based Pagination | |
21+
| Page based Pagination | |
22+
| Time based Pagination | |
23+
| Keyset based Pagination | |
24+
| Retry with backoff | |
25+
| OAuth2 Authorization code | |
26+
| OAuth2 Implicit | |
27+
| OAuth2 password | |
28+
| OAuth2 Client Credentials | |
29+
| Username/password Authentication | |
30+
| Bearer Authentication | |
31+
| Basic Authentication | |
32+
| API Key Authentication | |
33+
| XML Based API | |
34+
| JSON Based API | |
35+
| POST | |
36+
| GET | |
37+
| PATCH | |
38+
| DELETE | |
39+
| PUT | |
40+
| HEAD | |
41+
| OPTIONS | |

0 commit comments

Comments
 (0)