A method, apparatus, and device for detecting annotation configuration errors.

By using reverse scanning and hash mapping linked list matching, the system detects pairing errors between Spring and Swagger annotations, resolving program exceptions caused by incorrect annotation configuration in the Spring framework, and improving detection accuracy and troubleshooting efficiency.

CN116820985BActive Publication Date: 2026-11-17RAISECOM TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310802057.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-06-30
Publication Date
2026-11-17
Estimated Expiration
2043-06-30

AI Technical Summary

Technical Problem

In Spring framework development, the static configuration feature of Annotations can cause errors in property parameter configuration. The program may not report errors during compilation but will encounter exceptions during runtime, making troubleshooting difficult, especially in large projects where manually finding the problem is challenging.

Method used

The code files are scanned in reverse order, Spring and Swagger annotations are cached, annotation combinations are matched using a hash mapping linked list, annotation pairing errors are detected, and the error lines are output.

Benefits of technology

It improves the accuracy of annotation configuration error detection, resolves program exceptions caused by incorrect configuration, and simplifies the troubleshooting process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116820985B_ABST
    Figure CN116820985B_ABST
Patent Text Reader

Abstract

This application provides a method, apparatus, and device for detecting annotation configuration errors. The method includes: obtaining and caching a first annotation in the Spring framework that maps different types of requests to corresponding methods; obtaining and caching a second annotation in Swagger that generates interfaces for different types of requests; obtaining a code project file and scanning the code line by line using a reverse scanning method to determine whether the cached first annotation is detected; extracting the code line containing the first annotation and the code line before the next detection of the first annotation as an information block each time it is detected; determining the target information block containing the second annotation and pairing the first and second annotations in the target information block; and determining whether a pairing error exists. This application can solve the problem of quickly troubleshooting scenarios where incorrect configuration of Swagger annotations and Spring Web annotations leads to program exceptions.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of software development technology, and in particular to a method, apparatus and device for detecting annotation configuration errors. Background Technology

[0002] The Spring framework is a lightweight, enterprise-grade, one-stop solution created by Rod Johnson. Previously, most Java frameworks, including the Spring framework, used XML (eXtensible Markup Language) files as configuration files. However, as projects grew larger and business logic became more complex, the Spring framework, while providing convenience to developers, also had some pain points, such as projects being filled with numerous configuration files. With technological evolution, program development increasingly favored a "zero-configuration" approach, and currently, most programs use annotations to replace XML file configuration.

[0003] Annotations are special markers in code that can be read during compilation, class loading, and runtime, and corresponding processing can be performed.

[0004] In software development, whether it's front-end or back-end development, a clear set of API documentation is essential. However, writing and maintaining API documentation is time-consuming and often falls short of updates. Swagger technology effectively solves this API documentation problem. When services are defined using Swagger, consumers can interact with remote services with minimal logic.

[0005] When developing web applications using the Spring framework, various annotations are used to accomplish different functionalities. Swagger also uses annotations to generate API (Application Programming Interface) information for the server-side controller. When these different types of annotations are used together, incorrect configuration of certain attribute parameters can lead to program exceptions or failures. Furthermore, due to the static configuration nature of annotations, incorrect configuration of attribute parameters in "name=value" structures will not result in errors during compilation, but will cause exceptions or failures during runtime. Moreover, after an error occurs, the source code is not clearly identified, making troubleshooting difficult. This is especially true for large projects with numerous program files, making manual problem finding exceptionally challenging. Summary of the Invention

[0006] This application provides a method, apparatus, and device for detecting annotation configuration errors, which solves the problem that existing programs, after reporting errors, cannot clearly point to the code causing the errors, making troubleshooting difficult.

[0007] According to a first aspect of the embodiments of this application, a method for detecting annotation configuration errors is provided, comprising:

[0008] Get the first annotation in the Spring framework that maps different types of requests to corresponding methods and cache it; get the second annotation in Swagger that defines interfaces that generate different types of requests and cache it.

[0009] Obtain the code project file and scan the code line by line using a reverse scanning method to determine whether the cached first annotation is detected.

[0010] Each time the first annotation is encountered during scanning, the line of code containing the first annotation and the line of code preceding the next encounter with the first annotation are extracted as an information block;

[0011] Identify the target information block containing the second annotation, and combine the first and second annotations in the target information block as a pair;

[0012] Determine if there are any pairing errors in the first and second annotations of the paired combination.

[0013] As an optional implementation, the method further includes:

[0014] Obtain the interface request method attribute configuration from the second annotation in the target information block;

[0015] Compare the format of the interface request method attribute configuration with the format of the interface request method attribute configuration of the second annotation in the cache;

[0016] Based on the comparison results, determine whether there is an error in the format of the second annotation configuration information.

[0017] As an optional implementation, the different types of requests include HTTP GET requests of type GET, HTTP PUT requests of type PUT, HTTP POST requests of type POST, and HTTP DELETE requests of type DELETE.

[0018] Specifically, by setting the interface request method attribute of the second annotation to the type of the different requests, different types of request interfaces are configured.

[0019] As an optional implementation, each time the first annotation is scanned, the code line containing the first annotation and the code lines preceding the next scan of the first annotation are extracted as an information block, including:

[0020] Each time the first annotation is encountered, a new hash map linked list is created using the content of the current line of code as the key.

