Method and device for generating type declaration file based on swagger document

By traversing the JSON data in the Swagger document, extracting and generating the input and output parameter information of the interface, it solves the problem that the type declaration file generated by the Swagger-to-ts plug-in is too bloated, and implements a more concise type declaration file, which is convenient for developers. call.

CN113590095BActive Publication Date: 2024-10-25SHANGHAI BILIBILI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110869550.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-07-30
Publication Date
2024-10-25
Estimated Expiration
2041-07-30

AI Technical Summary

Technical Problem

The type declaration file generated by the existing Swagger-to-ts plug-in is too bloated, making it inconvenient to use when calling.

Method used

By obtaining the interface request path of multiple interfaces, traversing the JSON data in the Swagger document, extracting and generating the input and output parameter information of the interface, and generating the type declaration file according to the preset type declaration file generation rules to avoid obtaining fields for all interfaces. data, thereby reducing file bloat.

Benefits of technology

The generated type declaration file is more concise, easier to call, and improves development efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113590095B_ABST
    Figure CN113590095B_ABST
Patent Text Reader

Abstract

The embodiment of the present application provides a method for generating a type declaration file based on a swagger document, the method comprising: obtaining interface request paths of multiple interfaces; obtaining JSON data contained in a swagger document corresponding to a development project; traversing the JSON data according to the interface request paths of multiple interfaces, and obtaining a first data structure contained in the input parameter information of each interface and a second data structure contained in the output parameter information of each interface from the JSON data; obtaining first field data of a preset type from the JSON data according to the first data structure of each interface, and obtaining second field data of the preset type from the JSON data according to the second data structure of each interface; generating a type declaration file of the development project according to a preset first type declaration file generation rule by combining the first field data corresponding to each interface and the second field data corresponding to each interface. The present application can avoid the generated content being too bloated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a method and device for generating a type declaration file based on a swagger document. Background Art

[0002] With the development of Internet technology, the front-end and back-end of websites have begun to separate. In the process of interaction between the front-end and the back-end, the API (Application Programming Interface) interface has become an important channel. Therefore, API documents have become an important link between developers and other staff. Swagger is an API development tool that can be used to write Swagger documents. Swagger documents are documents provided by back-end personnel to front-end developers during the front-end and back-end joint debugging process, which is a common agreement between the two parties.

[0003] In the prior art, after a Swagger document is generated, a type declaration file can be generated from the Swagger document through a Swagger-to-ts plug-in. However, the inventors have found that the existing Swagger-to-ts plug-in generates a file for all types when generating a type declaration file, and then obtains and calls them one by one, resulting in the generated content being too bloated and very inconvenient to call and use. Summary of the invention

[0004] The purpose of the embodiments of the present application is to provide a method and apparatus for generating a type declaration file based on a swagger document, a computer device, and a computer-readable storage medium, so as to solve the problem that the content generated by the existing Swagger-to-ts plug-in is too bloated, resulting in great inconvenience in calling and using.

[0005] One aspect of an embodiment of the present application provides a method for generating a type declaration file based on a swagger document, the method comprising:

[0006] Get the interface request path of multiple interfaces;

[0007] Get the JSON data contained in the swagger document corresponding to the development project;

[0008] Traversing the JSON data according to the interface request paths of the multiple interfaces, and obtaining from the JSON data a first data structure included in the input parameter information of each interface and a second data structure included in the output parameter information of each interface;

[0009] Acquire first field data of a preset type from the JSON data according to the first data structure of each interface, and acquire second field data of the preset type from the JSON data according to the second data structure of each interface;

[0010] The first field data corresponding to each interface and the second field data corresponding to each interface are used to generate a type declaration file of the development project according to a preset first type declaration file generation rule.

[0011] Optionally, the method further comprises:

[0012] Traversing the JSON data according to the interface request paths of the multiple interfaces, and obtaining interface information of each interface from the JSON data, the interface information including description information of the interface, interface request path, input parameter information of the interface, and output parameter information of the interface;

[0013] Generate the front-end interface file of the development project according to the preset second type declaration file generation rule based on the interface information of each interface;

[0014] Insert the front-end interface file into the type declaration file.

[0015] Optionally, the obtaining interface request paths of multiple interfaces includes:

[0016] Obtaining a configuration file of the development project;

[0017] Reading the interface request path screening condition configured by the user from the configuration file;

[0018] The interface request paths of the multiple interfaces are determined according to the interface request path screening condition.

[0019] Optionally, traversing the JSON data according to the interface request path of the multiple interfaces, and obtaining from the JSON data a first data structure included in the input parameter information of each interface and a second data structure included in the output parameter information of each interface includes:

[0020] Traverse the JSON data according to the interface request path, and find the input parameter information and output parameter information of the first interface from the JSON data;

[0021] Respectively obtaining a first data structure and a second data structure from the input parameter information and the output parameter information of the first interface;

