From 045c7aaff26737c722a172836e73ec324c7db6d9 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Mon, 13 Apr 2026 17:42:19 +0200 Subject: [PATCH] Add description of type definitions If a type definition has a doc domment, it wasn't included as description of that type. Example: ``` /// A forum thread record thread { title!: title, } /// Thread title def type title = string constrained { maxLength: 200, minLength: 1, }; ``` Would lead to just ```json "title": { "type": "string", "maxLength": 200, "minLength": 1 } ``` With this commit it contains the `Thread title` as description: ``` "title": { "type": "string", "description": "Thread title", "maxLength": 200, "minLength": 1 } --- mlf-codegen/src/lib.rs | 9 +++++- .../lexicon/doc_comments/expected.json | 30 +++++++++++++++++++ tests/codegen/lexicon/doc_comments/input.mlf | 10 +++++++ tests/codegen/lexicon/doc_comments/test.toml | 4 +++ 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/codegen/lexicon/doc_comments/expected.json create mode 100644 tests/codegen/lexicon/doc_comments/input.mlf create mode 100644 tests/codegen/lexicon/doc_comments/test.toml diff --git a/mlf-codegen/src/lib.rs b/mlf-codegen/src/lib.rs index 192e5e9..6c2aff8 100644 --- a/mlf-codegen/src/lib.rs +++ b/mlf-codegen/src/lib.rs @@ -586,7 +586,14 @@ fn generate_subscription_json( } fn generate_def_type_json(def_type: &DefType, usage_counts: &HashMap, workspace: &Workspace, current_namespace: &str) -> Value { - generate_type_json(&def_type.ty, usage_counts, workspace, current_namespace) + let mut field_json = generate_type_json(&def_type.ty, usage_counts, workspace, current_namespace); + // Add description if the type definition has doc comments + if !def_type.docs.is_empty() { + if let Some(obj) = field_json.as_object_mut() { + obj.shift_insert(1, "description".to_string(), json!(extract_docs(&def_type.docs))); + } + } + field_json } fn generate_type_json(ty: &Type, usage_counts: &HashMap, workspace: &Workspace, current_namespace: &str) -> Value { diff --git a/tests/codegen/lexicon/doc_comments/expected.json b/tests/codegen/lexicon/doc_comments/expected.json new file mode 100644 index 0000000..676c8c4 --- /dev/null +++ b/tests/codegen/lexicon/doc_comments/expected.json @@ -0,0 +1,30 @@ +{ + "$type": "com.atproto.lexicon.schema", + "lexicon": 1, + "id": "com.example.thread", + "defs": { + "main": { + "type": "record", + "description": "A forum thread", + "key": "tid", + "record": { + "type": "object", + "required": [ + "title" + ], + "properties": { + "title": { + "type": "ref", + "ref": "#title" + } + } + } + }, + "title": { + "type": "string", + "description": "Thread title", + "maxLength": 200, + "minLength": 1 + } + } +} diff --git a/tests/codegen/lexicon/doc_comments/input.mlf b/tests/codegen/lexicon/doc_comments/input.mlf new file mode 100644 index 0000000..8ca195b --- /dev/null +++ b/tests/codegen/lexicon/doc_comments/input.mlf @@ -0,0 +1,10 @@ +/// A forum thread +record thread { + title!: title, +} + +/// Thread title +def type title = string constrained { + maxLength: 200, + minLength: 1, +}; diff --git a/tests/codegen/lexicon/doc_comments/test.toml b/tests/codegen/lexicon/doc_comments/test.toml new file mode 100644 index 0000000..68ec5ce --- /dev/null +++ b/tests/codegen/lexicon/doc_comments/test.toml @@ -0,0 +1,4 @@ +[test] +name = "doc_comments" +description = "Test doc comments for defs" +namespace = "com.example.thread" -- 2.51.2