[0021] The content from the line of code where the first annotation was last scanned to the line before the first annotation in the current scan is recorded as a linked list in the hash map linked list created when the first annotation was last scanned.

[0022] As an optional implementation, a target information block containing the second annotation is determined, and the first and second annotations in the target information block are paired and combined, including:

[0023] Traverse the information blocks and determine the target information block containing the second annotation;

[0024] For each target information block, a corresponding entity object is created, and the first attribute variable of the entity object is set to the first annotation in the corresponding target information block, and the second attribute variable of the entity object is set to the second annotation in the corresponding target information block;

[0025] Determine if there are pairing errors in the first and second annotations of the paired combination, including:

[0026] Determine whether there is a pairing error between the first attribute variable value and the second attribute variable value of each entity object by checking the entity class.

[0027] As an optional implementation, the code project files are obtained, including:

[0028] Get the code project file tree;

[0029] The code project files are obtained by filtering the filenames that meet the preset conditions in the code project file tree.

[0030] As an optional implementation, it further includes at least one of the following steps:

[0031] If a pairing error is found between the first and second annotations in a pairing combination, output the line of code where the pairing error occurred.

[0032] If a second annotation configuration information is found to have an incorrect format, output the line of code containing the incorrect second annotation.

[0033] When the value of the first attribute variable or the value of the second attribute variable in the entity object is determined to be empty, the line of code corresponding to the empty value is output.

[0034] According to a second aspect of the embodiments of this application, an apparatus for detecting annotation configuration errors is provided, comprising:

[0035] The annotation caching module is used to retrieve and cache the first annotation in the Spring framework that maps different types of requests to corresponding methods, and to retrieve and cache the second annotation in Swagger that generates different types of requests.

[0036] The reverse scanning module is used to obtain the code project file and scan the code line by line in a reverse scanning manner to determine whether the cached first annotation is detected.

[0037] The information block output module is used to extract the code line containing the first annotation and the code line before the next scan of the first annotation as an information block each time the first annotation is encountered.

[0038] An annotation pairing module is used to determine a target information block containing a second annotation, and to pair the first annotation and the second annotation in the target information block as a pairing combination;

[0039] The first error detection module is used to determine whether there is a pairing error in the first and second annotations in the pairing combination.

[0040] According to a third aspect of the embodiments of this application, a device for detecting annotation configuration errors is provided, comprising: a memory and a processor; wherein:

[0041] The memory is used to store computer programs;

[0042] The processor is used to read the program in the memory and execute the method for detecting annotation configuration errors provided in the above embodiments.

[0043] According to a fourth aspect of the embodiments of this application, a chip is provided, the chip being coupled to a memory in a device, such that the chip, during operation, calls program instructions stored in the memory to implement the above-described aspects of the embodiments of this application and any possible methods involved in each aspect.

[0044] According to a fifth aspect of the embodiments of this application, a computer-readable storage medium is provided, which stores program instructions that, when run on a computer, cause the computer to perform the above-described aspects of the embodiments of this application and any possible methods involved in each aspect.

[0045] According to a sixth aspect of the embodiments of this application, a computer program product is provided that, when the computer program product is run on an electronic device, causes the electronic device to perform methods that implement the above-described aspects of the embodiments of this application and any possible methods involved in each aspect.

[0046] The method, apparatus, and equipment for detecting annotation configuration errors provided in this application have the following advantages:

[0047] This application uses reverse order to traverse the code project files, which effectively solves the problem of missing or causing incorrect connections between related annotations when reading sequentially, thus improving the accuracy of checking program annotations. In addition, this application can handle scenarios where incorrect configuration of Swagger Annotation and Spring Web Annotation leads to program exceptions. Attached Figure Description

[0048] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0049] Figure 1 This is a flowchart of a method for detecting annotation configuration errors provided in an embodiment of this application;

[0050] Figure 2 This is a detailed flowchart of a method for detecting annotation configuration errors provided in the embodiments of this application;

[0051] Figure 3 This is a schematic diagram of the device structure for detecting annotation configuration errors provided in the embodiments of this application;

[0052] Figure 4 This is a schematic diagram of a device for detecting incorrect annotation configuration provided in the embodiments of this application. Detailed Implementation

[0053] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0054] Currently, an increasing number of software development companies are adopting lightweight Java EE (Java Platform Enterprise Edition) development platforms centered around the Spring framework in their enterprise project development. Industries such as banking, telecommunications, securities, and e-commerce have all unanimously chosen the Java EE platform as their enterprise development platform. The Spring framework is a lightweight, enterprise-level, one-stop solution created by Rod Johnson. Previously, most Java frameworks, including the Spring framework, used XML files as configuration files. However, as projects have become larger and more complex, the Spring framework, while providing convenience to developers, also has some pain points, such as projects being filled with a large number of configuration files. With technological evolution, program development is increasingly inclined towards a "zero-configuration" approach, and currently, most programs use annotations to replace XML file configuration.

[0055] Annotations are special markers in code that can be read and processed during compilation, class loading, and runtime. By using annotations, developers can embed supplementary information into source files without altering the original logic. Code analysis tools, development tools, and deployment tools can use this supplementary information for verification or deployment. Annotations provide a way to set metadata for program elements. In some ways, annotations are used like modifiers to modify the declarations of packages, classes, constructors, methods, member variables, and local variables. This information is stored in the name and value of the "name=value" structure of the annotation. Annotation is an interface; programs can obtain the annotation object of a specified program through reflection and then retrieve the metadata within the annotation through the annotation object.