[0022] Continue to traverse the JSON data according to the interface request path, and obtain the input parameter information and output parameter information of the next interface from the JSON data, and obtain the first data structure and the second data structure from the input parameter information and the output parameter information of the next interface, until the first data structure and the second data structure of all interfaces are obtained.

[0023] Optionally, the acquiring the first data structure and the second data structure from the input parameter information and the output parameter information of the first interface respectively includes:

[0024] Circularly parsing a first array containing input parameter information of the first interface to parse the first array to obtain the last field in the first array, and using the obtained last field as the first data structure;

[0025] The second array containing the output parameter information of the first interface is cyclically parsed to obtain the last field in the second array from the second array, and the obtained last field is used as the second data structure.

[0026] Optionally, acquiring first field data of a preset type from the JSON data according to the first data structure of each interface includes:

[0027] According to the first data structure of each interface, a third array matching the first data structure is obtained from the JSON data;

[0028] Acquire the first field data of the preset type from the third array;

[0029] The obtaining the second field data of the preset type from the JSON data according to the second data structure of each interface includes:

[0030] According to the second data structure of each interface, a fourth array matching the second data structure is obtained from the JSON data;

[0031] The second field data of the preset type is obtained from the fourth array.

[0032] Optionally, the method further comprises:

[0033] Poll to obtain the JSON data contained in the swagger document corresponding to the development project;

[0034] Determine whether the newly acquired JSON data has changed. If so, return to the step of traversing the JSON data according to the interface request path of multiple interfaces, and obtain the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data, and generate a prompt message.

[0035] Another aspect of an embodiment of the present application provides a type declaration file generation device based on a swagger document, including:

[0036] A first acquisition module, used to acquire interface request paths of multiple interfaces;

[0037] The second acquisition module is used to obtain the JSON data contained in the swagger document corresponding to the development project;

[0038] A traversal module, used to traverse the JSON data according to the interface request paths of multiple interfaces, and obtain from the JSON data a first data structure included in the input parameter information of each interface and a second data structure included in the output parameter information of each interface;

[0039] A third acquisition module, used to acquire first field data of a preset type from the JSON data according to the first data structure of each interface, and to acquire second field data of the preset type from the JSON data according to the second data structure of each interface;

[0040] The generating module is used to generate a type declaration file of the development project according to a preset first type declaration file generating rule by using the first field data corresponding to each interface and the second field data corresponding to each interface.

[0041] Another aspect of an embodiment of the present application provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor is used to implement the steps of any of the methods described above when executing the computer program.

[0042] Another aspect of an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, and the computer program is used to implement the steps of any of the above methods when executed by a processor.

[0043] The method, apparatus, computer device and computer-readable storage medium for generating a type declaration file based on a swagger document provided in the embodiments of the present application find the first field data and the second field data that are ultimately required to generate a type declaration file from JSON data according to the interface request paths of multiple interfaces, without having to obtain the first field data and the second field data for all interfaces. This ensures that the ultimately generated type declaration file will not be too bloated and is very convenient to call. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] Figure 1 A schematic diagram of an environment for implementing a method for generating a type declaration file based on a swagger document according to an embodiment of the present application is shown schematically;

[0045] Figure 2 A flowchart of a method for generating a type declaration file based on a swagger document according to an embodiment of the present application is schematically shown;

[0046] Figure 3 A schematic diagram schematically shows a detailed schematic diagram of the steps of obtaining interface request paths of multiple interfaces in an embodiment of the present application;

[0047] Figure 4 A flowchart schematically shows a detailed step process of traversing the JSON data according to the interface request path of multiple interfaces and obtaining the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data according to an embodiment of the present application;

[0048] Figure 5 A flowchart schematically shows a detailed step flow of obtaining a first data structure and a second data structure from input parameter information and output parameter information of the first interface respectively according to an embodiment of the present application;

[0049] Figure 6 A flowchart of a method for generating a type declaration file based on a swagger document according to another embodiment of the present application is schematically shown;

[0050] Figure 7 A block diagram of a device for generating a type declaration file based on a swagger document according to an embodiment of the present application is schematically shown;

[0051] Figure 8 A schematic diagram of the hardware architecture of a computer device suitable for implementing a method for generating a type declaration file based on a swagger document according to an embodiment of the present application is schematically shown. DETAILED DESCRIPTION

[0052] The advantages of the present application are further described below in conjunction with the accompanying drawings and specific embodiments.

[0053] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.

[0054] The terms used in this disclosure are for the purpose of describing specific embodiments only and are not intended to limit the disclosure. The singular forms of "a", "said" and "the" used in this disclosure and the appended claims are also intended to include plural forms unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0055] It should be understood that although the terms first, second, third, etc. may be used in the present disclosure to describe various information, such information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of the present disclosure, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".

