You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

正则表达式替换修改后的捕获组:OpenAPI JSON预处理需求

VB.NET Solution to Update OpenAPI operationId from Processed Summary

Hey there! Let's work through this OpenAPI JSON preprocessing task in VB.NET. This approach uses Newtonsoft.Json (a go-to library for JSON manipulation in .NET) to keep things clean and concise:

Step 1: Install the Required Library

First, add the Newtonsoft.Json NuGet package to your project. You can do this via the NuGet Package Manager, or run this command in the Package Manager Console:

Install-Package Newtonsoft.Json

Step 2: Full Implementation Code

Here's the complete code to load your OpenAPI JSON, update all operationId values, and save the modified file:

Imports Newtonsoft.Json.Linq
Imports System.IO
Imports System.Text.RegularExpressions

Module OpenApiOperationIdUpdater
    Sub Main()
        ' Replace with your OpenAPI JSON file path
        Dim openApiFilePath = "C:\your-path\openapi.json"
        
        ' Load the JSON document
        Dim openApiDoc = JObject.Parse(File.ReadAllText(openApiFilePath))

        ' Traverse every path and its associated operations (GET, POST, etc.)
        For Each pathProperty In openApiDoc("paths").Children(Of JProperty)()
            For Each operationProperty In pathProperty.Value.Children(Of JProperty)()
                ' Skip entries missing summary or operationId to avoid errors
                If operationProperty.Value("summary") Is Nothing OrElse operationProperty.Value("operationId") Is Nothing Then
                    Continue For
                End If

                ' Get the original summary and generate the new operationId
                Dim originalSummary = operationProperty.Value("summary").ToString()
                Dim newOperationId = CleanSummaryAndGenerateId(originalSummary)

                ' Update the operationId in the JSON
                operationProperty.Value("operationId") = newOperationId
            Next
        Next

        ' Save the modified JSON with indentation for readability
        File.WriteAllText(openApiFilePath, openApiDoc.ToString(Newtonsoft.Json.Formatting.Indented))
        Console.WriteLine("Successfully updated all operation IDs!")
    End Sub

    ''' <summary>
    ''' Cleans the summary (removes spaces/parentheses) and converts it to a PascalCase operationId
    ''' </summary>
    Private Function CleanSummaryAndGenerateId(summary As String) As String
        ' Split the summary into individual words using spaces as separators
        Dim words = Regex.Split(summary.Trim(), "\s+")

        ' Process each word: remove parentheses, capitalize first letter, lowercase the rest
        Dim processedWords = words.Select(Function(word)
                                              Dim cleanedWord = Regex.Replace(word, "[()]", "")
                                              If String.IsNullOrEmpty(cleanedWord) Then
                                                  Return String.Empty
                                              End If
                                              ' Ensure consistent casing across regions
                                              Return Char.ToUpper(cleanedWord(0)) & cleanedWord.Substring(1).ToLowerInvariant()
                                          End Function)

        ' Join all processed words into a single PascalCase string
        Return String.Concat(processedWords)
    End Function
End Module

Key Details Explained

  • Dynamic JSON Traversal: Using JObject lets us navigate the OpenAPI structure without defining a full set of model classes, which keeps the code lightweight.
  • Summary Processing: The CleanSummaryAndGenerateId function handles exactly what you need:
    1. Splits the summary into words by spaces
    2. Removes any parentheses from each word
    3. Converts each word to PascalCase (first letter uppercase, rest lowercase)
    4. Joins everything into a single string for the new operationId
  • Error Safety: The code skips any operations missing a summary or operationId to prevent runtime errors.
  • Readable Output: We save the modified JSON with indentation so it's easy to review changes.

Example Transformation

If your original summary is Get User (by ID), the function will generate GetUserById as the new operationId.

内容的提问来源于stack exchange,提问作者SteveCinq

火山引擎 最新活动