[0056] In software development, whether front-end or back-end, a clear set of API documentation is essential. However, writing and maintaining API documentation is time-consuming and often falls short of updates. Swagger technology effectively solves this problem. Swagger is an online software that automatically generates RESTful API documentation and provides functional testing capabilities. Swagger aims to define a standard, language-independent interface for REST APIs, enabling humans and computers to discover and understand the functionality of various services without seeing the source code, documentation, or network traffic analysis. When services are defined using Swagger, consumers can interact with remote services with minimal logic.

[0057] When developing web applications using the Spring framework, various annotations are used to accomplish different functionalities. Swagger also uses annotations to generate API information for the server-side controller (controller layer) functional interfaces. When these different types of annotations are used together, incorrect configuration of certain attribute parameters can lead to program exceptions or failures. Furthermore, due to the static configuration nature of annotations, if attribute parameters are misconfigured in the "name=value" structure of an annotation, no error will be reported during compilation, but exceptions or failures will occur during runtime. Moreover, after an error occurs, it's difficult to clearly pinpoint the code causing the problem, making troubleshooting challenging. This is especially true for large projects involving numerous program files, making manual problem finding exceptionally difficult.

[0058] In view of this, embodiments of this application propose a method for detecting annotation configuration errors, such as... Figure 1 As shown, the method includes:

[0059] Step 101: Obtain and cache the first annotation in the Spring framework that maps different types of requests to corresponding methods, and obtain and cache the second annotation in Swagger that defines the interface for generating different types of requests;

[0060] When developing web applications, the basic interaction between the front-end and back-end is through Hypertext Transfer Protocol (HTTP) requests. The main HTTP request methods are GET (send a request to a specific resource), POST (submit data to a specified resource for processing), PUT (upload the latest content of a specified resource), DELETE (request the server to delete the resource identified by the requested URI), HEAD (send a request to a specific resource to retrieve its headers), OPTIONS (check if the server accepts the request), TRACE (echo the request received by the server), and CONNECT (reserved in the HTTP / 1.1 protocol for proxy servers that can switch the connection to a piped mode).

[0061] When developing programs based on the Spring framework, only four types of request methods are typically used: GET, POST, PUT, and DELETE.

[0062] As an optional implementation, the different types of requests mentioned above include HTTP GET requests of type GET, HTTP PUT requests of type PUT, HTTP POST requests of type POST, and HTTP DELETE requests of type DELETE.

[0063] Spring defines a first annotation for each of these four request types, mapping the corresponding request type to the corresponding method. In this embodiment, the first annotation includes:

[0064] @GetMapping is used to map an HTTP GET request to a specified handler method;

[0065] @PostMapping is used to map an HTTP POST request to a specified processing method;

[0066] @PutMapping is used to map HTTP PUT requests to controller methods;

[0067] @DeleteMapping is one of four annotations used to map HTTP DELETE requests to specific methods that handle resource deletion.

[0068] As an optional implementation, this embodiment of the application configures different types of request interfaces by setting the interface request method attribute of the second annotation to the type of the different requests.

[0069] In other words, Swagger uses the `@ApiOperation` annotation (HTTP method for request operations) with the `httpMethod` attribute (interface request method), assigning four configurations: `httpMethod="GET", httpMethod="POST", httpMethod="PUT", and httpMethod="DELETE"`, to describe the application's function request interface.

[0070] Of course, the type of request is not limited to the four types mentioned above; it can be any of the various types of HTTP requests defined above.

[0071] Step 102: Obtain the code project file and scan the code line by line using a reverse scan method to determine whether the cached first annotation has been scanned;

[0072] In this step, the scan begins at the end of the code project file.

[0073] As an optional implementation, obtaining code project files includes: obtaining a code project file tree; and obtaining code project files by filtering the filenames in the code project file tree that meet preset conditions.

[0074] Since the code project file tree is a hierarchical collection of code project files, files matching the preset criteria can be identified by filtering and searching filenames within the tree. For example, filtering for the xxController.java file is possible because, according to the Spring Web project layering principle, xxService is used for business logic implementation, xxController for request control, and xxDao for database operations. Different levels of business processing are each annotated with corresponding comments. In this step, the preset criteria can be names determined according to the different levels of business processing functions.

[0075] In this step, the scanned code project files are read in reverse order, which will yield more accurate matching results compared to using a sequential method. This is because the Swagger @ApiOperation annotation is not required by the program; it is a non-essential configuration feature that can be configured or not. When used in combination with Spring Web request annotations, scanning from the first line of the file to the last line can easily lead to matching errors. For example, a REST request file contains two REST requests, A and B. Request A is annotated with both @ApiOperation and @GetMapping, while request B is only annotated with @PostMapping. Because the file stream is parsed line by line, after reading the `@ApiOperation` record, it needs to continue reading the matching Spring REST annotations. At this point, `@GetMapping` is read first, and then the file stream continues reading line by line. Since the next request B does not have `@ApiOperation`, but only `@PostMapping`, without special marking, the parser will use the `@PostMapping` from request B to replace `@GetMapping`, and combine `@PostMapping` from request B with `@ApiOperation` from request A. This results in an incorrect result because the pairing of `@ApiOperation` and `@GetMapping` becomes a pairing of `@ApiOperation` and `@PostMapping`.