[0056] In the description of the present application, it should be understood that the numerical labels before the steps do not indicate the order in which the steps are executed, but are only used to facilitate the description of the present application and to distinguish each step, and therefore should not be understood as a limitation on the present application.

[0057] The type declaration file generation method based on the swagger document provided in this application can be applied to Figure 1 In the application environment shown, the terminal 102 communicates with the server 104 through a network. The terminal 102 may be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, and portable wearable devices, and the server 104 may be implemented as an independent server or a server cluster consisting of multiple servers.

[0058] The first system and swagger are running on the terminal 102. The second system is running on the server 104. When processing work tasks, the terminal 102 can initiate a business request to the second system through the first system. The second system responds to the business request and calls multiple interfaces to execute the business request according to the business process. After execution, the second system returns the response data of each interface to the first system. The swagger in the terminal 102 records the response data of each interface and generates a native swagger file. The terminal 102 generates a JSON file using the native swagger file. Afterwards, the terminal 102 can use the JSON file to generate a type declaration file and a front-end interface file.

[0059] See also Figure 2 , which is a flow chart of a method for generating a type declaration file based on a swagger document in one embodiment of the present application. It can be understood that the flow chart in the embodiment of the present method is not used to limit the order of executing the steps. As can be seen from the figure, the method for generating a type declaration file based on a swagger document provided in the present embodiment includes:

[0060] Step S20: Obtain interface request paths of multiple interfaces.

[0061] Specifically, the interface request path is the request address of the interface. As an example, the interface request path can be expressed in the following format: path: / mal1-c / api / ***, that is, the path field is used to represent the interface request path of a certain interface, and the content corresponding to the path field represents the specific interface request path of the interface. In other words, / mal1-c / api / *** represents the interface request path of the current interface.

[0062] In an exemplary implementation, the interface request paths of multiple interfaces may be stored in a preset file first, and when the interface request paths need to be obtained later, they may be directly obtained from the preset file.

[0063] In another exemplary embodiment, referring to Figure 3 , the interface request path for obtaining multiple interfaces includes:

[0064] Step S30, obtaining a configuration file of the development project.

[0065] Specifically, the development project is a project that the user is currently developing. For example, if the user is currently developing software project A, then the software project A is the development project.

[0066] The configuration file is a file for storing configuration information of the development project. In this embodiment, the configuration file may include the address of the swagger document corresponding to the development project, and the interface request path screening condition pre-configured by the user to determine which interfaces need to generate type declaration files.

[0067] In one implementation, the configuration file of the development project, such as autoapi.config.js, can be found from the directory of the development project through the path module of nodejs.

[0068] Among them, Nodejs is a background language based on javascript, which enables js to have the ability to operate io / fs / persistence and other background functions.

[0069] Step S31, reading the interface request path screening condition configured by the user from the configuration file.

[0070] Specifically, the interface request path filtering condition is a matching rule for determining which interfaces need to be processed. In this embodiment, the interface request path filtering condition can be a regular expression, such as the regular expression shown below: / mall-c / activity_626 / , which represents finding the interface request path containing the string from the string to be filtered.

[0071] In another implementation, the interface request screening condition may also be directly a character string corresponding to the interface request paths of multiple interfaces.

[0072] In this embodiment, the request path filtering condition can be read from the configuration file through the file reading module (fs module) of nodejs. In one embodiment, the request path filtering condition can be obtained by reading the includes field in the configuration file through the fs module.

[0073] Step S32: determining interface request paths of multiple interfaces according to the interface request path screening condition.

[0074] Specifically, after the interface request path screening condition is obtained, the interface request paths of multiple interfaces can be conveniently obtained according to the screening condition.

[0075] In one implementation, when the interface request path screening condition is directly a character string corresponding to the interface request paths of multiple interfaces, it can be determined that these character strings are the interface request paths of the multiple interfaces.

[0076] In another embodiment, when the interface request path screening condition is a regular expression, it can be determined that all character strings containing the regular expression are the interface request paths of the interface.

[0077] For example, the regular expression is: / mall-c / activity_626 / , then the character strings: / mall-c / activity_626 / 124 / , / mall-c / activity_626 / asd / , / mall-c / activity_626 / 124 / dddd / , etc. are all interface request paths of the interface.

[0078] Step S21, obtaining the JSON data contained in the swagger document corresponding to the development project.

[0079] Specifically, when developing a project, in order to facilitate the joint debugging of the front-end and back-end, the back-end personnel will write a Swagger document for the reference of the front-end developers during the development of the project. Among them, the Swagger document is a common agreement between the two parties.

[0080] In this embodiment, the JSON data contained in the swagger document can be obtained from the server through an ajax request, wherein the ajax request includes the address of the swagger document.

[0081] In this embodiment, the JSON data includes interface information of all interfaces, wherein the interface information may include interface request path information of the interface, input parameter information of the interface, and output parameter information of the interface, etc.

