Automatic interface document generation method and related equipment thereof
By building a code parser and static code analysis tree, extracting and analyzing the metadata and context information of the API interface, the errors or omissions caused by the lack of metadata or context information in the automatic generation of traditional API documents are solved, and the complete generation and Accurate API documentation.
Patent Information
- Application Number
- CN202411491092.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2025-02-07
AI Technical Summary
When the traditional API document automatic generation scheme lacks metadata or context information, it is easy to generate wrong or omitted documents, resulting in user misunderstanding or incorrect use.
By building a code parser and static code analysis tree, the basic metadata of the interface is extracted, and deeper context information is obtained through static code analysis, such as function dependencies, call relationships and execution processes, to generate complete and accurate API documents.
Ensure the completeness and accuracy of the generated API document, avoid users' misunderstandings or incorrect use of the interface, and even if some information is missing in the source code, the necessary document details can be supplemented through context analysis.
Smart Images

Figure CN119396375A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of artificial intelligence technology, and specifically relates to a method for automatically generating an interface document and related equipment. Background Art
[0002] The automatic generation of API (Application Programming Interface) documents by financial business systems is an important technology in the field of information technology. API service providers use certain methods or tools to process structured data based on API metadata definitions and automatically generate API documents, which helps developers and third-party partners integrate.
[0003] Currently, there is no perfect solution in the industry that can solve metadata dependency and complex context association at the same time. Traditional automatic API document generation solutions have certain limitations. The automatic generation process often relies on comments and metadata in the code. For complex APIs, it relies on context information such as business logic, authorization mechanism, and usage examples. If metadata or context information is missing, the generated documents will have errors or omissions, which will lead to customer misunderstandings or incorrect use.
[0004] In addition, although some current automatic API documentation solutions can reduce the burden of document writing, they cannot completely replace the work of manually maintaining and updating documents. When the API changes, developers still need to manually update comments and metadata to ensure that the generated documentation is synchronized with the actual code, which has a high maintenance cost. Summary of the invention
[0005] The purpose of the embodiments of the present application is to propose a method, apparatus, computer device and storage medium for automatically generating interface documents, so as to solve the problem that in the traditional automatic generation of API documents, when metadata or context information is missing, the generated API documents will produce errors or omissions, thereby leading to user misunderstanding or incorrect use.
[0006] In order to solve the above technical problems, the present application embodiment provides a method for automatically generating an interface document, which adopts the following technical solution:
[0007] A method for automatically generating an interface document, comprising:
[0008] Receive an interface document generation instruction, obtain pre-collected interface source code and code comments, and obtain an interface document template that matches the interface source code;
[0009] Build a code parser and use it to parse the interface source code and code comments to obtain code metadata;
[0010] Construct a code analysis tree for the interface source code, and perform static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata;
[0011] Traverse the preset controller classes and class processing methods, select the adapted controller class and class processing method to encapsulate the code metadata and context information, and generate response data;
[0012] Import the response data into the interface document template to generate the interface document corresponding to the interface source code.
[0013] Further, the steps of receiving an interface document generation instruction, obtaining pre-collected interface source code and code comments, and obtaining an interface document template matching the interface source code specifically include:
[0014] Receive an interface document generation instruction, parse the interface document generation instruction, and obtain document path information of the interface source code;
[0015] Extract interface source code from a preset code database based on document path information;
[0016] Identify a code comment file of the interface source code, and extract code comments from the code comment file;
[0017] Identify the interface type corresponding to the interface source code, and obtain an interface document template that matches the interface source code according to the interface type.
[0018] Furthermore, a code parser is constructed and used to parse the interface source code and code comments to obtain code metadata, specifically including:
[0019] Build an adaptive code parser according to the interface type corresponding to the interface source code;
[0020] The code parser reads the interface source code and code comments in sequence, and performs code parsing to obtain code metadata, wherein the code metadata includes interface class, interface parameter, parameter type, function name, and return value.
[0021] Further, a code analysis tree is constructed for the interface source code, and static code analysis is performed on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata, specifically including:
[0022] Identify the code structure of the interface source code and configure an adaptive abstract syntax tree based on the code structure;
[0023] The interface source code is segmented according to the code structure to obtain several source code fragments;
[0024] Identify the association relationship between each source code fragment, and import each source code fragment into the abstract syntax tree based on the association relationship to obtain a code analysis tree;
[0025] Determine a target tree node matching the code metadata on the code analysis tree;
[0026] Traverse the adjacent tree nodes and edges of the target tree node to obtain the context information of the code metadata.
[0027] Further, the steps of identifying the association relationship between the source code fragments and importing the source code fragments into the abstract syntax tree based on the association relationship to obtain the code analysis tree specifically include:
[0028] Determine the association relationship between the source code fragments based on the code comments corresponding to the source code fragments;
[0029] Determine the storage location of each source code fragment in the abstract syntax tree according to the association relationship;
[0030] According to the storage position of each source code fragment in the abstract syntax tree, each source code fragment is imported into a tree node of the abstract syntax tree;
[0031] The association relationship between each source code fragment is imported into the matching edge in the abstract syntax tree to obtain the code analysis tree.
[0032] Furthermore, the tree node includes a root node and a leaf node, and the step of traversing the adjacent tree nodes and edges of the target tree node to obtain the context information of the code metadata specifically includes:
[0033] On the code analysis tree, starting from the target tree node, traverse upward to the root node to obtain the first traversal information;
[0034] On the code analysis tree, starting from the target tree node, traverse downward until the last leaf node to obtain the second traversal information;
[0035] The first traversal information and the second traversal information are combined to obtain context information of the code metadata.
[0036] Further, the steps of traversing the preset controller classes and class processing methods, selecting the adapted controller class and class processing method to encapsulate the code metadata and context information, and generating response data specifically include:
[0037] Traverse the preset controller classes and class processing methods, and select the adapted controller class and class processing method according to the interface class in the code metadata;
[0038] Set the initial values of the interface parameters, parameter types, function names, and return values in the code metadata in sequence according to the preset parameter generation rules and context information;
[0039] Use the adapted controller class and class processing method to encapsulate the initial value and generate response data.
[0040] In order to solve the above technical problems, the embodiment of the present application also provides an automatic interface document generation device, which adopts the following technical solution:
[0041] An interface document automatic generation device, comprising:
[0042] A code acquisition module is used to receive an interface document generation instruction, obtain pre-collected interface source code and code comments, and obtain an interface document template that matches the interface source code;
[0043] The code parsing module is used to build a code parser and use the code parser to parse the interface source code and code comments to obtain code metadata;
[0044] A static analysis module is used to construct a code analysis tree for the interface source code, and to perform static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata;
[0045] The data encapsulation module is used to traverse the preset controller classes and class processing methods, select the adapted controller classes and class processing methods to encapsulate the code metadata and context information, and generate response data;
[0046] The document generation module is used to import the response data into the interface document template and generate the interface document corresponding to the interface source code.
[0047] In order to solve the above technical problems, the embodiment of the present application further provides a computer device, which adopts the following technical solution:
[0048] A computer device comprises a memory and a processor, wherein the memory stores computer-readable instructions, and when the processor executes the computer-readable instructions, the steps of the method for automatically generating an interface document as described in any one of the above items are implemented.
[0049] In order to solve the above technical problems, the embodiment of the present application further provides a computer-readable storage medium, which adopts the following technical solution:
[0050] A computer-readable storage medium having computer-readable instructions stored thereon, wherein the computer-readable instructions, when executed by a processor, implement the steps of the method for automatically generating an interface document as described in any one of the above.
[0051] Compared with the prior art, the embodiments of the present application have the following beneficial effects:
[0052] The present application discloses a method for automatically generating interface documents and related equipment, belonging to the field of artificial intelligence technology. The present application effectively solves the problem of document errors or omissions caused by lack of metadata or context information in the traditional API document automatic generation process by constructing a code parser and a static code analysis tree. The parser can not only extract the basic metadata of the interface, but also obtain deeper context information such as function dependencies, call relationships and execution processes through static code analysis, thereby ensuring the integrity and accuracy of the document. At the same time, even if some information is missing in the source code, the present application can supplement the necessary document details through context analysis, generate API documents with rich content and high consistency, and avoid user misunderstanding or incorrect use of the interface. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] In order to more clearly illustrate the scheme in the present application, a brief introduction is given below to the drawings required for use in the description of the embodiments of the present application. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0054] Figure 1 An exemplary system architecture diagram is shown in which the present application can be applied;
[0055] Figure 2 A flowchart of an embodiment of a method for automatically generating an interface document according to the present application is shown;
[0056] Figure 3 A schematic diagram showing the structure of an embodiment of an interface document automatic generation device according to the present application;
[0057] Figure 4 A schematic structural diagram of an embodiment of a computer device according to the present application is shown. DETAILED DESCRIPTION
[0058] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by technicians in the technical field of the present application; the terms used in the specification of the application herein are only for the purpose of describing specific embodiments and are not intended to limit the present application; the terms "including" and "having" and any variations thereof in the specification and claims of the present application and the above-mentioned drawings are intended to cover non-exclusive inclusions. The terms "first", "second", etc. in the specification and claims of the present application or the above-mentioned drawings are used to distinguish different objects, not to describe a specific order.
[0059] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0060] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the accompanying drawings.
[0061] like Figure 1 As shown, the system architecture 100 may include a terminal device 101, a network 102 and a server 103. The terminal device 101 may be a laptop 1011, a tablet computer 1012 or a mobile phone 1013. The network 102 is used to provide a medium for a communication link between the terminal device 101 and the server 103. The network 102 may include various connection types, such as wired, wireless communication links or optical fiber cables.
[0062] The user can use the terminal device 101 to interact with the server 103 through the network 102 to receive or send messages, etc. Various communication client applications can be installed on the terminal device 101, such as web browser applications, shopping applications, search applications, instant messaging tools, email clients, social platform software, etc.
[0063] The terminal device 101 can be any electronic device with a display screen and supporting web browsing. In addition to a laptop computer 1011, a tablet computer 1012 or a mobile phone 1013, the terminal device 101 can also be an e-book reader, an MP3 player (Moving Picture Experts Group Audio Layer III), an MP4 (Moving Picture Experts Group Audio Layer IV), a laptop computer, a desktop computer, etc.
[0064] The server 103 may be a server that provides various services, such as a background server that provides support for web pages displayed on the terminal device 101 .
[0065] It should be noted that the method for automatically generating an interface document provided in the embodiment of the present application is generally executed by a server / terminal device, and accordingly, the device for automatically generating an interface document is generally arranged in a server / terminal device.
[0066] It should be understood that Figure 1 The numbers of terminal devices, networks and servers in the embodiment are merely illustrative. The above system may have any number of terminal devices, networks and servers according to implementation requirements.
[0067] Continue to refer Figure 2 , shows a flow chart of an embodiment of a method for automatically generating an interface document according to the present application. The method for automatically generating an interface document comprises the following steps:
[0068] S201, receiving an interface document generation instruction, obtaining pre-collected interface source code and code comments, and obtaining an interface document template matching the interface source code.
[0069] Specifically, the system receives interface document generation instructions, which can be triggered in different ways, such as manually executing command line instructions by developers, or automatically issuing instructions through automated build tools (such as CI / CD pipelines). Then, the system obtains the pre-collected interface source code and related code comments according to the interface document generation instructions. These interface source codes are pre-stored in the version control system (such as Git), and the system needs to obtain the corresponding code through the correct path, branch, tag, etc. At the same time, interface comments, as an important source of API documentation, often exist in standard formats such as Javadoc and Docstring. The system needs to be able to correctly extract and understand these comment information.
[0070] In addition, this step also requires the system to automatically match the appropriate interface document template based on the acquired source code. These templates are usually based on industry standards (such as Swagger / OpenAPI, RAML) or customized document specifications within the team. Through template matching, the system can ensure that the generated documents conform to the project style and usage scenarios.
[0071] S202, building a code parser, and using the code parser to parse the interface source code and code comments to obtain code metadata.
[0072] Specifically, the code parser is a crucial component in the entire API document generation process, and it is necessary to perform structured parsing of the source code according to the grammatical rules of the target programming language. When building a parser, the system can identify and correctly parse the key elements in the interface source code, including class names, function names, parameters, return types, error codes, etc. It is usually necessary to use or develop parser tools adapted to specific programming languages, such as Java can use Javaparser, Python can use AST module, etc.
[0073] In addition to the basic code structure, the parser also needs to process the comments in the code. Different programming languages have their own comment standards, and the parser should be able to extract relevant documentation information of the API from the comments, including interface descriptions, parameter descriptions, return value descriptions, and error codes. Through source code parsing, the system can convert the interface source code and comments into an operational metadata structure, which will be used for static analysis and document generation.
[0074] S203, constructing a code analysis tree for the interface source code, and performing static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata.
[0075] Specifically, the code analysis tree is an abstract syntax tree (AST) generated by parsing the interface source code. AST can represent the syntax elements and their hierarchical relationships in the source code in a tree structure. By constructing AST, the system can identify high-level syntax structures such as control flow, data flow, and dependency relationships in the code.
[0076] Based on this structure, the system can perform static code analysis. Static code analysis is the process of analyzing source code through preset source code syntax detection rules without running the code. Its purpose is to extract contextual information from code metadata to supplement and improve the content that the code parser cannot directly obtain. Contextual information includes the calling relationship between functions, the parameter passing process, the scope and life cycle of variables, etc. For example, static analysis can determine whether a function depends on external resources, whether it has asynchronous execution features, or its return results under different conditions. This information will provide rich context for the interface description in the API document, helping users better understand the usage scenarios and possible behaviors of the interface.
[0077] S204, traversing preset controller classes and class processing methods, selecting an adapted controller class and class processing method to encapsulate the code metadata and context information, and generating response data.
[0078] Specifically, controller classes and class processing methods are usually the core of API interfaces in modern web applications or microservice architectures, responsible for processing external requests and returning responses.
[0079] In order to automatically generate API documentation, the system needs to first traverse the controller classes in the code, which are often identified by specific annotations (such as Spring's @RestController or @RequestMapping). The system will recognize these annotations and then find the corresponding classes and methods. Class processing methods refer to specific methods in the controller class that define different API endpoints and their processing logic.
[0080] In order to generate a complete API document, the system also needs to filter out the adapted processing methods, extract their relevant information and combine it with the metadata and context information in the previous step. This step is not just a simple acquisition of the method signature, but also requires identification of the method's request type (such as GET, POST, PUT, etc.), path parameters, query parameters, request body format, and response format. In addition, the system should also consider the return value type and exception handling logic of the processing method, and integrate this information into response data. Response data is the core content of the API document. The system will encapsulate the response data into a structured data format to provide a basis for filling in the document template.
[0081] S205, importing the response data into the interface document template to generate an interface document corresponding to the interface source code.
[0082] Specifically, after the response data is encapsulated, the system needs to import this structured information into a pre-matched interface document template to generate the final API document. Interface document templates are usually based on standardized document formats, such as OpenAPI, Swagger, etc., or are templates customized according to project requirements. The import process needs to map the response data to specific fields in the document template to ensure that the metadata, context information, request parameters, response format, etc. of each API interface can be accurately filled in the corresponding position of the document. For example, the template may contain the interface URL, HTTP method, parameter description, return value type, error code list, etc. The system needs to ensure that these fields can be fully displayed through the import of response data.
[0083] The generated interface documentation can be output in multiple formats, such as HTML, Markdown, PDF, etc., to meet different display requirements. In addition, the process should also consider the structured layout and readability of the generated documentation to ensure that the documentation is both developer-friendly and can clearly show how to use the API interface. Finally, the documentation can be published to the document server, API portal, or project site through automated tools to ensure that team members and external users can obtain the latest interface documentation at any time.
[0084] In the above embodiment, the present application discloses a method for automatically generating interface documents, which belongs to the field of artificial intelligence technology. The present application effectively solves the problem of document errors or omissions caused by lack of metadata or context information in the traditional API document automatic generation process by constructing a code parser and a static code analysis tree. The parser can not only extract the basic metadata of the interface, but also obtain deeper context information such as function dependencies, call relationships, and execution processes through static code analysis, thereby ensuring the integrity and accuracy of the document. At the same time, even if some information is missing in the source code, the present application can supplement the necessary document details through context analysis, generate API documents with rich content and high consistency, and avoid user misunderstanding or incorrect use of the interface.
[0085] Further, the steps of receiving an interface document generation instruction, obtaining pre-collected interface source code and code comments, and obtaining an interface document template matching the interface source code specifically include:
[0086] Receive an interface document generation instruction, parse the interface document generation instruction, and obtain document path information of the interface source code;
[0087] Extract interface source code from a preset code database based on document path information;
[0088] Identify a code comment file of the interface source code, and extract code comments from the code comment file;
[0089] Identify the interface type corresponding to the interface source code, and obtain an interface document template that matches the interface source code according to the interface type.
[0090] In this embodiment, after receiving the interface document generation instruction and parsing it, the system first obtains the document path information, which indicates the location where the interface source code is stored. The document path includes the file path, version branch, etc. in the code repository. Based on the path information, the system extracts the corresponding interface source code from a preset code database (such as Git, SVN and other version management systems). Next, the system will identify the code comment files in the source code. These comment files usually contain key information such as the API's functional description, parameter description, return value type, and exception handling. The system parses these files through preset comment specifications to extract usable metadata. Then, the system identifies the interface type of the interface source code, such as RESTful API, RPC interface, GraphQL interface, etc., and automatically matches the appropriate document template according to the interface type. These templates determine the format and content structure of the generated document to ensure that the document can clearly and standardizedly display the relevant information of the interface.
[0091] Through the above steps, key issues such as data extraction and template matching in the interface document generation process are effectively solved, ensuring that the document can accurately reflect the interface source code and its annotation information, thereby improving the quality and practicality of the document.
[0092] Furthermore, a code parser is constructed and used to parse the interface source code and code comments to obtain code metadata, specifically including:
[0093] Build an adaptive code parser according to the interface type corresponding to the interface source code;
[0094] The code parser reads the interface source code and code comments in sequence, and performs code parsing to obtain code metadata, wherein the code metadata includes interface class, interface parameter, parameter type, function name, and return value.
[0095] In this embodiment, an adaptive code parser is first constructed according to the interface type of the interface source code. Different interface types (such as RESTful API, GraphQL, RPC, etc.) correspond to different programming languages or framework specifications, so different parsers are required to process the code. The parser needs to understand the syntax and structure of the interface source code and be able to identify important elements in the interface, such as class definitions, method signatures, parameter lists, and return types. The system reads the interface source code and related comments in sequence through the parser, parses the information in the code line by line, and generates corresponding code metadata. These metadata include interface class names, interface methods, parameter types and names, return value types, and function names, etc. This information can not only construct a technical description of the interface, but also assist in generating a clear explanation of the interface behavior in the document.
[0096] Through the above steps, this metadata extraction method based on code parsing ensures that the generated documentation accurately describes the behavior and usage of the interface and keeps it synchronized with the actual code.
[0097] Furthermore, the abstract syntax tree includes tree nodes and edges, a code analysis tree is constructed for the interface source code, and static code analysis is performed on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain the context information of the code metadata, specifically including:
[0098] Identify the code structure of the interface source code and configure an adaptive abstract syntax tree based on the code structure;
[0099] The interface source code is segmented according to the code structure to obtain several source code fragments;
[0100] Identify the association relationship between each source code fragment, and import each source code fragment into the abstract syntax tree based on the association relationship to obtain a code analysis tree;
[0101] Determine a target tree node matching the code metadata on the code analysis tree;
[0102] Traverse the adjacent tree nodes and edges of the target tree node to obtain the context information of the code metadata.
[0103] In this embodiment, the system first identifies the code structure of the interface source code, determines the organization of the code, such as class, method, function, module, etc., and configures the corresponding abstract syntax tree (AST) according to this structure. AST is a tree structure representation of the source code, which can capture the syntax and logic of the code through hierarchical relationships. Next, the system divides the interface source code according to the identified code structure and decomposes it into several source code fragments, such as class declaration, method definition, variable declaration, etc. These fragments correspond to different parts of the code and represent the core components of the interface.
[0104] The system also needs to identify the relationships between these code snippets, such as call chains, variable dependencies, parameter passing, etc. Based on these relationships, the system gradually imports each snippet into the abstract syntax tree to build a complete code analysis tree. The code analysis tree not only retains the syntax information of the source code, but also structurally shows the relationship between the code snippets. By further determining the target nodes that match the code metadata (such as classes, functions, parameters, etc.) on the code analysis tree, the system can traverse the adjacent nodes and edges of these nodes to extract the context information of the interface. Context information is an important part of the interface document, which helps the system understand the environment, data flow and dependencies when the function is called, and ensures that the operating logic of the interface can be reflected in the document.
[0105] Through the above steps, by combining code segmentation with the abstract syntax tree, a comprehensive analysis of the code structure and association relationships is achieved, and a detailed code analysis tree is generated, which helps to capture complex interface logic and ensures the extraction of contextual information, making the generated API document more explanatory and reference-worthy.
[0106] Further, the steps of identifying the association relationship between the source code fragments and importing the source code fragments into the abstract syntax tree based on the association relationship to obtain the code analysis tree specifically include:
[0107] Determine the association relationship between the source code fragments based on the code comments corresponding to the source code fragments;
[0108] Determine the storage location of each source code fragment in the abstract syntax tree according to the association relationship;
[0109] According to the storage position of each source code fragment in the abstract syntax tree, each source code fragment is imported into a tree node of the abstract syntax tree;
[0110] The association relationship between each source code fragment is imported into the matching edge in the abstract syntax tree to obtain the code analysis tree.
[0111] In this embodiment, the system first determines the association between the source code fragments based on the code comments corresponding to the source code fragments. The code comments often contain description information about methods, classes, variables, etc. Through these comments, the dependencies, calling sequences, and logical relationships between different code fragments can be inferred. For example, a method comment may mention that a function calls another function, or that a class depends on a specific external resource. The system identifies the specific association between the code fragments based on these comments.
[0112] Next, the system determines the storage location of each code snippet in the abstract syntax tree (AST) based on these associations. AST is a tree data structure that represents code structure and syntax through tree nodes and edges. When code snippets are imported into the abstract syntax tree, each snippet is mapped to a tree node to ensure that the code structure is fully presented. At the same time, the system maps the associations between the snippets into edges within the abstract syntax tree to complete the representation of the logical flow of the code, and finally obtains a comprehensive code analysis tree. This code analysis tree can not only represent the independent functions of each code snippet, but also show how the various source code snippets interact with each other.
[0113] Through the above steps, combined with code comments and code structure, a comprehensive code analysis tree is constructed to ensure that the association relationship between source codes is accurately captured, thereby improving the accuracy of code parsing and ensuring that the generated documents are consistent with the code logic.
[0114] Furthermore, the tree node includes a root node and a leaf node, and the step of traversing the adjacent tree nodes and edges of the target tree node to obtain the context information of the code metadata specifically includes:
[0115] On the code analysis tree, starting from the target tree node, traverse upward to the root node to obtain the first traversal information;
[0116] On the code analysis tree, starting from the target tree node, traverse downward until the last leaf node to obtain the second traversal information;
[0117] The first traversal information and the second traversal information are combined to obtain context information of the code metadata.
[0118] In this embodiment, the system first locates the target tree node on the code analysis tree. Usually, the node represents a specific code metadata, such as a class, method, or variable. Next, the system starts to traverse upward from this target node to the root node. During the upward traversal, the system will record the relevant information along the way through the parent node in turn, and obtain higher-level context information such as the class, module, and package where the code snippet is located. This information constitutes the first traversal information, which helps reveal the location and background of the target snippet in the broader code structure. For example, the target node may be a method, and the first traversal information will provide the class to which the method belongs and other modules it depends on.
[0119] Then, the system starts to traverse downward from the target tree node until the last leaf node, recording all lower-level nodes encountered in the process. The goal of this process is to capture detailed information related to the target fragment, such as function call chains, parameter passing, variable usage, etc., to form the second traversal information, which can supplement the internal implementation details of the code fragment and show its specific operation logic.
[0120] Finally, the system combines the first traversal information with the second traversal information to form complete contextual information of the code metadata. This top-down traversal method not only ensures the global positioning of the code snippet, but also displays its internal logic and implementation in detail.
[0121] Through the above steps, the context information of the code metadata is effectively constructed, combining the global structure of the code with the local implementation details to ensure that the generated document has an accurate and comprehensive description of the behavior and logic of the interface, which helps users to deeply understand how to use the interface.
[0122] Further, the steps of traversing the preset controller classes and class processing methods, selecting the adapted controller class and class processing method to encapsulate the code metadata and context information, and generating response data specifically include:
[0123] Traverse the preset controller classes and class processing methods, and select the adapted controller class and class processing method according to the interface class in the code metadata;
[0124] Set the initial values of the interface parameters, parameter types, function names, and return values in the code metadata in sequence according to the preset parameter generation rules and context information;
[0125] Use the adapted controller class and class processing method to encapsulate the initial value and generate response data.
[0126] In this embodiment, the system first traverses the preset controller classes and class processing methods. These controller classes are usually responsible for processing API requests and executing specific business logic through class processing methods. In order to ensure that the generated API document can accurately reflect the actual behavior of the interface, the system will select the appropriate controller class and class processing method based on the interface class in the code metadata. The interface class usually contains the definition of the API and the interface layer of the business logic. The system will match the corresponding controller class to ensure that the interface behavior is consistent when the document is generated.
[0127] Next, the system gradually sets initial values for the interface parameters, parameter types, function names, and return values in the code metadata based on the preset parameter generation rules and context information. Setting initial values is a key step in automatically generating API documents. The system parses the interface definition and context to infer the appropriate parameter types, default values, etc. to ensure the accuracy of the interface call method. These initial values can help generate more complete API documents.
[0128] Finally, the system uses the adapted controller class and class processing method to encapsulate these initial values and generate response data. The response data includes the actual response structure and format of the interface, which is an important part of the API document describing the interface behavior. By encapsulating the response data, the system can generate specific input and output examples for each interface to help developers understand the usage of the interface and the expected results.
[0129] Through the above steps, it is effectively ensured that the generated API document can accurately reflect the actual behavior and parameter logic of the interface. The generated response data clearly displays the input and output format of the interface, which improves the practicality and accuracy of the document.
[0130] In the above embodiment, the present application discloses a method for automatically generating interface documents, which belongs to the field of artificial intelligence technology. The present application effectively solves the problem of document errors or omissions caused by lack of metadata or context information in the traditional API document automatic generation process by constructing a code parser and a static code analysis tree. The parser can not only extract the basic metadata of the interface, but also obtain deeper context information such as function dependencies, call relationships, and execution processes through static code analysis, thereby ensuring the integrity and accuracy of the document. At the same time, even if some information is missing in the source code, the present application can supplement the necessary document details through context analysis, generate API documents with rich content and high consistency, and avoid user misunderstanding or incorrect use of the interface.
[0131] In this embodiment, the electronic device (eg, Figure 1The server shown in the figure) can receive instructions or obtain data through a wired connection or a wireless connection. It should be noted that the above wireless connection method may include but is not limited to 3G / 4G connection, WiFi connection, Bluetooth connection, WiMAX connection, Zigbee connection, UWB (ultra wideband) connection, and other wireless connection methods currently known or to be developed in the future.
[0132] It should be emphasized that in order to further ensure the privacy and security of the above source code information, the above source code information can also be stored in a node of a blockchain.
[0133] The blockchain referred to in this application is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm, etc. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods. Each data block contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. Blockchain can include the underlying blockchain platform, platform product service layer, and application service layer.
[0134] The embodiments of the present application can acquire and process relevant data based on artificial intelligence technology. Among them, artificial intelligence (AI) is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.
[0135] AI basic technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics, etc. AI software technologies mainly include computer vision technology, robotics technology, biometrics technology, speech processing technology, natural language processing technology, and machine learning / deep learning.
[0136] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through computer-readable instructions, and the computer-readable instructions can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the aforementioned storage medium can be a non-volatile storage medium such as a disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM).
[0137] It should be understood that, although the steps in the flowchart of the accompanying drawings are displayed in sequence as indicated by the arrows, these steps are not necessarily executed in sequence in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least a part of the steps in the flowchart of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.
[0138] Further references Figure 3 , as a response to the above Figure 2 The present application provides an embodiment of an interface document automatic generation device, and the device embodiment is similar to Figure 2 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.
[0139] like Figure 3 As shown, the interface document automatic generation device 300 described in this embodiment includes:
[0140] The code acquisition module 301 is used to receive an interface document generation instruction, obtain pre-collected interface source code and code comments, and obtain an interface document template that matches the interface source code;
[0141] The code parsing module 302 is used to build a code parser and use the code parser to parse the interface source code and code comments to obtain code metadata;
[0142] The static analysis module 303 is used to construct a code analysis tree for the interface source code, and perform static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata;
[0143] The data encapsulation module 304 is used to traverse the preset controller classes and class processing methods, select the adapted controller class and class processing method to encapsulate the code metadata and context information, and generate response data;
[0144] The document generation module 305 is used to import the response data into the interface document template to generate an interface document corresponding to the interface source code.
[0145] Furthermore, the code acquisition module 301 is specifically used for:
[0146] Receive an interface document generation instruction, parse the interface document generation instruction, and obtain document path information of the interface source code; extract the interface source code from a preset code database based on the document path information; identify the code comment file of the interface source code, and extract code comments from the code comment file; identify the interface type corresponding to the interface source code, and obtain an interface document template that matches the interface source code according to the interface type.
[0147] Furthermore, the code parsing module 302 is specifically used for:
[0148] An adaptive code parser is constructed according to the interface type corresponding to the interface source code; the interface source code and code comments are read in sequence by the code parser, and the code is parsed to obtain code metadata, wherein the code metadata includes interface class, interface parameter, parameter type, function name, and return value.
[0149] Furthermore, the abstract syntax tree includes tree nodes and edges, and the static analysis module 303 is specifically used for:
[0150] Identify the code structure of the interface source code and configure an adaptive abstract syntax tree according to the code structure; segment the interface source code according to the code structure to obtain several source code fragments; identify the association relationship between each source code fragment, and import each source code fragment into the abstract syntax tree based on the association relationship to obtain a code analysis tree; determine the target tree node that matches the code metadata on the code analysis tree; traverse the adjacent tree nodes and edges of the target tree node to obtain the context information of the code metadata.
[0151] Furthermore, the static analysis module 303 is further used to:
[0152] Based on the code comments corresponding to each source code fragment, the association relationship between each source code fragment is determined; according to the association relationship, the storage position of each source code fragment in the abstract syntax tree is determined; according to the storage position of each source code fragment in the abstract syntax tree, each source code fragment is imported into the tree node of the abstract syntax tree; the association relationship between each source code fragment is imported into the matching edge in the abstract syntax tree to obtain a code analysis tree.
[0153] Furthermore, the static analysis module 303 is also used for:
[0154] On the code analysis tree, starting from the target tree node, traverse upward to the root node to obtain the first traversal information; on the code analysis tree, starting from the target tree node, traverse downward to the last leaf node to obtain the second traversal information; combine the first traversal information and the second traversal information to obtain the context information of the code metadata.
[0155] Furthermore, the data encapsulation module 304 is specifically used for:
[0156] Traverse the preset controller classes and class processing methods, and select the adapted controller classes and class processing methods according to the interface classes in the code metadata; set the initial values of the interface parameters, parameter types, function names, and return values in the code metadata in sequence according to the preset parameter generation rules and context information; use the adapted controller classes and class processing methods to encapsulate the initial values and generate response data.
[0157] In the above embodiment, the present application discloses an interface document automatic generation device, which belongs to the field of artificial intelligence technology. The present application effectively solves the problem of document errors or omissions caused by lack of metadata or context information in the traditional API document automatic generation process by constructing a code parser and a static code analysis tree. The parser can not only extract the basic metadata of the interface, but also obtain deeper context information such as function dependencies, call relationships and execution processes through static code analysis, thereby ensuring the integrity and accuracy of the document. At the same time, even if some information is missing in the source code, the present application can supplement the necessary document details through context analysis, generate API documents with rich content and high consistency, and avoid user misunderstanding or incorrect use of the interface.
[0158] To solve the above technical problems, the present application also provides a computer device. Figure 4 , Figure 4 This is a basic structural block diagram of the computer device in this embodiment.
[0159] The computer device 4 includes a memory 41, a processor 42, and a network interface 43 that are interconnected and communicated through a system bus. It should be noted that the figure only shows a computer device 4 with a memory 41, a processor 42, and a network interface 43, but it should be understood that it is not required to implement all the components shown, and more or fewer components can be implemented instead. Among them, those skilled in the art can understand that the computer device here is a device that can automatically perform numerical calculations and / or information processing according to pre-set or stored instructions, and its hardware includes but is not limited to microprocessors, application specific integrated circuits (Application Specific Integrated Circuit, ASIC), programmable gate arrays (Field-Programmable Gate Array, FPGA), digital processors (Digital Signal Processor, DSP), embedded devices, etc.
[0160] The computer device may be a computing device such as a desktop computer, a notebook, a PDA, a cloud server, etc. The computer device may interact with a user through a keyboard, a mouse, a remote controller, a touch pad, or a voice control device.
[0161] The memory 41 includes at least one type of readable storage medium, and the readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic memory, disk, optical disk, etc. In some embodiments, the memory 41 can be an internal storage unit of the computer device 4, such as a hard disk or memory of the computer device 4. In other embodiments, the memory 41 can also be an external storage device of the computer device 4, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (FlashCard), etc. equipped on the computer device 4. Of course, the memory 41 can also include both the internal storage unit of the computer device 4 and its external storage device. In this embodiment, the memory 41 is generally used to store the operating system and various application software installed on the computer device 4, such as computer-readable instructions of the automatic generation method of interface documents, etc. In addition, the memory 41 can also be used to temporarily store various types of data that have been output or are to be output.
[0162] The processor 42 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other data processing chips in some embodiments. The processor 42 is generally used to control the overall operation of the computer device 4. In this embodiment, the processor 42 is used to run the computer-readable instructions stored in the memory 41 or process data, such as running the computer-readable instructions of the interface document automatic generation method.
[0163] The network interface 43 may include a wireless network interface or a wired network interface. The network interface 43 is generally used to establish a communication connection between the computer device 4 and other electronic devices.
[0164] In the above embodiment, the present application discloses a computer device, which belongs to the field of artificial intelligence technology. The present application effectively solves the problem of document errors or omissions caused by lack of metadata or context information in the automatic generation process of traditional API documents by constructing a code parser and a static code analysis tree. The parser can not only extract the basic metadata of the interface, but also obtain deeper context information such as function dependencies, call relationships and execution processes through static code analysis, thereby ensuring the integrity and accuracy of the document. At the same time, even if some information is missing in the source code, the present application can supplement the necessary document details through context analysis, generate API documents with rich content and high consistency, and avoid user misunderstanding or incorrect use of the interface.
[0165] The present application also provides another implementation, namely, providing a computer-readable storage medium, wherein the computer-readable storage medium stores computer-readable instructions, and the computer-readable instructions can be executed by at least one processor to enable the at least one processor to perform the steps of the automatic interface document generation method as described above.
[0166] In the above embodiments, the present application discloses a computer-readable storage medium, which belongs to the field of artificial intelligence technology. The present application effectively solves the problem of document errors or omissions caused by lack of metadata or context information in the automatic generation process of traditional API documents by constructing a code parser and a static code analysis tree. The parser can not only extract the basic metadata of the interface, but also obtain deeper context information such as function dependencies, call relationships, and execution processes through static code analysis, thereby ensuring the integrity and accuracy of the document. At the same time, even if some information is missing in the source code, the present application can supplement the necessary document details through context analysis, generate API documents with rich content and high consistency, and avoid user misunderstanding or incorrect use of the interface.
[0167] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in each embodiment of the present application.
[0168] The present application can be used in many general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronic devices, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, etc. The present application can be described in the general context of computer executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application can also be practiced in distributed computing environments, in which tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0169] Obviously, the embodiments described above are only some embodiments of the present application, rather than all embodiments. The preferred embodiments of the present application are given in the accompanying drawings, but they do not limit the patent scope of the present application. The present application can be implemented in many different forms. On the contrary, the purpose of providing these embodiments is to make the understanding of the disclosure of the present application more thorough and comprehensive. Although the present application is described in detail with reference to the aforementioned embodiments, for those skilled in the art, it is still possible to modify the technical solutions recorded in the aforementioned specific implementation methods, or to perform equivalent replacement of some of the technical features therein. Any equivalent structure made using the contents of the specification and drawings of this application, directly or indirectly used in other related technical fields, is similarly within the scope of patent protection of this application.
Claims
1. A method for automatically generating an interface document, characterized in that: include: Receiving an interface document generation instruction, obtaining pre-collected interface source code and code comments, and obtaining an interface document template matching the interface source code; Constructing a code parser, and using the code parser to parse the interface source code and the code comments to obtain code metadata; Constructing a code analysis tree for the interface source code, and performing static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata; Traversing preset controller classes and class processing methods, selecting an adapted controller class and class processing method to encapsulate the code metadata and the context information, and generating response data; The response data is imported into the interface document template to generate an interface document corresponding to the interface source code.
2. The method for automatically generating an interface document according to claim 1, characterized in that: The step of receiving an interface document generation instruction, obtaining pre-collected interface source code and code comments, and obtaining an interface document template matching the interface source code specifically includes: Receiving an interface document generation instruction, parsing the interface document generation instruction, and obtaining document path information of the interface source code; Extracting the interface source code from a preset code database based on the document path information; Identify a code comment file of the interface source code, and extract the code comment from the code comment file; An interface type corresponding to the interface source code is identified, and an interface document template matching the interface source code is acquired according to the interface type.
3. The method for automatically generating an interface document according to claim 2, characterized in that: The step of constructing a code parser and using the code parser to parse the interface source code and the code comments to obtain code metadata specifically includes: Construct an adaptive code parser according to the interface type corresponding to the interface source code; The code parser reads the interface source code and the code comments in sequence, and performs code parsing to obtain the code metadata, wherein the code metadata includes interface class, interface parameter, parameter type, function name, and return value.
4. The method for automatically generating an interface document according to claim 1, characterized in that: The step of constructing a code analysis tree for the interface source code, and performing static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata specifically includes: Identify the code structure of the interface source code, and configure an adaptive abstract syntax tree according to the code structure; Performing code segmentation on the interface source code according to the code structure to obtain a plurality of source code fragments; Identify the association relationship between each of the source code fragments, and import each of the source code fragments into the abstract syntax tree based on the association relationship to obtain the code analysis tree; Determining a target tree node matching the code metadata on the code analysis tree; Traverse the adjacent tree nodes and edges of the target tree node to obtain context information of the code metadata.
5. The method for automatically generating an interface document according to claim 4, characterized in that: The step of identifying the association relationship between the source code fragments, and importing the source code fragments into the abstract syntax tree based on the association relationship to obtain the code analysis tree specifically includes: Determining associations between the source code snippets based on code comments corresponding to the source code snippets; Determine the storage location of each of the source code fragments in the abstract syntax tree according to the association relationship; According to the storage position of each source code fragment in the abstract syntax tree, import each source code fragment into a tree node of the abstract syntax tree; The association relationship between each of the source code fragments is imported into the matching edges in the abstract syntax tree to obtain the code analysis tree.
6. The method for automatically generating an interface document according to claim 4, characterized in that: The tree node includes a root node and a leaf node, and the step of traversing the adjacent tree nodes and edges of the target tree node to obtain the context information of the code metadata specifically includes: On the code analysis tree, starting from the target tree node, traversing upward to the root node to obtain first traversal information; On the code analysis tree, starting from the target tree node, traversing downward until the last leaf node, to obtain second traversal information; The first traversal information and the second traversal information are combined to obtain context information of the code metadata.
7. The method for automatically generating an interface document according to claim 3, characterized in that: The step of traversing the preset controller classes and class processing methods, selecting the adapted controller class and class processing method to encapsulate the code metadata and the context information, and generating response data specifically includes: Traversing the preset controller classes and class processing methods, and selecting an adapted controller class and class processing method according to the interface class in the code metadata; The initial values of the interface parameters, parameter types, function names, and return values in the code metadata are sequentially set according to the preset parameter generation rules and the context information; The initial value is encapsulated using the adapted controller class and class processing method to generate the response data.
8. An interface document automatic generation device, characterized in that: include: A code acquisition module is used to receive an interface document generation instruction, obtain pre-collected interface source code and code comments, and obtain an interface document template that matches the interface source code; A code parsing module, used to build a code parser, and use the code parser to parse the interface source code and the code comments to obtain code metadata; A static analysis module, used to construct a code analysis tree for the interface source code, and perform static code analysis on the code analysis tree based on the code metadata and preset source code syntax detection rules to obtain context information of the code metadata; A data encapsulation module, used to traverse preset controller classes and class processing methods, select an adapted controller class and class processing method to encapsulate the code metadata and the context information, and generate response data; The document generation module is used to import the response data into the interface document template to generate an interface document corresponding to the interface source code.
9. A computer device, characterized in that: It comprises a memory and a processor, wherein the memory stores computer-readable instructions, and when the processor executes the computer-readable instructions, the steps of the method for automatically generating interface documents as described in any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-readable instructions, which, when executed by a processor, implement the steps of the method for automatically generating an interface document as described in any one of claims 1 to 7.
Citation Information
Cited By
Knowledge graph-based two-hop incremental interface consistency verification method and system
CN120743922A
Data analysis method and device based on syntactic analysis generator
CN120909602A
A data parsing method and device based on a syntax analysis generator
CN120909602B
Universal document automatic generation method based on structured code annotation
CN121433716A
Interface document automatic generation method and device, electronic equipment and storage medium
CN121579066A