[0076] Step 103: Each time the first annotation is scanned, the code line containing the first annotation and the code line before the next scan of the first annotation are extracted as an information block;

[0077] In the code corresponding to the request method configured with the second annotation, the second annotation comes first and the first annotation comes after. Since the @ApiOperation annotation is not required by the program, that is, it can be configured or not. Furthermore, there may be problems in the configuration process, which may lead to program exceptions when Swagger annotations and Spring Web annotations are used together due to incorrect configuration.

[0078] Step 104: Determine the target information block containing the second annotation, and pair the first annotation and the second annotation in the target information block as a pair;

[0079] To overcome the errors inherent in sequential scanning, this embodiment employs a reverse-order file scanning method, thus avoiding this situation. In the code project file, if a request method is configured with a second annotation, the second annotation comes first, followed by the first annotation. If the request method is not configured with a second annotation, the first annotation is located at the beginning of the method's line. This embodiment uses a reverse scanning method to extract the code line containing the first annotation and the code line before the next scan of the first annotation as an information block. Thus, when the method is configured with a second annotation, the corresponding first and second annotations will appear in the same information block. When the method is not configured with a second annotation, the information block only contains the content of the method corresponding to the next scan of the first annotation, without the second annotation, thus avoiding the pairing errors that occur in sequential scanning. For example, this embodiment first reads @PostMapping, but when scanning upwards, it first reads @GetMapping and then @ApiOperation, thus obtaining the correct combination. This approach requires no additional special processing, improving parsing efficiency.

[0080] Step 105: Determine whether there are any pairing errors in the first and second annotations of the paired combination.

[0081] For request methods of the same type, the type of the first annotation configured by Spring for that request method must match the type of the interface request method attribute of the second annotation in Swagger. In other words, the first and second annotations in a method must be correctly matched. Specifically:

[0082] @ApiOperation(httpMethod="GET") corresponds to the @GetMapping annotation defined in Spring Web.

[0083] @ApiOperation(httpMethod="POST") corresponds to the @PostMapping annotation defined in Spring Web;

[0084] @ApiOperation(httpMethod="PUT") corresponds to the @PutMapping annotation defined in Spring Web;

[0085] @ApiOperation(httpMethod="DELETE") corresponds to the @DeleteMapping annotation defined in Spring Web.

[0086] The program will only achieve the correct result when these annotation parameters are configured correctly and combined in pairs.

[0087] Since the `@ApiOperation` annotation is not mandatory for the program (meaning it can be configured or not), and configuration issues can arise during the configuration process, incorrect configuration can lead to program exceptions when Swagger annotations and Spring Web annotations are used together. This application uses reverse traversal of the code project files, effectively solving the problem of missing or incorrectly linking related annotations during sequential reading, thus improving the accuracy of annotation checking. Furthermore, this application can refer to the correct pairing method described above for pairing detection, effectively handling scenarios where incorrect pairing of Swagger annotations and Spring Web annotations due to configuration issues causes program exceptions.

[0088] Even if the pairing is correct, the interface request method attribute configuration in the second annotation may still have formatting errors, such as an extra space. As an optional implementation method, this method also includes:

[0089] Obtain the interface request method attribute configuration from the second annotation in the target information block;

[0090] Compare the format of the interface request method attribute configuration with the format of the interface request method attribute configuration of the second annotation in the cache;

[0091] Based on the comparison results, determine whether there is an error in the format of the second annotation configuration information.

[0092] The information block retrieval method implemented in this application embodiment can be achieved by creating a hash mapping linked list. As an optional implementation method, each time the first annotation is scanned, the code line containing the first annotation and the code lines before the next scan of the first annotation are retrieved as the information block, including:

[0093] Each time the first annotation is encountered, the content of the current line of code is used as the key to create a new hash map linked list. After creating the new hash map linked list, the scanning can continue upwards until the first annotation is encountered again. The content of the scanned line of code is put into the newly created hash map linked list.

[0094] The content from the line of code where the first annotation was last scanned to the line before the first annotation in the current scan is recorded as a linked list in the hash map linked list created when the first annotation was last scanned.

[0095] In other words, the code lines scanned before the first annotation were encountered in this scan, along with the code lines scanned in the previous scan that encountered the first annotation, were all placed into the hash map linked list created when the first annotation was encountered in the previous scan.

[0096] In this embodiment of the application, to extract paired combinations from information blocks, as an optional implementation, a target information block containing a second annotation is determined, and the first and second annotations in the target information block are used as paired combinations, including:

[0097] Traverse the information blocks and determine the target information block containing the second annotation;

[0098] For each target information block, a corresponding entity object is created, and the first attribute variable of the entity object is set to the first annotation in the corresponding target information block, and the second attribute variable of the entity object is set to the second annotation in the corresponding target information block;

[0099] Determine if there are pairing errors in the first and second annotations of the paired combination, including:

[0100] Determine whether there is a pairing error between the first attribute variable value and the second attribute variable value of each entity object by checking the entity class.