[0082] Step S22, traverse the JSON data according to the interface request paths of multiple interfaces, and obtain the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data.

[0083] Specifically, when the interface information of each interface is saved in the JSON data, it is saved starting with the interface request path. Therefore, when obtaining the interface information of each interface, the interface information of the interface can be located in the JSON data through the interface request path of the interface.

[0084] In this embodiment, the JSON data is traversed in a manner of the interface request path, and the interface information of each interface can be found in the JSON data in turn.

[0085] After finding the interface information of the first interface, you can continue to obtain the input parameter information and output parameter information of the current interface from the interface information, where the input parameter information can include multiple input parameter objects and attribute information of multiple input parameter objects, where the attribute information can include the interface field name, field type, field length, field description information, first data structure, etc. of the input parameter object. Similarly, the output parameter information can also include multiple output parameter objects and attribute information of multiple output parameter objects, where the attribute information can include the interface field name, field type, field length, field description information, second data structure, etc. of the output parameter object.

[0086] In this embodiment, the first data structure is used to determine the data structure of the input parameter that needs to generate the type declaration file, and the data structure includes multiple data fields.

[0087] The second data structure is used to determine the data structure of the parameter that needs to generate the type declaration file, and the data structure contains multiple data fields.

[0088] In an exemplary embodiment, referring to Figure 4 , traversing the JSON data according to the interface request path of multiple interfaces, and obtaining the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data includes: step S40, traversing the JSON data according to the interface request path to find the input parameter information and output parameter information of the first interface from the JSON data; step S41, respectively obtaining the first data structure and the second data structure from the input parameter information and the output parameter information of the first interface; step S42, continuing to traverse the JSON data according to the interface request path, and obtaining the input parameter information and the output parameter information of the next interface from the JSON data, and obtaining the first data structure and the second data structure from the input parameter information and the output parameter information of the next interface, until the first data structure and the second data structure of all interfaces are obtained.

[0089] Specifically, the input parameter information and output parameter information of the first interface are first found from the JSON data through the interface request path. Then, the first data structure and the second data structure can be found from the input parameter information and the output parameter information respectively. After finding the first data structure and the second data structure, they are stored in the memory.

[0090] After finding the first data structure and the second data structure of the first interface, continue to traverse the remaining JSON data according to the interface request path until the output parameter information and input parameter information of the next interface (the second interface) are found, and then the first data structure and the second data structure are obtained from the output parameter information and the input parameter information of the second interface. After that, repeat the above operation until the first data structure and the second data structure of all interfaces are found according to the interface request path.

[0091] In an exemplary embodiment, referring to Figure 5 , the obtaining of the first data structure and the second data structure from the input parameter information and the output parameter information of the first interface respectively includes: step S50, looping and parsing the first array containing the input parameter information of the first interface to parse the first array to obtain the last field in the first array, and using the obtained last field as the first data structure; step S51, looping and parsing the second array containing the output parameter information of the first interface to parse the second array to obtain the last field in the second array, and using the obtained last field as the second data structure.

[0092] As an example, the input parameter information of the first interface is as follows:

[0093]

[0094]

[0095] Among them, "parameters" represents the field of the input parameter information, and the content after the field represents the specific input parameter information. The content in "[]" represents the first array.

[0096] In this embodiment, the first array is parsed in a loop so that each layer of content in the first array can be obtained in turn. After obtaining each layer of content, the last field in the first array is used as the first data structure, that is, the field FirstLookOrderinfoQuery is used as the first data structure.

[0097] Similarly, when obtaining the second data structure, the second array of the output parameter information is parsed according to the above method to obtain the second data structure. As an example, "response" can be used to represent the field of the output parameter information, and the content after the field represents the specific output parameter information. The content in "[]" represents the second array.

[0098] Step S23, obtaining first field data of a preset type from the JSON data according to the first data structure of each interface, and obtaining second field data of the preset type from the JSON data according to the second data structure of each interface.

[0099] Specifically, after obtaining the first data structure of each interface, a third array matching the first data structure can be found from the JSON data according to the first structure of each interface, and then the first field data of the preset type can be obtained from the third array.

[0100] The preset type is a pre-set data field.

[0101] As an example, assume that the third array obtained according to the first data structure cartCouponltemsNumVo is as follows:

[0102]

[0103]

[0104] Among them, cartCouponltemsNumVo represents the first data structure, and the content after this field represents the data specifically contained in the first data structure, that is, the data contained in the third array. The preset type is the "properties" field in the third array, that is, the property field, and the data content corresponding to the property field is the first field data.

[0105] In one implementation, all field data contained in the attribute field may be used as the first field data, or part of the field data contained in the attribute field may be used as the first field data. The specific selection of which field data to use as the first field data is not limited in this embodiment and may be set according to actual conditions. For example, the field data "code: Integer, / / returned error code" and the field data "message: string, / / returned error description code" may be selected as the first field data.

