From 29ff7bc1fb992e40c1d6360374dc968f246f2794 Mon Sep 17 00:00:00 2001 From: Kostiantyn Shyrolapov Date: Wed, 15 May 2024 23:07:27 -0400 Subject: [PATCH] v0.2.1: show optional properties --- .gitignore | 1 + src/main.zig | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 62825e2..29691c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ openapi.json +types.js .env zig-cache zig-out diff --git a/src/main.zig b/src/main.zig index 2742501..d2409c5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -36,7 +36,15 @@ fn build_jsdoc_typedef(allocator: std.mem.Allocator, schema_key: []const u8, sch const type_ = try get_jsdoc_type(allocator, value, ""); try output.append(type_); try output.append("} "); - try output.append(key); + + if (is_property_required(schema_object, key)) { + try output.append(key); + } else { + try output.append("["); + try output.append(key); + try output.append("]"); + } + try output.append("\n"); } try output.append(" */\n"); @@ -47,6 +55,17 @@ fn build_jsdoc_typedef(allocator: std.mem.Allocator, schema_key: []const u8, sch return output_str; } +fn is_property_required(schema_object: std.json.Value, property_key: []const u8) bool { + const required = schema_object.object.get("required") orelse return false; + + for (required.array.items) |required_key| { + if (std.mem.eql(u8, required_key.string, property_key)) { + return true; + } + } + return false; +} + fn get_jsdoc_type(allocator: std.mem.Allocator, value: std.json.Value, array_suffix: []const u8) ![]const u8 { if (value.object.get("enum")) |enum_| { const enum_values = enum_.array.items;