[0101] As mentioned earlier, when the information block is stored using a hash-mapped linked list, the hash-mapped linked list is traversed, and the corresponding linked list content is retrieved based on each key value. It is then determined whether the linked list content includes the code line for `@ApiOperation`. If so, an entity object (APIOperation) is defined for each key value. This entity object has two attribute variables: a second attribute variable `apiOperation` and a first attribute variable `request`, where:

[0102] The first attribute variable, `request`, stores the key value (the line containing annotations such as `@GetMapping` and `@PostMapping`). The second attribute variable, `apiOperation`, stores the line containing records with `@ApiOperation` annotations. The entity objects are stored in a linked list of entity objects `ControllerModel`, where each `ControllerModel` entity has a separate linked list `List` dedicated to storing `APIOperation` entities. <apioperation>apiOperationList.

[0103] The entity class determines whether there is a pairing error between the first and second attribute variable values ​​of each entity object. The role of this entity class is to pair the content of the lines containing annotations such as @GetMapping and @PostMapping with the content of the lines containing annotations such as @ApiOperation according to the correct pairing method described above.

[0104] The following is a detailed flowchart of the method for detecting annotation configuration errors according to an embodiment of this application, taking the four types of request methods mentioned above as examples, in conjunction with the accompanying drawings. Figure 2 As shown, the method includes:

[0105] Step 200: Obtain all the correct annotations for @ApiOperation(httpMethod="xxx") and store them in the application cache;

[0106] All annotations for the correct @ApiOperation(httpMethod="xxx") can be obtained by enumerating them from an external source, or they can be pre-stored in a set location and read directly when needed.

[0107] Among them, httpMethod="xxx" includes httpMethod="GET", httpMethod="POST", httpMethod="PUT" and httpMethod="DELETE";

[0108] Step 201: Obtain the correct Spring Web annotations and store @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping in the application cache;

[0109] The correct Spring Web annotations can be obtained by enumerating them from an external source, or they can be pre-stored in a set location and read directly when needed.

[0110] Step 202: Determine the code project files that meet the preset conditions;

[0111] Step 203: Read the determined code project file by scanning upwards line by line in reverse order;

[0112] Step 204: Determine whether the current line contains the first annotation of the four types: @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping. If yes, proceed to step 205; otherwise, proceed to step 203.

[0113] If the current line contains any of the annotations @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping, then proceed to step 205;

[0114] Step 205: Determine if a hash mapping chain exists where the code line content of the previous scan that contains the @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping annotations is used as the key. If yes, proceed to step 207; otherwise, proceed to step 206.

[0115] In this step, the code line content used as the key is a string containing the annotations @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping. This key corresponds to the client's request operation: GET, POST, PUT, or DELETE.

[0116] Step 206: Define a hash map linked list with the current line content as the key and the hash value as empty; return to execute step 203.

[0117] Step 207: Create a linked list containing the code lines from the last scanned line containing the @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping annotation to the current line before the @GetMapping, @PostMapping, @PutMapping, or @DeleteMapping annotation appears.

[0118] In steps 206 and 207 above, the key and the linked list content are respectively corresponding. Specifically, one key corresponds to each request operation method, and thus corresponds to one linked list content.

[0119] Further explanation: Because a request method is always annotated with a string containing `@GetMapping`, `@PostMapping`, `@PutMapping`, or `@DeleteMapping`, this can be used as the starting point of a request method. The lines preceding the next `@GetMapping`, `@PostMapping`, `@PutMapping`, or `@DeleteMapping` annotation string are all code records of a request method. Therefore, in this application, the line containing the `@GetMapping`, `@PostMapping`, `@PutMapping`, or `@DeleteMapping` annotation string can be used as the starting marker for a request method. This application also defines a hash-mapped linked list to extract all lines of code for a request method. The key is the content of the current line, and the hash value is a linked list used to store the line content of the current request method. Until the program reads the next `@GetMapping`, `@PostMapping`, `@PutMapping`, or `@DeleteMapping` annotation string, the current key belongs to the same method, so the read line records will be correctly stored in the linked list corresponding to the current key.

[0120] Step 208: Determine if the current line is the first line. If it is, proceed to step 209; otherwise, proceed to step 206.

[0121] In this step, if the current row is already the first row, it means that the current filtering search has ended.

[0122] Step 209: Traverse the hash map linked list, retrieve the corresponding linked list content from the hash map linked list according to each key value, and determine whether the record line of the linked list content contains a line of code starting with @ApiOperation. If so, proceed to step 210; otherwise, this application does not involve this.

[0123] In this step, to determine whether a record row in the linked list contains a line of code starting with @ApiOperation, it is necessary to check the linked list content corresponding to each key value in the hash mapping linked list. For multiple key values, it is necessary to check whether the record rows in multiple linked lists contain a line of code starting with @ApiOperation. The result of the check is also one for each linked list content corresponding to each key value.

[0124] Step 210: Define an entity object (APIOperation) for each key value. The entity object has two attribute variables, apiOperation and request. The content of the line containing annotations such as @GetMapping and @PostMapping is paired with the content of the line containing annotations such as @ApiOperation through the entity class.

[0125] The aforementioned entity objects are stored in a linked list of entity objects (ControllerModel). (The ControllerModel entity has a dedicated linked list, List, for storing APIOperation entities.) <apioperation>(apiOperationList).