[0106] Similarly, after obtaining the second data structure of each interface, a fourth array matching the second data structure can be found from the JSON data according to the second structure of each interface, and then the second field data of the preset type can be obtained from the fourth array.

[0107] The specific method for obtaining the second field data refers to the above method for obtaining the first field data, which will not be described in detail in this embodiment.

[0108] Step S24, generating a type declaration file of the development project according to a preset first type declaration file generation rule by combining the first field data corresponding to each interface and the second field data corresponding to each interface.

[0109] Specifically, the first type declaration file generation rule is a rule for generating a type declaration file by splicing the first field data and the second field data of each interface. The rule is pre-defined by the user and can be modified and adjusted according to actual conditions.

[0110] As an example, the first type declaration file generation rule may be in the following format: interface+interface data structure (first data structure or second data structure) {first field data or second field data}

[0111] In a specific implementation, the generated type declaration file is as follows:

[0112] Interface BuddyAssistRecordOuery{

[0113] assistMid:number,

[0114] beAssistecMid:nunber

[0115] }

[0116] interface LuckDramOuery{

[0117] jackpotId:number,

[0118] mid:nunber,

[0119] portrait: string,

[0120] uname: string,

[0121] }

[0122] Among them, BuddyAssistRecordOuery represents the first data structure of the interface, LuckDramOuery represents the second data structure of the interface, assistMid:number, beAssistecMid:nunber represent the first field data, jackpotId:number, mid:nunber, portrait:string, uname:string represent the second field data.

[0123] The type declaration file is a TS file, which is used to provide static type detection for JavaScript, making up for the disadvantages of weak languages, and is a superset of JavaScript.

[0124] The type declaration file generation method in this embodiment finds the first field data and the second field data that are ultimately needed to generate the type declaration file from the JSON data according to the interface request paths of multiple interfaces, without having to obtain the first field data and the second field data for all interfaces. This ensures that the type declaration file that is ultimately generated is not too bloated and is very easy to call.

[0125] In an exemplary embodiment, referring to Figure 6 , the method further comprises:

[0126] Step S60, traverse the JSON data according to the interface request paths of multiple interfaces, and obtain interface information of each interface from the JSON data, wherein the interface information includes interface description information, interface request path, interface input parameter information, and interface output parameter information.

[0127] Specifically, the description information is information used to describe the interface, which may include the name of the interface, the remarks information of the interface, etc.

[0128] As an example, some interface information of an interface is obtained as follows:

