From 88c77deede1bb174759b4971b7e4ad8e7dcc4ba9 Mon Sep 17 00:00:00 2001 From: Alex Groleau Date: Mon, 1 Sep 2025 22:58:01 -0400 Subject: [PATCH] punc request and response moved to punc schemas --- src/lib.rs | 57 ++++------------- src/schemas.rs | 168 ++++++++++++++++++++++++++++--------------------- 2 files changed, 111 insertions(+), 114 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bdfe185..0a8e7cd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -140,15 +140,24 @@ fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB { .unwrap_or(false); let punc_schema_type = if is_public { SchemaType::PublicPunc } else { SchemaType::PrivatePunc }; - // Add punc local schemas as resources (from schemas field) - use $id directly (universal) + // Add punc schemas from the 'schemas' array if let Some(schemas_raw) = punc_obj.get("schemas") { if let Some(schemas_array) = schemas_raw.as_array() { for schema_def in schemas_array { if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) { - if let Err(e) = add_schema_resource(&mut compiler, schema_id, schema_def.clone(), SchemaType::Type, &mut errors) { + let request_schema_id = format!("{}.request", punc_name); + let response_schema_id = format!("{}.response", punc_name); + + let schema_type_for_def = if schema_id == request_schema_id || schema_id == response_schema_id { + punc_schema_type + } else { + SchemaType::Type // For local/nested schemas + }; + + if let Err(e) = add_schema_resource(&mut compiler, schema_id, schema_def.clone(), schema_type_for_def, &mut errors) { errors.push(json!({ - "code": "PUNC_LOCAL_SCHEMA_RESOURCE_FAILED", - "message": format!("Failed to add local schema resource '{}' for punc '{}'", schema_id, punc_name), + "code": "PUNC_SCHEMA_RESOURCE_FAILED", + "message": format!("Failed to add schema resource '{}' for punc '{}'", schema_id, punc_name), "details": { "punc_name": punc_name, "schema_id": schema_id, @@ -162,46 +171,6 @@ fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB { } } } - - // Add request schema as resource if present - use {punc_name}.request - if let Some(request_schema) = punc_obj.get("request") { - if !request_schema.is_null() { - let request_schema_id = format!("{}.request", punc_name); - if let Err(e) = add_schema_resource(&mut compiler, &request_schema_id, request_schema.clone(), punc_schema_type, &mut errors) { - errors.push(json!({ - "code": "PUNC_REQUEST_SCHEMA_RESOURCE_FAILED", - "message": format!("Failed to add request schema resource for punc '{}'", punc_name), - "details": { - "punc_name": punc_name, - "schema_id": request_schema_id, - "cause": format!("{}", e) - } - })); - } else { - all_schema_ids.push(request_schema_id); - } - } - } - - // Add response schema as resource if present - use {punc_name}.response - if let Some(response_schema) = punc_obj.get("response") { - if !response_schema.is_null() { - let response_schema_id = format!("{}.response", punc_name); - if let Err(e) = add_schema_resource(&mut compiler, &response_schema_id, response_schema.clone(), punc_schema_type, &mut errors) { - errors.push(json!({ - "code": "PUNC_RESPONSE_SCHEMA_RESOURCE_FAILED", - "message": format!("Failed to add response schema resource for punc '{}'", punc_name), - "details": { - "punc_name": punc_name, - "schema_id": response_schema_id, - "cause": format!("{}", e) - } - })); - } else { - all_schema_ids.push(response_schema_id); - } - } - } } } } diff --git a/src/schemas.rs b/src/schemas.rs index 05d06b9..3bbb30a 100644 --- a/src/schemas.rs +++ b/src/schemas.rs @@ -13,14 +13,15 @@ pub fn simple_schemas() -> JsonB { let puncs = json!([{ "name": "simple", "public": false, - "request": { + "schemas": [{ + "$id": "simple.request", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer", "minimum": 0 } }, "required": ["name", "age"] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -32,10 +33,10 @@ pub fn invalid_schemas() -> JsonB { let puncs = json!([{ "name": "invalid_punc", "public": false, - "request": { - "$id": "urn:invalid_schema", + "schemas": [{ + "$id": "invalid_punc.request", "type": ["invalid_type_value"] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -47,7 +48,8 @@ pub fn errors_schemas() -> JsonB { let puncs = json!([{ "name": "detailed_errors_test", "public": false, - "request": { + "schemas": [{ + "$id": "detailed_errors_test.request", "type": "object", "properties": { "address": { @@ -60,7 +62,7 @@ pub fn errors_schemas() -> JsonB { } }, "required": ["address"] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -72,7 +74,8 @@ pub fn oneof_schemas() -> JsonB { let puncs = json!([{ "name": "oneof_test", "public": false, - "request": { + "schemas": [{ + "$id": "oneof_test.request", "oneOf": [ { "type": "object", @@ -89,7 +92,7 @@ pub fn oneof_schemas() -> JsonB { "required": ["number_prop"] } ] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -102,19 +105,21 @@ pub fn root_types_schemas() -> JsonB { { "name": "object_test", "public": false, - "request": { + "schemas": [{ + "$id": "object_test.request", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer", "minimum": 0 } }, "required": ["name", "age"] - } + }] }, { "name": "array_test", "public": false, - "request": { + "schemas": [{ + "$id": "array_test.request", "type": "array", "items": { "type": "object", @@ -122,7 +127,7 @@ pub fn root_types_schemas() -> JsonB { "id": { "type": "string", "format": "uuid" } } } - } + }] } ]); @@ -136,27 +141,30 @@ pub fn strict_schemas() -> JsonB { { "name": "basic_strict_test", "public": true, - "request": { + "schemas": [{ + "$id": "basic_strict_test.request", "type": "object", "properties": { "name": { "type": "string" } } - } + }] }, { "name": "non_strict_test", "public": false, - "request": { + "schemas": [{ + "$id": "non_strict_test.request", "type": "object", "properties": { "name": { "type": "string" } } - } + }] }, { "name": "nested_strict_test", "public": true, - "request": { + "schemas": [{ + "$id": "nested_strict_test.request", "type": "object", "properties": { "user": { @@ -175,34 +183,37 @@ pub fn strict_schemas() -> JsonB { } } } - } + }] }, { "name": "already_unevaluated_test", "public": true, - "request": { + "schemas": [{ + "$id": "already_unevaluated_test.request", "type": "object", "properties": { "name": { "type": "string" } }, "unevaluatedProperties": true - } + }] }, { "name": "already_additional_test", "public": true, - "request": { + "schemas": [{ + "$id": "already_additional_test.request", "type": "object", "properties": { "name": { "type": "string" } }, "additionalProperties": false - } + }] }, { "name": "conditional_strict_test", "public": true, - "request": { + "schemas": [{ + "$id": "conditional_strict_test.request", "type": "object", "properties": { "creating": { "type": "boolean" } @@ -218,7 +229,7 @@ pub fn strict_schemas() -> JsonB { }, "required": ["name"] } - } + }] } ]); @@ -231,14 +242,15 @@ pub fn required_schemas() -> JsonB { let puncs = json!([{ "name": "basic_validation_test", "public": false, - "request": { + "schemas": [{ + "$id": "basic_validation_test.request", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer", "minimum": 0 } }, "required": ["name", "age"] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -250,7 +262,8 @@ pub fn dependencies_schemas() -> JsonB { let puncs = json!([{ "name": "dependency_split_test", "public": false, - "request": { + "schemas": [{ + "$id": "dependency_split_test.request", "type": "object", "properties": { "creating": { "type": "boolean" }, @@ -261,7 +274,7 @@ pub fn dependencies_schemas() -> JsonB { "dependencies": { "creating": ["name", "kind"] } - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -273,7 +286,8 @@ pub fn nested_req_deps_schemas() -> JsonB { let puncs = json!([{ "name": "nested_dep_test", "public": false, - "request": { + "schemas": [{ + "$id": "nested_dep_test.request", "type": "object", "properties": { "items": { @@ -294,7 +308,7 @@ pub fn nested_req_deps_schemas() -> JsonB { } }, "required": ["items"] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -307,19 +321,21 @@ pub fn additional_properties_schemas() -> JsonB { { "name": "additional_props_test", "public": false, - "request": { + "schemas": [{ + "$id": "additional_props_test.request", "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "number" } }, "additionalProperties": false - } + }] }, { "name": "nested_additional_props_test", "public": false, - "request": { + "schemas": [{ + "$id": "nested_additional_props_test.request", "type": "object", "properties": { "user": { @@ -330,7 +346,7 @@ pub fn additional_properties_schemas() -> JsonB { "additionalProperties": false } } - } + }] } ]); @@ -344,7 +360,8 @@ pub fn unevaluated_properties_schemas() -> JsonB { { "name": "simple_unevaluated_test", "public": false, - "request": { + "schemas": [{ + "$id": "simple_unevaluated_test.request", "type": "object", "properties": { "name": { "type": "string" }, @@ -354,12 +371,13 @@ pub fn unevaluated_properties_schemas() -> JsonB { "^attr_": { "type": "string" } }, "unevaluatedProperties": false - } + }] }, { "name": "conditional_unevaluated_test", "public": false, - "request": { + "schemas": [{ + "$id": "conditional_unevaluated_test.request", "type": "object", "allOf": [ { @@ -377,7 +395,7 @@ pub fn unevaluated_properties_schemas() -> JsonB { "age": { "type": "number" } }, "unevaluatedProperties": false - } + }] } ]); @@ -390,14 +408,15 @@ pub fn format_schemas() -> JsonB { let puncs = json!([{ "name": "format_test", "public": false, - "request": { + "schemas": [{ + "$id": "format_test.request", "type": "object", "properties": { "uuid": { "type": "string", "format": "uuid" }, "date_time": { "type": "string", "format": "date-time" }, "email": { "type": "string", "format": "email" } } - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -602,16 +621,18 @@ pub fn punc_with_refs_schemas() -> JsonB { { "name": "public_ref_test", "public": true, - "request": { + "schemas": [{ + "$id": "public_ref_test.request", "$ref": "person" - } + }] }, { "name": "private_ref_test", "public": false, - "request": { + "schemas": [{ + "$id": "private_ref_test.request", "$ref": "person" - } + }] } ]); @@ -636,13 +657,14 @@ pub fn enum_schemas() -> JsonB { let puncs = json!([{ "name": "enum_ref_test", "public": false, - "request": { + "schemas": [{ + "$id": "enum_ref_test.request", "type": "object", "properties": { "priority": { "$ref": "task_priority" } }, "required": ["priority"] - } + }] }]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) @@ -669,34 +691,40 @@ pub fn punc_local_refs_schemas() -> JsonB { { "name": "punc_with_local_ref_test", "public": false, - "schemas": [{ - "$id": "local_address", - "type": "object", - "properties": { - "street": { "type": "string" }, - "city": { "type": "string" } + "schemas": [ + { + "$id": "local_address", + "type": "object", + "properties": { + "street": { "type": "string" }, + "city": { "type": "string" } + }, + "required": ["street", "city"] }, - "required": ["street", "city"] - }], - "request": { - "$ref": "local_address" - } + { + "$id": "punc_with_local_ref_test.request", + "$ref": "local_address" + } + ] }, { "name": "punc_with_local_ref_to_global_test", "public": false, - "schemas": [{ - "$id": "local_user_with_thing", - "type": "object", - "properties": { - "user_name": { "type": "string" }, - "thing": { "$ref": "global_thing" } + "schemas": [ + { + "$id": "local_user_with_thing", + "type": "object", + "properties": { + "user_name": { "type": "string" }, + "thing": { "$ref": "global_thing" } + }, + "required": ["user_name", "thing"] }, - "required": ["user_name", "thing"] - }], - "request": { - "$ref": "local_user_with_thing" - } + { + "$id": "punc_with_local_ref_to_global_test.request", + "$ref": "local_user_with_thing" + } + ] } ]); @@ -732,4 +760,4 @@ pub fn title_override_schemas() -> JsonB { let puncs = json!([]); cache_json_schemas(jsonb(enums), jsonb(types), jsonb(puncs)) -} \ No newline at end of file +}