[0126] Step 211: In the entity object list (ControllerModel:apiOperationList), traverse the entity list and determine the corresponding key value (APIOperation:request) for each entity object in the entity object list. If the value is empty, the code has an error in the use of the current request annotation, and proceed to step 213; otherwise, proceed to step 212.

[0127] Because all annotations are predefined and fixed interface definitions, and the @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping annotations are required for defining client requests; if they are missing, the client request program will not receive them.

[0128] Step 212: Determine whether the variable describing the row content of the @ApiOperation record for each entity object (i.e., the httpMethod attribute of the annotation) is consistent with all annotations of @ApiOperation(httpMethod = "xxx") in the cache. If they are consistent, it means that the data is consistent and the reason for the program error is not involved. Otherwise, proceed to step 213.

[0129] Step 213: Log errors in the console or file. Specifically, print the incorrect lines of code to the console or output them to a text file for logging. This allows program developers to check the content logged in the console or file and correct the corresponding files if any erroneous annotations are used.

[0130] In related technologies, reading a file typically involves reading and parsing it sequentially from the first line to the last. However, due to the flexible configuration of program annotations, sequential reading and parsing often misses or leads to incorrect connections between related annotations. This application solves this problem by traversing the program file in reverse order, effectively improving the accuracy of annotation checking. Furthermore, this application not only addresses scenarios where incorrect configuration of Swagger Annotations and Spring Web Annotations causes program exceptions, but can also be applied to resolve program exceptions caused by incorrect annotation configurations in other programs.

[0131] Please see Figure 3 This application provides an apparatus for detecting annotation configuration errors, comprising:

[0132] Annotation caching module 301 is used to retrieve and cache the first annotation in the Spring framework that maps different types of requests to corresponding methods, and to retrieve and cache the second annotation in Swagger that generates different types of requests.

[0133] The reverse scanning module 302 is used to obtain the code project file and scan the code line by line in a reverse scanning manner to determine whether the cached first annotation is detected.

[0134] The information block output module 303 is used to extract the code line containing the first annotation and the code line before the next scan of the first annotation as an information block each time the first annotation is scanned;

[0135] Annotation pairing module 304 is used to determine a target information block containing a second annotation, and to pair the first annotation and the second annotation in the target information block as a pairing combination;

[0136] The first error detection module 305 is used to determine whether there is a pairing error in the first annotation and the second annotation in the pairing combination.

[0137] As an optional implementation, it also includes:

[0138] The second error detection module is used to obtain the interface request method attribute configuration in the second annotation in the target information block; compare the format of the interface request method attribute configuration with the format of the interface request method attribute configuration of the second annotation in the cache; and determine whether there is a format error in the second annotation configuration information based on the comparison result.

[0139] As an optional implementation, the different types of requests include HTTP GET requests of type GET, HTTP PUT requests of type PUT, HTTP POST requests of type POST, and HTTP DELETE requests of type DELETE.

[0140] Specifically, by setting the interface request method attribute of the second annotation to the type of the different requests, different types of request interfaces are configured.

[0141] As an optional implementation, each time the first annotation is scanned, the information block output module extracts the code line containing the first annotation and the code lines before the next scan of the first annotation as an information block, including:

[0142] Each time the first annotation is encountered, a new hash map linked list is created using the content of the current line of code as the key.

[0143] The content from the line of code where the first annotation was last scanned to the line before the first annotation in the current scan is recorded as a linked list in the hash map linked list created when the first annotation was last scanned.

[0144] As an optional implementation, the annotation pairing module determines a target information block containing a second annotation, and pairs the first and second annotations in the target information block as a combination, including:

[0145] Traverse the information blocks and determine the target information block containing the second annotation;

[0146] For each target information block, a corresponding entity object is created, and the first attribute variable of the entity object is set to the first annotation in the corresponding target information block, and the second attribute variable of the entity object is set to the second annotation in the corresponding target information block;

[0147] The first error detection module determines whether there are pairing errors in the first and second annotations of the paired combination, including:

[0148] Determine whether there is a pairing error between the first attribute variable value and the second attribute variable value of each entity object by checking the entity class.

[0149] As an optional implementation, the reverse scan module obtains the code project files, including:

[0150] Get the code project file tree;

[0151] The code project files are obtained by filtering the filenames that meet the preset conditions in the code project file tree.

[0152] As an optional implementation, it also includes:

[0153] The error output module is used to perform at least one of the following steps:

[0154] If a pairing error is found between the first and second annotations in a pairing combination, output the line of code where the pairing error occurred.

[0155] If a second annotation configuration information is found to have an incorrect format, output the line of code containing the incorrect second annotation.

[0156] When the value of the first attribute variable or the value of the second attribute variable in the entity object is determined to be empty, the line of code corresponding to the empty value is output.

[0157] Please see Figure 4 The embodiments of the device for detecting annotation configuration errors in this application include:

[0158] like Figure 4 As shown, the electronic device 140 is presented in the form of a general-purpose electronic device. The components of the electronic device 140 may include, but are not limited to: at least one processor 141, at least one memory 142, and a bus 143 connecting different system components (including the memory 142 and the processor 141). The processor is used to read and execute programs from the memory.

[0159] Get the first annotation in the Spring framework that maps different types of requests to corresponding methods and cache it; get the second annotation in Swagger that defines interfaces that generate different types of requests and cache it.