[0129] "paths": {

[0130] " / mall-c / api / ***": {

[0131] "get": {

[0132] "tags": [

[0133] "merchants-controller"

[0134] "summary": "***",

[0135] "operationtd":"***",

[0136] "consumes": [

[0137] “application / json”

[0138] "produces:

[0139] "* / *"

[0140] ],

[0141] parameters": [

[0142] "name": "***",

[0143] "in": "***",

[0144] "description":"***"

[0145] "required": false, "type": "***",

[0146] "format": "***"

[0147] },

[0148] {

[0149] "name": "***",

[0150] "in": "***",

[0151] "description": "***"

[0152] Among them, the tag and summary fields are description information of the interface, the parameters field is the input parameter information of the interface, and the response field (not shown) is the output parameter information of the interface.

[0153] The Path field is the interface request path field, and / mall-c / api / *** is the interface request path of the interface.

[0154] Step S61, generating a front-end interface file of the development project according to a preset second type declaration file generation rule based on the interface information of each interface.

[0155] Specifically, the second type declaration file generation rule is a rule for splicing the interface description information, interface request path, interface input parameter information, and interface output parameter information of each interface to generate a front-end interface file. The rule is pre-defined by the user and can be modified and adjusted according to actual conditions.

[0156] As an example, the second type declaration file generation rule may be the following splicing rule: interface description information +

[0157] Export const the last field in the interface request path + input parameter information or output parameter information + fetch{url: interface request path? + input parameter information or output parameter information}.

[0158] It should be noted that the input and output parameter field data specifically selected in the input parameter information or output parameter information in the above-mentioned splicing rules can be set and adjusted according to actual conditions, and is not limited in the present embodiment. Similarly, the field data specifically selected in the interface description information can also be set and adjusted according to actual conditions, and is not limited in the present embodiment.

[0159] In a specific implementation, some of the strings in the generated front-end interface file are as follows:

[0160] / / merchants-controller***

[0161] export const top=(shop_id:number,pageType:string)=>Fetch({url:' / mall-c / api / merchants / goods / top?shop_id+pageType:

[0162] Among them, " / / merchants-controller***" represents the interface description information, "top" represents the last field of the interface request path, "shop_id:number, pageType:string" represents the input parameter field data in the interface, and "mall-c / api / merchants / goods / top" represents the interface request path of the interface.

[0163] Step S62: insert the front-end interface file into the type declaration file.

[0164] Specifically, the front-end interface file can be inserted at the end of the type declaration file, or can be placed at the beginning of the type declaration file, which is not limited in this implementation.

[0165] Among them, the front-end interface file is a file used to facilitate the front-end to refer to the file to develop the front-end interface.

[0166] In this embodiment, the front-end interface file is generated while the type declaration file is generated, without the need for the user to manually write the file, thereby saving the developer's time in writing the file and improving development efficiency.

[0167] In an exemplary implementation, after the front-end interface file is inserted into the type declaration file, in order to facilitate user search, the file can be written to the api.ts file under the working directory through the fs and path modules of nodejs.

[0168] In an exemplary implementation, in order to avoid the problem that after the swagger document is changed, the front-end personnel need to wait for the back-end to actively notify them and then obtain the swagger document, resulting in low efficiency of joint debugging, in this embodiment, after generating the type declaration file, the JSON data contained in the swagger document corresponding to the development project can be polled, and after obtaining the JSON data, it can be compared with the JSON data obtained last time. If it is found that the JSON data has changed, the step of returning to execute the step of traversing the JSON data according to the interface request path of multiple interfaces, and obtaining the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data, so as to regenerate a new type declaration file.

[0169] In this embodiment, in order to allow the user to know in time that a new type declaration file is generated, a prompt message may be generated on the page to remind the user.

[0170] See also Figure 7 As shown, it is a program module diagram of an embodiment of a type declaration file generating device 70 based on a swagger document of the present application.

[0171] In this embodiment, the type declaration file generation device 70 based on the swagger document includes a series of computer program instructions stored in the memory. When the computer program instructions are executed by the processor, the type declaration file generation function based on the swagger document of each embodiment of the present application can be implemented. In some embodiments, based on the specific operations implemented by each part of the computer program instruction, the type declaration file generation device 70 based on the swagger document can be divided into one or more modules. The specific modules that can be divided are as follows:

[0172] A first acquisition module 71, used to acquire interface request paths of multiple interfaces;

[0173] The second acquisition module 72 is used to obtain the JSON data contained in the swagger document corresponding to the development project;

[0174] A traversal module 73, used to traverse the JSON data according to the interface request paths of the multiple interfaces, and obtain from the JSON data the first data structure included in the input parameter information of each interface and the second data structure included in the output parameter information of each interface;

[0175] A third acquisition module 74, configured to acquire first field data of a preset type from the JSON data according to the first data structure of each interface, and to acquire second field data of the preset type from the JSON data according to the second data structure of each interface;

[0176] The generating module 75 is used to generate a type declaration file of the development project by combining the first field data corresponding to each interface and the second field data corresponding to each interface according to a preset first type declaration file generating rule.

[0177] In an exemplary embodiment, the type declaration file generating device 70 based on the swagger document further includes: a fourth acquisition module and an insertion module, wherein:

[0178] A fourth acquisition module is used to traverse the JSON data according to the interface request path of multiple interfaces, and obtain interface information of each interface from the JSON data, wherein the interface information includes description information of the interface, interface request path, input parameter information of the interface, and output parameter information of the interface;

[0179] The generating module 75 is further used to generate the front-end interface file of the development project according to the preset second type declaration file generating rule based on the interface information of each interface;

[0180] The insertion module is used to insert the front-end interface file into the type declaration file.

[0181] In an exemplary embodiment, the first acquisition module 71 is further used to acquire a configuration file of the development project; read a user-configured interface request path screening condition from the configuration file; and determine interface request paths of multiple interfaces according to the interface request path screening condition.

[0182] In an exemplary embodiment, the traversal module 73 is also used to traverse the JSON data according to the interface request path, and find the input parameter information and output parameter information of the first interface from the JSON data; obtain the first data structure and the second data structure from the input parameter information and the output parameter information of the first interface respectively; continue to traverse the JSON data according to the interface request path, and obtain the input parameter information and output parameter information of the next interface from the JSON data, and obtain the first data structure and the second data structure from the input parameter information and the output parameter information of the next interface, until the first data structure and the second data structure of all interfaces are obtained.

[0183] In an exemplary embodiment, the traversal module 73 is also used to perform a loop parsing on a first array containing input parameter information of the first interface, so as to parse the first array to obtain the last field in the first array, and use the obtained last field as the first data structure; and perform a loop parsing on a second array containing output parameter information of the first interface, so as to parse the second array to obtain the last field in the second array, and use the obtained last field as the second data structure.

[0184] In an exemplary embodiment, the third acquisition module 74 is further used to acquire a third array matching the first data structure from the JSON data according to the first data structure of each interface; and acquire the first field data of the preset type from the third array.

[0185] The third acquisition module 74 is further used to acquire a fourth array matching the second data structure from the JSON data according to the second data structure of each interface; and acquire the second field data of the preset type from the fourth array.

[0186] In an exemplary embodiment, the type declaration file generating device 70 based on the swagger document further includes: a polling module and a judgment module, wherein:

[0187] The polling module is used to poll and obtain the JSON data contained in the swagger document corresponding to the development project;

[0188] The judgment module is used to judge whether the newly acquired JSON data has changed. If it has changed, it returns to execute the steps of traversing the JSON data according to the interface request path of multiple interfaces, and obtaining the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data, and generates a prompt message.

[0189] This embodiment finds the first field data and the second field data that are ultimately needed to generate a type declaration file from the JSON data according to the interface request paths of multiple interfaces, without having to obtain the first field data and the second field data for all interfaces. This ensures that the type declaration file that is ultimately generated is not too bloated and is very easy to call.

[0190] Figure 8The hardware architecture diagram of a computer device 8 suitable for implementing a method for generating a type declaration file based on a swagger document according to an embodiment of the present application is schematically shown. In this embodiment, the computer device 8 is a device that can automatically perform numerical calculations and / or information processing according to pre-set or stored instructions. For example, it can be a tablet computer, a laptop computer, a desktop computer, a rack server, a blade server, a tower server or a cabinet server (including an independent server, or a server cluster composed of multiple servers), etc. Figure 8 As shown, the computer device 8 includes at least but is not limited to: a memory 120, a processor 121, and a network interface 122 that can communicate with each other through a system bus. Among them:

[0191] The memory 120 includes at least one type of computer-readable storage medium, which may be volatile or non-volatile. Specifically, the readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (e.g., 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, magnetic disk, optical disk, etc. In some embodiments, the memory 120 may be an internal storage module of the computer device 8, such as a hard disk or memory of the computer device 8. In other embodiments, the memory 120 may also be an external storage device of the computer device 8, such as a plug-in hard disk equipped on the computer device 8, a smart memory card (Smart Media Card, referred to as SMC), a secure digital (Secure Digital, referred to as SD) card, a flash card, etc. Of course, the memory 120 may also include both the internal storage module of the computer device 8 and its external storage device. In this embodiment, the memory 120 is generally used to store the operating system and various application software installed on the computer device 8, such as the program code of the type declaration file generation method based on the swagger document, etc. In addition, the memory 120 can also be used to temporarily store various data that have been output or are to be output.

[0192] In some embodiments, the processor 121 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other chip for generating a type declaration file based on a swagger document. The processor 121 is generally used to control the overall operation of the computer device 8, such as performing control and processing related to data interaction or communication with the computer device 8. In this embodiment, the processor 121 is used to run the program code stored in the memory 120 or process data.

[0193] The network interface 122 may include a wireless network interface or a wired network interface, and the network interface 122 is generally used to establish a communication link between the computer device 8 and other computer devices. For example, the network interface 122 is used to connect the computer device 8 to an external terminal through a network, to establish a data transmission channel and a communication link between the computer device 8 and the external terminal, etc. The network may be a wireless or wired network such as an intranet, the Internet, the Global System of Mobile communication (GSM), Wideband Code Division Multiple Access (WCDMA), 4G network, 5G network, Bluetooth, Wi-Fi, etc.

[0194] It should be pointed out that Figure 8 Only a computer device having components 120 - 122 is shown, but it should be understood that implementation of all of the components shown is not a requirement, and more or fewer components may be implemented instead.

[0195] In this embodiment, the type declaration file generation method based on the swagger document stored in the memory 120 can be divided into one or more program modules and executed by one or more processors (processor 121 in this embodiment) to complete the present application.

[0196] An embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for generating a type declaration file based on a swagger document in the embodiment are implemented.

[0197] In this embodiment, the computer-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 computer-readable storage medium can be an internal storage unit of a computer device, such as a hard disk or memory of the computer device. In other embodiments, the computer-readable storage medium can also be an external storage device of a computer device, such as a plug-in hard disk equipped on the computer device, a smart memory card (Smart Media Card, referred to as SMC), a secure digital (Secure Digital, referred to as SD) card, a flash card (Flash Card), etc. Of course, the computer-readable storage medium can also include both the internal storage unit of the computer device and its external storage device. In this embodiment, the computer-readable storage medium is generally used to store the operating system and various application software installed on the computer device, such as the program code of the type declaration file generation method based on the swagger document in the embodiment. In addition, the computer-readable storage medium can also be used to temporarily store various types of data that have been output or are to be output.

[0198] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on at least two network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the embodiment scheme of the present application. Those of ordinary skill in the art may understand and implement it without creative labor.

[0199] Through the description of the above implementation methods, a person of ordinary skill in the art can clearly understand that each implementation method can be implemented by means of software plus a general hardware platform, and of course, by hardware. A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.

[0200] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for generating a type declaration file based on a swagger document, characterized in that the method comprises: Get the interface request path of multiple interfaces; Get the JSON data contained in the swagger document corresponding to the development project; Traversing the JSON data according to the interface request paths of the multiple interfaces, and obtaining from the JSON data a first data structure included in the input parameter information of each interface and a second data structure included in the output parameter information of each interface; Acquire first field data of a preset type from the JSON data according to the first data structure of each interface, and acquire second field data of the preset type from the JSON data according to the second data structure of each interface; Generate a type declaration file of the development project according to a preset first type declaration file generation rule by using first field data corresponding to each interface and second field data corresponding to each interface; Traversing the JSON data according to the interface request paths of the multiple interfaces, and obtaining interface information of each interface from the JSON data, the interface information including description information of the interface, interface request path, input parameter information of the interface, and output parameter information of the interface; Generate the front-end interface file of the development project according to the preset second type declaration file generation rule based on the interface information of each interface; Insert the front-end interface file into the type declaration file.

2. According to the method for generating a type declaration file based on a swagger document according to claim 1, it is characterized in that The interface request path for obtaining multiple interfaces includes: Obtaining a configuration file of the development project; Reading the interface request path screening condition configured by the user from the configuration file; The interface request paths of the multiple interfaces are determined according to the interface request path screening condition.

3. The method for generating a type declaration file based on a swagger document according to claim 1, characterized in that: The traversing of the JSON data according to the interface request paths of the multiple interfaces and obtaining the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data includes: Traverse the JSON data according to the interface request path, and find the input parameter information and output parameter information of the first interface from the JSON data; Respectively obtaining a first data structure and a second data structure from the input parameter information and the output parameter information of the first interface; Continue to traverse the JSON data according to the interface request path, and obtain the input parameter information and output parameter information of the next interface from the JSON data, and obtain the first data structure and the second data structure from the input parameter information and the output parameter information of the next interface, until the first data structure and the second data structure of all interfaces are obtained.

4. The method for generating a type declaration file based on a swagger document according to claim 3, characterized in that: The obtaining of the first data structure and the second data structure from the input parameter information and the output parameter information of the first interface respectively includes: Circularly parsing a first array containing input parameter information of the first interface to parse the first array to obtain the last field in the first array, and using the obtained last field as the first data structure; The second array containing the output parameter information of the first interface is cyclically parsed to obtain the last field in the second array from the second array, and the obtained last field is used as the second data structure.

5. The method for generating a type declaration file based on a swagger document according to claim 1, characterized in that: The acquiring first field data of a preset type from the JSON data according to the first data structure of each interface includes: According to the first data structure of each interface, a third array matching the first data structure is obtained from the JSON data; Acquire the first field data of the preset type from the third array; The obtaining the second field data of the preset type from the JSON data according to the second data structure of each interface includes: According to the second data structure of each interface, a fourth array matching the second data structure is obtained from the JSON data; The second field data of the preset type is obtained from the fourth array.

6. The method for generating a type declaration file based on a swagger document according to any one of claims 1 to 5, characterized in that: The method further comprises: Poll to obtain the JSON data contained in the swagger document corresponding to the development project; Determine whether the newly acquired JSON data has changed. If so, return to the step of traversing the JSON data according to the interface request path of multiple interfaces, and obtain the first data structure contained in the input parameter information of each interface and the second data structure contained in the output parameter information of each interface from the JSON data, and generate a prompt message.

7. A type declaration file generation device based on a swagger document, characterized in that: include: A first acquisition module, used to acquire interface request paths of multiple interfaces; The second acquisition module is used to obtain the JSON data contained in the swagger document corresponding to the development project; A traversal module, used to traverse the JSON data according to the interface request paths of multiple interfaces, and obtain from the JSON data a first data structure included in the input parameter information of each interface and a second data structure included in the output parameter information of each interface; A third acquisition module, used to acquire first field data of a preset type from the JSON data according to the first data structure of each interface, and to acquire second field data of the preset type from the JSON data according to the second data structure of each interface; A generating module, used for generating a type declaration file of the development project by combining the first field data corresponding to each interface and the second field data corresponding to each interface according to a preset first type declaration file generating rule; A fourth acquisition module is used to traverse the JSON data according to the interface request path of multiple interfaces, and obtain interface information of each interface from the JSON data, wherein the interface information includes description information of the interface, interface request path, input parameter information of the interface, and output parameter information of the interface; The generation module is also used to generate the front-end interface file of the development project according to the preset second type declaration file generation rule based on the interface information of each interface; The insertion module is used to insert the front-end interface file into the type declaration file.

8. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, it is used to implement the steps of the method according to any one of claims 1 to 6.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, it is used to implement the steps of the method according to any one of claims 1 to 6.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, it is used to implement the steps of the method according to any one of claims 1 to 6.