diff --git a/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs b/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs index 76f86ef..72f8602 100644 --- a/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs +++ b/src/WireMockInspector/CodeGenerators/MappingCodeGenerator.cs @@ -56,7 +56,8 @@ private static string ReadEmbeddedResource(string resourceName) using StreamReader reader = new StreamReader(stream); return reader.ReadToEnd(); } - public const string DefaultTemplateName = "(default)"; + public const string DefaultTemplateForCSharp = "C# (default)"; + public const string DefaultTemplateForJSON = "JSON (default)"; private static JToken? TryParseJson(string? payload) @@ -75,10 +76,21 @@ private static string ReadEmbeddedResource(string resourceName) } } - public static string GenerateCSharpCode(LogRequestModel logRequest, LogResponseModel logResponse, MappingCodeGeneratorConfigViewModel config) + private static string EscapeStringForJson(string value) + { + if (string.IsNullOrEmpty(value)) + { + return value; + } + + return JsonConvert.SerializeObject(value); + } + + public static MarkdownCode GenerateCode(LogRequestModel logRequest, LogResponseModel logResponse, MappingCodeGeneratorConfigViewModel config) { var options = new TemplateOptions(); options.ValueConverters.Add(o => o is JToken t? t.ToString(): null ); + options.Filters.AddFilter("escape_json", (input, arguments, templateContext) => new StringValue(EscapeStringForJson(input.ToStringValue()))); options.Filters.AddFilter("escape_string_for_csharp", (input, arguments, templateContext) => new StringValue(EscapeStringForCSharp(input.ToStringValue()) )); options.Filters.AddFilter("format_as_anonymous_object", (input, arguments, templateContext) => { @@ -94,23 +106,9 @@ public static string GenerateCSharpCode(LogRequestModel logRequest, LogResponseM _ => input }; }); + var parser = new FluidParser(); - - var templateCode =""; - if (config.SelectedTemplate == DefaultTemplateName) - { - templateCode = ReadEmbeddedResource("WireMockInspector.CodeGenerators.default_template.liquid"); - } - else if(string.IsNullOrWhiteSpace(config.SelectedTemplate) == false) - { - var templatePath = Path.Combine(PathHelper.GetTemplateDir(), config.SelectedTemplate); - if (File.Exists(templatePath)) - { - templateCode = File.ReadAllText(templatePath); - } - } - - + var (lang, templateCode) = GetTemplateCode(config); if (parser.TryParse(templateCode, out var ftemplate, out var error)) { var reader = new JsonDataSourceReader(); @@ -162,13 +160,37 @@ public static string GenerateCSharpCode(LogRequestModel logRequest, LogResponseM { data = data }, options)); - return result; + return new MarkdownCode(lang, result); } - return error; + return new MarkdownCode("text", $"Error: {error}"); } + private static (string lang, string template) GetTemplateCode(MappingCodeGeneratorConfigViewModel config) + { + if (config.SelectedTemplate == DefaultTemplateForCSharp) + { + return ("cs", ReadEmbeddedResource("WireMockInspector.CodeGenerators.default_template.liquid")); + } + + if (config.SelectedTemplate == DefaultTemplateForJSON) + { + return ("json", ReadEmbeddedResource("WireMockInspector.CodeGenerators.json_definition_template.liquid")); + } + + if(string.IsNullOrWhiteSpace(config.SelectedTemplate) == false) + { + var templatePath = Path.Combine(PathHelper.GetTemplateDir(), config.SelectedTemplate); + if (File.Exists(templatePath)) + { + return ("cs", File.ReadAllText(templatePath)); + } + } + + return ("cs",string.Empty); + } + private static string GetFullPath(LogRequestModel logRequest) { var query = HttpUtility.ParseQueryString(""); diff --git a/src/WireMockInspector/CodeGenerators/json_definition_template.liquid b/src/WireMockInspector/CodeGenerators/json_definition_template.liquid new file mode 100644 index 0000000..7e29b2e --- /dev/null +++ b/src/WireMockInspector/CodeGenerators/json_definition_template.liquid @@ -0,0 +1,115 @@ +{%- assign request = data.request -%} +{%- assign response = data.response -%} +{%- assign config = data.config -%} +{ + "Priority": 0, + "Request": { + {%- if config.IncludePath %} + "Path": { + "Matchers": [ + { + "Name": "WildcardMatcher", + "Pattern": {{ request.Path | escape_json }} + } + ] + }, + {%- endif %} + {%- if config.IncludeMethod %} + "Methods": [ + {{ request.Method | escape_json }} + ], + {%- endif %} + {%- if config.IncludeClientIP and request.ClientIP %} + "ClientIP": {{ request.ClientIP | escape_json }}, + {%- endif %} + {%- if config.IncludeAbsolutePath and request.AbsolutePath %} + "AbsolutePath": {{ request.AbsolutePath | escape_json }}, + {%- endif %} + {%- if config.IncludeUrl and request.Url %} + "Url": {{ request.Url | escape_json }}, + {%- endif %} + {%- if config.IncludeAbsoluteUrl and request.AbsoluteUrl %} + "AbsoluteUrl": {{ request.AbsoluteUrl | escape_json }}, + {%- endif %} + {%- if config.IncludeProxyUrl and request.ProxyUrl %} + "ProxyUrl": {{ request.ProxyUrl | escape_json }}, + {%- endif %} + {%- if config.IncludeQuery and request.Query %} + "Params": [ + {%- for item in request.Query %} + {%- assign query_key = item[0] %} + {%- assign query_values = item[1] %} + { + "Name": {{ query_key | escape_json }}, + "Matchers": [ + {%- for value in query_values %} + { + "Name": "ExactMatcher", + "Pattern": {{ value | escape_json }} + }{%- if forloop.last == false %},{% endif %} + {%- endfor %} + ] + }{%- if forloop.last == false %},{% endif %} + {%- endfor %} + ], + {%- endif %} + {%- if config.IncludeHeaders and request.Headers %} + "Headers": [ + {%- for item in request.Headers %} + {%- assign header_key = item[0] %} + {%- assign header_values = item[1] | join: "\", \"" | escape_json %} + { + "Name": {{ header_key | escape_json }}, + "Matchers": [ + { + "Name": "ExactMatcher", + "Pattern": {{ header_values }} + } + ] + }{%- if forloop.last == false %},{% endif %} + {%- endfor %} + ], + {%- endif %} + + {%- if config.IncludeBody %} + {%- if request.BodyAsJson %} + "Body": { + "Matcher": { + "Name": "WildcardMatcher", + "Pattern": {{ request.BodyAsJson | escape_json }} + } + }, + {%- elsif request.Body %} + "Body": { + "Matcher": { + "Name": "WildcardMatcher", + "Pattern": {{ request.Body | escape_json }} + } + } + {%- endif %} + {%- endif %} + }, + "Response": { + {%- if config.IncludeStatusCode and response.StatusCode %} + "StatusCode": {{ response.StatusCode }}, + {%- endif %} + "BodyDestination": "SameAsSource", + {%- if config.IncludeHeadersResponse and response.Headers %} + "Headers": { + {%- for item in response.Headers %} + {%- assign header_key = item[0] %} + {%- assign header_values = item[1] | join: ", " | escape_json %} + {{ header_key | escape_json }}: {{ header_values }}{%- if forloop.last == false %},{% endif %} + {%- endfor %} + }, + {%- endif %} + {%- if config.IncludeBodyResponse %} + {%- if response.BodyAsJson %} + "BodyAsJson": {{ response.BodyAsJson | escape_json }}, + {%- elsif response.Body %} + {%- assign body = response.Body | escape_json %} + "Body": {{ body }} + {%- endif %} + {%- endif %} + } +} \ No newline at end of file diff --git a/src/WireMockInspector/ViewModels/MainWindowViewModel.cs b/src/WireMockInspector/ViewModels/MainWindowViewModel.cs index 60dc086..2bb2afe 100644 --- a/src/WireMockInspector/ViewModels/MainWindowViewModel.cs +++ b/src/WireMockInspector/ViewModels/MainWindowViewModel.cs @@ -459,7 +459,7 @@ await _settingsManager.UpdateSettings(settings => Config = { - SelectedTemplate = MappingCodeGenerator.DefaultTemplateName, + SelectedTemplate = MappingCodeGenerator.DefaultTemplateForCSharp, Templates = GetAvailableTemplates().ToList(), IncludeClientIP = false, IncludePath = true, @@ -476,15 +476,13 @@ await _settingsManager.UpdateSettings(settings => }; }).ToProperty(this, x=>x.CodeGenerator, out _codeGenerator); } - - - private IEnumerable GetAvailableTemplates() { var templateDir = PathHelper.GetTemplateDir(); - yield return MappingCodeGenerator.DefaultTemplateName; + yield return MappingCodeGenerator.DefaultTemplateForCSharp; + yield return MappingCodeGenerator.DefaultTemplateForJSON; foreach (var file in Directory.GetFiles(templateDir, "*.liquid")) { diff --git a/src/WireMockInspector/ViewModels/MappingCodeGeneratorViewModel.cs b/src/WireMockInspector/ViewModels/MappingCodeGeneratorViewModel.cs index 4756289..d4e9dcd 100644 --- a/src/WireMockInspector/ViewModels/MappingCodeGeneratorViewModel.cs +++ b/src/WireMockInspector/ViewModels/MappingCodeGeneratorViewModel.cs @@ -39,10 +39,7 @@ public MappingCodeGeneratorViewModel() Config.WhenAnyPropertyChanged() .Where(x=> x is not null) - .Select(x => - { - var code = MappingCodeGenerator.GenerateCSharpCode(Request, Response, x); - return new MarkdownCode("cs", code); - }).ToProperty(this, x => x.OutputCode, out _outputCode); + .Select(x => MappingCodeGenerator.GenerateCode(Request, Response, x)) + .ToProperty(this, x => x.OutputCode, out _outputCode); } } \ No newline at end of file diff --git a/src/WireMockInspector/Views/CodeBlockViewer.cs b/src/WireMockInspector/Views/CodeBlockViewer.cs index 7c4c976..bf6353e 100644 --- a/src/WireMockInspector/Views/CodeBlockViewer.cs +++ b/src/WireMockInspector/Views/CodeBlockViewer.cs @@ -49,28 +49,18 @@ private void OnCodeChanged(MarkdownCode newValue, MarkdownCode NewValue) } - private void SetMarkdown(ViewModels.MarkdownCode md) + private void SetMarkdown(ViewModels.MarkdownCode md) { if (md is not null) { - if (this.Document is not null) - { - this.Document.Text = md.rawValue; - } - else - { - this.Document = new TextDocument(md.rawValue); - } + if (_currentLang != md.lang) { _currentLang = md.lang; - CodeBlockViewer _textEditor = this; if (_registryOptions.GetLanguageByExtension("." + md.lang) is { } languageByExtension) { _textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(languageByExtension.Id)); - - } @@ -80,6 +70,16 @@ private void SetMarkdown(ViewModels.MarkdownCode md) } this.TextArea.TextView.LineTransformers.Add(new DiffLineBackgroundRenderer(md.oldTextLines)); } + + if (this.Document is not null || _currentLang != md.lang) + { + this.Document.Text = md.rawValue; + } + else + { + this.Document = new TextDocument(md.rawValue); + } + } else { diff --git a/src/WireMockInspector/WireMockInspector.csproj b/src/WireMockInspector/WireMockInspector.csproj index 9988e31..242e7e2 100644 --- a/src/WireMockInspector/WireMockInspector.csproj +++ b/src/WireMockInspector/WireMockInspector.csproj @@ -76,5 +76,6 @@ +