[0160] Obtain the code project file and scan the code line by line using a reverse scanning method to determine whether the cached first annotation is detected.

[0161] Each time the first annotation is encountered during scanning, the line of code containing the first annotation and the line of code preceding the next encounter with the first annotation are extracted as an information block;

[0162] Identify the target information block containing the second annotation, and combine the first and second annotations in the target information block as a pair;

[0163] Determine if there are any pairing errors in the first and second annotations of the paired combination.

[0164] As an optional implementation, the processor described above is further used for:

[0165] Obtain the interface request method attribute configuration from the second annotation in the target information block;

[0166] Compare the format of the interface request method attribute configuration with the format of the interface request method attribute configuration of the second annotation in the cache;

[0167] Based on the comparison results, determine whether there is an error in the format of the second annotation configuration information.

[0168] As an optional implementation, the different types of requests include HTTP GET requests of type GET, HTTP PUT requests of type PUT, HTTP POST requests of type POST, and HTTP DELETE requests of type DELETE.

[0169] The processor configures different types of request interfaces by setting the interface request method attribute of the second annotation to the type of the different requests.

[0170] As an optional implementation, each time the processor encounters the first annotation, it extracts the code line containing the first annotation and the code lines preceding the next encounter with the first annotation as an information block, including:

[0171] Each time the first annotation is encountered, a new hash map linked list is created using the content of the current line of code as the key.

[0172] The content from the line of code where the first annotation was last scanned to the line before the first annotation in the current scan is recorded as a linked list in the hash map linked list created when the first annotation was last scanned.

[0173] As an optional implementation, the processor determines a target information block containing a second annotation and pairs the first and second annotations in the target information block, including:

[0174] Traverse the information blocks and determine the target information block containing the second annotation;

[0175] For each target information block, a corresponding entity object is created, and the first attribute variable of the entity object is set to the first annotation in the corresponding target information block, and the second attribute variable of the entity object is set to the second annotation in the corresponding target information block;

[0176] Determine if there are pairing errors in the first and second annotations of the paired combination, including:

[0177] Determine whether there is a pairing error between the first attribute variable value and the second attribute variable value of each entity object by checking the entity class.

[0178] As an optional implementation, the processor acquires the code project file, including:

[0179] Get the code project file tree;

[0180] The code project files are obtained by filtering the filenames that meet the preset conditions in the code project file tree.

[0181] As an optional implementation, the processor is also configured to perform at least one of the following steps:

[0182] If a pairing error is found between the first and second annotations in a pairing combination, output the line of code where the pairing error occurred.

[0183] If a second annotation configuration information is found to have an incorrect format, output the line of code containing the incorrect second annotation.

[0184] When the value of the first attribute variable or the value of the second attribute variable in the entity object is determined to be empty, the line of code corresponding to the empty value is output.

[0185] Bus 143 represents one or more of several bus structures, including a memory bus or memory controller, peripheral bus, processor, or local bus using any of the various bus structures.

[0186] The memory 142 may include a readable medium in the form of volatile memory, such as random access memory (RAM) 1421 and / or cache memory 1422, and may further include read-only memory (ROM) 1423.

[0187] The memory 142 may also include a program / utility 1425 having a set (at least one) of program modules 1424, including but not limited to: an operating system, one or more application programs, other program modules, and program data, each or some combination of these examples may include an implementation of a network environment.

[0188] Electronic device 140 can also communicate with one or more external devices 144 (e.g., keyboard, pointing device, etc.), and with one or more devices that enable a user to interact with electronic device 140, and / or with any device that enables electronic device 140 to communicate with one or more other electronic devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 145. Furthermore, electronic device 140 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 146. As shown, network adapter 146 communicates with other modules used in electronic device 140 via bus 143. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 140, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.

[0189] In some possible implementations, various aspects of the method for detecting annotation configuration errors provided in this application can also be implemented in the form of a program product, which includes program code that, when the program product is run on a computer device, causes the computer device to perform the steps of the method for detecting annotation configuration errors according to various exemplary embodiments of this application described above.

[0190] The program product may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples (a non-exhaustive list) of readable storage media include: electrical connections having one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.

[0191] The program product of the embodiments of this application may employ a portable compact disc read-only memory (CD-ROM) and include program code, and may run on an electronic device. However, the program product of this application is not limited thereto. In this document, the readable storage medium may be any tangible medium that contains or stores a program that may be used by or in conjunction with an instruction execution system, apparatus, or device.

[0192] A readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying readable program code. This propagated data signal may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable signal medium may also be any readable medium other than a readable storage medium, capable of sending, propagating, or transmitting a program for use by or in conjunction with an instruction execution system, apparatus, or device.

[0193] The program code contained on the readable medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.

[0194] Program code for performing the operations of this application can be written in any combination of one or more programming languages, including object-oriented programming languages ​​such as Java and C++, and conventional procedural programming languages ​​such as C or similar languages. The program code can execute entirely on the user's electronic device, partially on the user's device, as a standalone software package, partially on the user's electronic device and partially on a remote electronic device, or entirely on a remote electronic device or server. In cases involving remote electronic devices, the remote electronic device can be connected to the user's electronic device via any type of network—including a local area network (LAN) or a wide area network (WAN)—or can be connected to an external electronic device (e.g., via the Internet using an Internet service provider).

