Generate a Java record for a JSON Schema object type. Returns the class content.
(
className: string,
schema: JSONSchema7,
_nestedTypes: Map<string, { code: string }>,
_packageName: string,
visibility: "public" | "internal" = "public"
)
| 1259 | |
| 1260 | /** Generate a Java record for a JSON Schema object type. Returns the class content. */ |
| 1261 | function generateRpcClass( |
| 1262 | className: string, |
| 1263 | schema: JSONSchema7, |
| 1264 | _nestedTypes: Map<string, { code: string }>, |
| 1265 | _packageName: string, |
| 1266 | visibility: "public" | "internal" = "public" |
| 1267 | ): { code: string; imports: Set<string> } { |
| 1268 | const imports = new Set<string>(); |
| 1269 | const localNestedTypes = new Map<string, JavaClassDef>(); |
| 1270 | const lines: string[] = []; |
| 1271 | const visModifier = visibility === "public" ? "public " : ""; |
| 1272 | |
| 1273 | const properties = Object.entries(schema.properties || {}); |
| 1274 | const fields = properties.flatMap(([propName, propSchema]) => { |
| 1275 | if (typeof propSchema !== "object") return []; |
| 1276 | const prop = propSchema as JSONSchema7; |
| 1277 | // Record components are always boxed (nullable by design). |
| 1278 | const result = schemaTypeToJava(prop, false, className, propName, localNestedTypes); |
| 1279 | for (const imp of result.imports) imports.add(imp); |
| 1280 | return [{ propName, javaName: toCamelCase(propName), javaType: result.javaType, description: prop.description }]; |
| 1281 | }); |
| 1282 | |
| 1283 | lines.push(`@JsonInclude(JsonInclude.Include.NON_NULL)`); |
| 1284 | lines.push(`@JsonIgnoreProperties(ignoreUnknown = true)`); |
| 1285 | if (fields.length === 0) { |
| 1286 | lines.push(`${visModifier}record ${className}() {`); |
| 1287 | } else { |
| 1288 | lines.push(`${visModifier}record ${className}(`); |
| 1289 | for (let i = 0; i < fields.length; i++) { |
| 1290 | const f = fields[i]; |
| 1291 | const comma = i < fields.length - 1 ? "," : ""; |
| 1292 | if (f.description) { |
| 1293 | lines.push(` /** ${f.description} */`); |
| 1294 | } |
| 1295 | lines.push(` @JsonProperty("${f.propName}") ${f.javaType} ${f.javaName}${comma}`); |
| 1296 | } |
| 1297 | lines.push(`) {`); |
| 1298 | } |
| 1299 | |
| 1300 | // Add nested types as nested records/enums inside this record |
| 1301 | for (const [, nested] of localNestedTypes) { |
| 1302 | lines.push(...renderNestedType(nested, 1, new Map(), imports)); |
| 1303 | } |
| 1304 | |
| 1305 | if (localNestedTypes.size > 0 && lines[lines.length - 1] === "") lines.pop(); |
| 1306 | lines.push(`}`); |
| 1307 | |
| 1308 | return { code: lines.join("\n"), imports }; |
| 1309 | } |
| 1310 | |
| 1311 | async function generateRpcTypes(schemaPath: string): Promise<void> { |
| 1312 | console.log("\n🔌 Generating RPC types..."); |
no test coverage detected
searching dependent graphs…