|
| 1 | +use proc_macro2::Span; |
| 2 | +use vespera_core::openapi::OpenApi; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + error::MacroResult, |
| 6 | + metadata::{CollectedMetadata, StructMetadata}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Tracks component-schema definitions and their source while exported apps |
| 10 | +/// are folded into a parent document. |
| 11 | +pub(super) struct SchemaMergeGuard { |
| 12 | + metadata: CollectedMetadata, |
| 13 | +} |
| 14 | + |
| 15 | +impl SchemaMergeGuard { |
| 16 | + pub(super) fn new(parent: &OpenApi) -> MacroResult<Self> { |
| 17 | + let mut guard = Self { |
| 18 | + metadata: CollectedMetadata::new(), |
| 19 | + }; |
| 20 | + guard.record(parent, "the parent app", Span::call_site())?; |
| 21 | + Ok(guard) |
| 22 | + } |
| 23 | + |
| 24 | + /// Reject a child whose component name is already attached to a different |
| 25 | + /// JSON Schema. Equal definitions are intentionally retained as normal |
| 26 | + /// deduplication. |
| 27 | + pub(super) fn check_child( |
| 28 | + &mut self, |
| 29 | + child: &OpenApi, |
| 30 | + child_origin: &str, |
| 31 | + span: Span, |
| 32 | + ) -> MacroResult<()> { |
| 33 | + self.record(child, child_origin, span) |
| 34 | + } |
| 35 | + |
| 36 | + fn record(&mut self, document: &OpenApi, origin: &str, span: Span) -> MacroResult<()> { |
| 37 | + let Some(schemas) = document |
| 38 | + .components |
| 39 | + .as_ref() |
| 40 | + .and_then(|components| components.schemas.as_ref()) |
| 41 | + else { |
| 42 | + return Ok(()); |
| 43 | + }; |
| 44 | + |
| 45 | + for (name, schema) in schemas { |
| 46 | + let definition = serde_json::to_string(schema).map_err(|error| { |
| 47 | + syn::Error::new( |
| 48 | + span, |
| 49 | + format!( |
| 50 | + "OpenAPI merge: failed to compare schema `{name}` from {origin}. Error: {error}." |
| 51 | + ), |
| 52 | + ) |
| 53 | + })?; |
| 54 | + |
| 55 | + self.metadata.structs.push( |
| 56 | + StructMetadata::new(name.clone(), definition) |
| 57 | + .with_source_identity(origin.to_string()), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + self.metadata |
| 62 | + .check_duplicate_schema_names() |
| 63 | + .map_err(|message| syn::Error::new(span, format!("OpenAPI merge: {message}"))) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(test)] |
| 68 | +mod tests { |
| 69 | + use super::*; |
| 70 | + |
| 71 | + fn document(schema: &serde_json::Value) -> OpenApi { |
| 72 | + serde_json::from_value(serde_json::json!({ |
| 73 | + "openapi": "3.1.0", |
| 74 | + "info": { "title": "test", "version": "1.0.0" }, |
| 75 | + "paths": {}, |
| 76 | + "components": { "schemas": { "ExampleItem": schema } } |
| 77 | + })) |
| 78 | + .unwrap() |
| 79 | + } |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn different_same_named_schemas_are_rejected_with_both_origins() { |
| 83 | + let parent = document(&serde_json::json!({ |
| 84 | + "type": "object", |
| 85 | + "properties": { "id": { "type": "string" } } |
| 86 | + })); |
| 87 | + let child = document(&serde_json::json!({ |
| 88 | + "type": "object", |
| 89 | + "properties": { "collisionMarker": { "type": "boolean" } } |
| 90 | + })); |
| 91 | + let mut guard = SchemaMergeGuard::new(&parent).unwrap(); |
| 92 | + |
| 93 | + let error = guard |
| 94 | + .check_child(&child, "merged app `plugin_b::PluginB`", Span::call_site()) |
| 95 | + .expect_err("different definitions must fail"); |
| 96 | + let message = error.to_string(); |
| 97 | + |
| 98 | + assert!(message.contains("Duplicate OpenAPI schema name 'ExampleItem'")); |
| 99 | + assert!(message.contains("plugin_b::PluginB")); |
| 100 | + assert!(message.contains("parent app")); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn identical_same_named_schemas_are_accepted() { |
| 105 | + let schema = serde_json::json!({ |
| 106 | + "type": "object", |
| 107 | + "properties": { |
| 108 | + "error": { "type": "string" }, |
| 109 | + "code": { "type": "integer" } |
| 110 | + }, |
| 111 | + "required": ["error", "code"] |
| 112 | + }); |
| 113 | + let parent = document(&schema); |
| 114 | + let child = document(&schema); |
| 115 | + let mut guard = SchemaMergeGuard::new(&parent).unwrap(); |
| 116 | + |
| 117 | + guard |
| 118 | + .check_child(&child, "merged app `plugin::Plugin`", Span::call_site()) |
| 119 | + .expect("identical definitions should deduplicate"); |
| 120 | + } |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn a_schema_defined_once_is_unaffected() { |
| 124 | + let parent = document(&serde_json::json!({ "type": "string" })); |
| 125 | + |
| 126 | + SchemaMergeGuard::new(&parent).expect("one definition should be accepted"); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn child_to_child_conflict_reports_the_first_child() { |
| 131 | + let empty: OpenApi = serde_json::from_value(serde_json::json!({ |
| 132 | + "openapi": "3.1.0", |
| 133 | + "info": { "title": "test", "version": "1.0.0" }, |
| 134 | + "paths": {} |
| 135 | + })) |
| 136 | + .unwrap(); |
| 137 | + let first = document(&serde_json::json!({ "type": "string" })); |
| 138 | + let second = document(&serde_json::json!({ "type": "integer" })); |
| 139 | + let mut guard = SchemaMergeGuard::new(&empty).unwrap(); |
| 140 | + guard |
| 141 | + .check_child(&first, "merged app `plugin_a::PluginA`", Span::call_site()) |
| 142 | + .unwrap(); |
| 143 | + |
| 144 | + let error = guard |
| 145 | + .check_child(&second, "merged app `plugin_b::PluginB`", Span::call_site()) |
| 146 | + .expect_err("the later child must conflict with the first"); |
| 147 | + let message = error.to_string(); |
| 148 | + |
| 149 | + assert!(message.contains("plugin_a::PluginA")); |
| 150 | + assert!(message.contains("plugin_b::PluginB")); |
| 151 | + } |
| 152 | +} |
0 commit comments