[0195] It should be noted that although several units or sub-units of the device have been mentioned in the detailed description above, this division is merely exemplary and not mandatory. In fact, according to embodiments of this application, the features and functions of two or more units described above can be embodied in one unit. Conversely, the features and functions of one unit described above can be further divided and embodied by multiple units.

[0196] Furthermore, although the operations of the method of this application are described in a specific order in the accompanying drawings, this does not require or imply that these operations must be performed in that specific order, or that all the operations shown must be performed to achieve the desired result. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into one step, and / or one step may be broken down into multiple steps.

[0197] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0198] This application is described with reference to flowchart illustrations and block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block and / or block in the flowchart illustrations and block diagrams, as well as combinations of blocks and processes in the flowchart illustrations and block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the process. Figure 1 One or more processes and boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0199] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and boxes Figure 1 The function specified in one or more boxes.

[0200] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and boxes Figure 1 The steps of the function specified in one or more boxes.

[0201] Although preferred embodiments of this application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this application.

[0202] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the spirit and scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.< / apioperation> < / apioperation>

Claims

1. A method for detecting annotation configuration errors, characterized in that, include: Get the first annotation in the Spring framework that maps different types of requests to corresponding methods and cache it; get the second annotation in Swagger that defines interfaces that generate different types of requests and cache it. Obtain the code project file and scan the code line by line using a reverse scanning method to determine whether the cached first annotation is detected. Each time the first annotation is encountered during scanning, the line of code containing the first annotation and the line of code preceding the next encounter with the first annotation are extracted as an information block; Identify the target information block containing the second annotation, and combine the first and second annotations in the target information block as a pair; Determine if there are any pairing errors in the first and second annotations of the paired combination; The different types of requests include HTTP GET requests of type GET, HTTP PUT requests of type PUT, HTTP POST requests of type POST, and HTTP DELETE requests of type DELETE. Specifically, by setting the interface request method attribute of the second annotation to different request types, different types of request interfaces can be configured.

2. The method according to claim 1, characterized in that, Also includes: Obtain the interface request method attribute configuration from the second annotation in the target information block; Compare the format of the interface request method attribute configuration with the format of the interface request method attribute configuration of the second annotation in the cache; Based on the comparison results, determine whether there is an error in the format of the second annotation configuration information.

3. The method according to claim 1, characterized in that, Each time the first annotation is encountered during scanning, the line of code containing the first annotation and the lines of code preceding the next encounter with the first annotation are extracted as an information block, including: Each time the first annotation is encountered, a new hash map linked list is created using the content of the current line of code as the key. The content from the line of code where the first annotation was last scanned to the line before the first annotation in the current scan is recorded as a linked list in the hash map linked list created when the first annotation was last scanned.

4. The method according to claim 1, characterized in that, Identify the target information block containing the second annotation, and pair the first and second annotations in the target information block as a group, including: Traverse the information blocks and determine the target information block containing the second annotation; For each target information block, a corresponding entity object is created, and the first attribute variable of the entity object is set to the first annotation in the corresponding target information block, and the second attribute variable of the entity object is set to the second annotation in the corresponding target information block; Determine if there are pairing errors in the first and second annotations of the paired combination, including: Determine whether there is a pairing error between the first attribute variable value and the second attribute variable value of each entity object by checking the entity class.

5. The method according to claim 1, characterized in that, Obtain the code project files, including: Get the code project file tree; The code project files are obtained by filtering the filenames that meet the preset conditions in the code project file tree.

6. The method according to any one of claims 1 to 5, characterized in that, It also includes at least one of the following steps: If a pairing error is found between the first and second annotations in a pairing combination, output the line of code where the pairing error occurred. If a second annotation configuration information is found to have an incorrect format, output the line of code containing the incorrect second annotation. When the value of the first attribute variable or the value of the second attribute variable in the entity object is determined to be empty, the line of code corresponding to the empty value is output.

7. A device for detecting annotation configuration errors, characterized in that, include: The annotation caching module is used to retrieve and cache the first annotation in the Spring framework that maps different types of requests to corresponding methods, and to retrieve and cache the second annotation in Swagger that generates different types of requests. The reverse scanning module is used to obtain the code project file and scan the code line by line in a reverse scanning manner to determine whether the cached first annotation is detected. The information block output module is used to extract the code line containing the first annotation and the code line before the next scan of the first annotation as an information block each time the first annotation is encountered. An annotation pairing module is used to determine a target information block containing a second annotation, and to pair the first annotation and the second annotation in the target information block as a pairing combination; The first error detection module is used to determine whether there is a pairing error in the first and second annotations in the pairing combination; The different types of requests include HTTP GET requests of type GET, HTTP PUT requests of type PUT, HTTP POST requests of type POST, and HTTP DELETE requests of type DELETE. Specifically, by setting the interface request method attribute of the second annotation to different request types, different types of request interfaces can be configured.

8. A device for detecting annotation configuration errors, characterized in that, include: Memory and processor; The memory is used to store computer programs; The processor is used to read the program in the memory and execute the steps of the method as described in any one of claims 1 to 6.

9. A computer program medium, characterized in that, It stores a computer program, characterized in that when the program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • API interface class verification method, system and device

    CN112241370A

  • Judgment method, device and equipment for same vulnerability records and readable storage medium

    CN116226862A