Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with one or more embodiments of the present specification. Rather, they are merely examples of apparatus and methods consistent with certain aspects of one or more embodiments of the specification, as detailed in the claims which follow.
It should be noted that: in other embodiments, the steps of the corresponding methods are not necessarily performed in the order shown and described herein. In some other embodiments, the method may include more or fewer steps than those described herein. Moreover, a single step described in this specification may be broken down into multiple steps for description in other embodiments; multiple steps described in this specification may be combined into a single step in other embodiments.
Before introducing the present solution, a few terms that the present solution may relate to are explained first:
prettier is a plug-in used to format javascript, and supports include ES2017, JSX, Flow, Typescript, CSS, LES, and SCSS.
The TypeScript is a superset of JavaScript and supports the ECMAScript 6 standard, the TypeScript is designed to develop a large application, the large application can be compiled into pure JavaScript, and the compiled JavaScript can run on any browser. And the legality of the corresponding parameters can be checked during compiling, so that the normalization of writing codes of developers in multi-user cooperation is effectively restrained.
The Interface is an Interface of Typescript, is used for restricting the type of a variable, has one-to-one correspondence between the name and the variable needing to be verified, does not check the sequence of the attribute, and can be reused as long as the corresponding attribute exists and the type is also right. And by matching with eslint, whether the variable type is legal or not can be checked, developers are restricted to write codes with correct types, and errors are prompted and repaired before the codes are compiled and run.
The reference type is a data type in JavaScript, such as Object, Array, Function, Date, etc., and is an Object stored in the heap memory by JavaScript. The reference type data stored in the stack memory is actually the reference address of the object in the heap memory. The object in the heap memory in storage can be quickly found through the reference address. The non-referenced type is accessed by value and can directly manipulate the actual value stored in the variable.
Example one
As shown in fig. 3, the present application is directed to a method for dynamically generating a specification file based on Swagger, including the following steps:
calling an api interface provided by swagger to obtain web data in a webjson format, and converting the web data into a data source in the json data format;
generating a corresponding interface definition based on the classification name of the api interface corresponding to the data source;
generating a Typescript file based on the interface definition;
and formatting the Typescript file into a specification file, wherein the specification file conforms to an esint specification and a prettier beautifying specification.
It is worth mentioning that the scheme provides a tool or a method for dynamically generating the specification file based on Swagger, so that the interface connection between java back end and web front end development is facilitated. Correspondingly, the tool for dynamically generating the specification file based on the Swagger comprises the following steps: the system comprises a data capturing module for capturing a data source, an interface dynamic template generating module for generating an interface definition, and a Typescript file generating module for generating a Typescript file; and a file formatting module for formatting the Typescript file, wherein the function corresponding to each module corresponds to the function.
In addition, before the data capture module calls the document data, the document access address of Swagger provided by a back-end developer is obtained, and the parameters of the document access address are imported to call the api interface provided by Swagger. Html, then the starting parameter that the data crawling module needs to import at runtime is hostname port.
It is worth to be noted that the method for dynamically generating the specification file based on Swagger according to the present scheme provides two modes for generating the Typescript file: a simplified version mode and a conventional version mode. The Typescript file generated in the simplified version mode is small in size, the interface response type field is uniformly output as any, the request type field only outputs simple types such as string, number and the like, the input simple variable can be set as true, if the input simple variable is true, the type of the interface parameter is not specifically analyzed, and the input simple variable is directly exported as a params variable, wherein the type is any type in the java script.
The data capture module in the scheme uses a node.js http module in the process of calling document data, obtains web data in an utf-8 format through an api interface provided by a request Swagger, and simultaneously converts the web data into a json data format data source which is convenient to process by javascript.
The http module is one of the core members in the node, and is the basis for the interaction between the client and the server, and the http module can generally construct a service in a simple manner and also can initiate a request.
Correspondingly, in the step of calling the api interface provided by Swagger to acquire web data in the webjson format, the http module of node.
In the step of calling the api interface of restfull provided by Swagger by using the http module of node.js, the http module calls the document access address of Swagger to obtain document data, and obtains groups pre-classified by Swagger based on the document data, and then obtains the detailed address of the corresponding group by obtaining the url address of json data of each group after grouping the document data, and then calls the address where the specific classification name is located, so that the api interface of the restfull of the same classification name in each group can be obtained.
The internal of the Swagger divides interfaces with different purposes into different groups in advance, and after the interfaces are obtained, detailed api-docs addresses of the corresponding groups can be obtained, wherein the detailed api-docs addresses comprise detailed addresses of detailed description information of the current group.
In the step of converting the web data into a data source of a json data format, the web data is converted into a json object which can be identified by javascript through serialization by using a json function of the javascript, that is, the data source is stored in a memory in the form of the json object.
The interface dynamic template generation module collects and generates dictionaries for all api interfaces of a Swagger overall situation by analyzing paths fields of a data source in a json data format, and determines the request type of the api interfaces by judging keys to which the api interfaces belong. And meanwhile, the api interface is divided into corresponding categories according to the classification name of the api interface, and if the api interface has no relevant fields of the classification name, the api interface is merged into a global common api interface.
Correspondingly, in the step of generating corresponding interface definitions based on the group names of the data sources, the api interfaces corresponding to the data sources and having the same classification name are classified into the same class, the data sources are analyzed to determine the request type of each api interface, access is obtained based on the request types, and the interface definitions are generated based on the access. The category name is stored in the tags field in the document data of sawgger.
In the step of classifying the api interfaces corresponding to the data sources and having the same classification name into the same class, if the api interfaces corresponding to the data sources do not have the classification name, the data sources are merged into the global common api interface. And classification information about the api interface is stored in the memory.
Generally, a common restfull type interface is more common with get and post requests: for the post request, if the request parameters include parameters transmitted through the address field query, the interface dynamic template generation module filters the request parameters first and adds the parameters to the entry of the post request. If the request is a post request, the parameters containing the body field are filtered and used as the participation to generate the participation template.
Correspondingly, in the step of determining the request type of each api interface to obtain the entry parameter and obtaining the entry parameter based on the request type, the request type of the api interface is a get request or a post request, the request type is judged to be the get request or the post request according to the type contained in the method field of the data source, if the request is the get request, the parameter containing the query field is filtered and is used as the entry parameter to be generated into an entry parameter template; if the request is a post request, the parameters containing the body field are filtered and used as the participation to generate the participation template. In some embodiments, after the request type is spliced to the interface definition name, the request type is effectively distinguished from the name and finally output as the interface definition.
After confirming the request type of each api interface and obtaining the access parameter, judging the access parameter, in the step of generating interface definitions based on the access parameter, judging whether the access parameter is multiple or not, if the access parameter is multiple, indicating that the request type is an array, circularly traversing the array, obtaining a corresponding data transmission object by comparing fields in definitions, defining a cache array, caching the obtained data transmission object into a memory, using the cached data in the memory during the second access, and marking the used data transmission object; and when the traversal is finished or only one entry parameter is used, converting the swagger type of the data source into a type which can be identified by typescript according to a predefined type conversion function, obtaining a corresponding data transmission object by comparing fields in definitions, and generating an interface definition based on the data transmission object.
Acquiring the data transmission object: similar to the interface field in the Typescript, the type of the data transmission object pair is defined as string \ integer \ int \ long \ Array \ file \ bootie \ corresponding to string \ number \ string \ Array \ blob \ bootie in the Typescript, if the field is a common type in the javascript, such as string, number, blob, and bootie, the field is directly converted into several types of non-reference types identified by the Typescript according to the predefined type conversion function. If the field is of an Array type or an Object type, the field is firstly defined as a generic type, interface definitions under the generic type are searched in the global cache data and are applied to the generic interface definitions, and then the step of judging whether the input parameters are multiple is traversed again until all the interface definitions of the generic type are found out.
If the data structure is a data structure with deep hierarchy and similar to a tree, the interface definition can be continuously found out in a loop, and the method is exemplary; if a certain object A containing the subclass a is expressed as A.a, in this case, the definition of A and a can be found only by traversing the loop to find A; for the infinite reference problem possibly existing in the Object type, the traversal times of the generic specific type in the same interface are judged, so that the generic specific type is prevented from being referenced for multiple times, the traversal times are judged through a counter in the scheme, and if the traversal times are larger than 1, the generic specific type is judged to be referenced for multiple times.
The interface dynamic template generating module is internally provided with a template character string to generate an interface character string corresponding to the request type. Specifically, the scheme analyzes the url field of the data source, identifies parameters similar to { id } incoming address bar, converts the parameters into $ { id }, and generates a format which can be analyzed by variables in a template character string which can be identified by javascript.
The generated interface type data transmission object finally generates an interface definition, the scheme promotes the interface definition which appears for many times to be a global interface, uniformly defines a Deepcommon character string for the global interface and places the Deepcommon character string in a temporary array for marking that the Deepcommon character string is the global interface. And storing the file until a Typescript file is generated later. The Interface definition is Interface in Typescript, is abstract to behavior and is embodied as concrete class. The scheme converts the data transmission object into the interface definition.
And splicing the generated interface definition output into a character string, wherein an interface name is added into the character string header information, and an interface annotation and a public packet are added at the same time. Usually, a request function is packaged in project development for requesting, where importing the request function may also be customized for a project, for example, importing an axios or vue-resource javascript http request library, and the best practice of this embodiment recommends using the uniformly packaged request function in the project, and can uniformly modify and uniformly add a request interceptor or response interceptor at a later stage.
When a back-end developer defines an interface name, the back-end developer may be relatively random, different interfaces are defined with the same interface name, the front-end developer needs to convert the interface name into different names when in use, the expression of the front end may deviate from the actual use of the API interface, the names are uniformly generated according to the interface title, if the same name is met, the operationId is added behind the name for distinguishing, and the problem that the interface name needs to be defined repeatedly is effectively solved. The following are exemplary: the name of the back end of the acquired user name is named userInfo, but the name of the api of the front end may be that getinfo deviates from the reality, so that GET _ userInfo is obtained by splicing, and the purpose of the interface and the request type are effectively expressed.
That is, in order to avoid the problem caused by the inconsistency of the interface names, the api interfaces of the present solution uniformly generate the interface names according to the interface titles, and if the names of at least two api interfaces are consistent, the operationId is added after the interface name of each api interface.
When the Swagger is used for generating the interface annotation, texts such as text/text can be input at the back end, but in javascript, a single text/text can be converted into an annotation character, if the text/character is lacked, the problem of text verification error can be caused, and the problem that the generated annotation text can be reported by mistake is solved by converting the text/text into null through the regular full quantity by using the interface dynamic template generation module. That is to say, the interface dynamic template generation module of the present solution converts the text of the text after the interface annotation to null.
The Typescript file generation module of the scheme generates the Typescript file with the suffix name of ts by using the fs module of the node and the api interface corresponding to the generated classified interface definitions. And creating different folders according to the classification name of the api interface through mkdirSync and existSync functions of the fs module, and not creating the folders if the folders exist. Generating folders according to category names allows developers to intuitively see which folder different types of interfaces are in.
That is, in the "generate Typescript file based on the interface definition" step, the fs module of the node is used to generate a Typescript file with a ts suffix name for the interface definition, and the Typescript file generated by the interface definition with the same category name is placed in the same folder. And the folders are named by category name.
Specifically, a character string is defined by the generated interface, and after traversing the array of the previously generated Deepcommon public interface cache, the array is injected into the forefront of the character string, and only the Chinese comment field of the interface is reserved for the execution flow marked as the simple generation Typcript file by using the simple variable (for the execution flow marked as true and executed as the simple generation Typcript file by using the simple variable). And then calling a writeFileSync function in the fs module of the node to generate a file of the corresponding ts suffix. The folders are created according to the major categories, and different files of the minor categories are written into the folders.
The file formatting module of the scheme modifies the code of the Typescript file into a code which is in line with the beauty according to the formatting rule by calling the preter module in npm, and outputs the Typescript file which can normally run in different editors without error report. The modules in the node are used, so that the same content can be output in different operating system platforms or different integrated development environments. Compared with the traditional node calling mode, when the system is not provided with the preter, a temporary warehouse is created to install the preter module, the dependence environment of the current project cannot be polluted, meanwhile, the node version and the command version can be designated, and the problem that different projects use commands of different versions is solved. The problem that the number of times of submitting the remote warehouse in a formatted mode is too large due to the fact that different developers continuously and repeatedly modify the Typescript file is solved.
That is, in the "format the Typescript file as a specification file" step, the Typescript file is formatted as a specification file using the preter module in call npm.
For a Typescript file, it is in the javascript language itself. prettier can be used with esint, which is a code detection tool that recognizes ECMAScript/JavaScript and gives reports according to rules, which is a static analysis, commonly used words look for problematic patterns or codes, and does not depend on a specific coding style. For most programming languages, there is code checking, and generally a compiler will have built-in checking tools. JavaScript is a dynamic, weakly typed language, which is relatively error-prone during development. Because there is no compiler, debugging is usually required continuously during execution in order to find JavaScript code errors. Eslin, a tool that allows programmers to find problems during the encoding process rather than during execution. By using the rule of eslint and a preter tool containing a specific coding style, after the operation, a Typescript file which is in accordance with the specification and is easy to read can be generated. The facility needs to call the preter formatted file. Typically npm or yarn will be used. Before use, these tools require the download of preter modules in advance into the node _ modules folder of the global directory or current project. And then invokes the preter instruction. Preferably, the npx module is used for calling a preter instruction, so that the node version and the command version can be specified, the problem that different commands use different versions of commands is solved, the dependency package in the node _ modules is automatically loaded, the $ PATH (program entry), namely the directory corresponding to the module, can be executed by the commands in the dependency package, the installation is completed, the executable dependency package is only temporarily installed during installation and is not installed to the global directory, the storage space occupied by the program is saved, and the conflict with the existing dependency package after long-term use is avoided.
The method and the tool for dynamically generating the specification file based on the Swagger automatically generate a Typescript file for a user after a node.js script is operated, define parameters including arrays, character strings, numbers, files, Boolean, objects and other types, directly identify the non-reference types by original types, simultaneously can synchronously and recursively search attribute names and attribute types of corresponding arrays and objects aiming at the arrays and the objects, and format the generated Typescript file to adapt to different editors or plug-ins.
In order to illustrate the difference between the Swagger-based method and tool for dynamically generating the specification file according to the present embodiment and the conventional manual modification of the Typescript file, the following example is described. As shown in fig. 1, fig. 1 is a process of modifying a Typescript file by a conventional person:
in the same project, a developer A uses an eslin formatted Typescript file, the developer A changes the formatted file to a git or svn remote code warehouse, and at the moment, formatted change records of a.ts, b.ts, c.ts, d.ts and the like are submitted to a remote end. The developer B uses the preter formatted Typescript file, and the formatted Typescript file modifies part of the style file g.css, at this time, similarly, the formatted change records of a.ts, b.ts, c.ts, d.ts, and the like are submitted to the remote end, and the modification of g.css is also submitted to the remote end. Such an approach causes developers to be unable to effectively identify g.cs changes and other unformatted changes, and thus a tool that does not worry about confusion of submitted records due to file formatting is urgently needed.
And fig. 2 is a process of the method for dynamically generating the specification file based on Swagger according to the present scheme: the method for dynamically generating the specification file based on the Swagger generates the specification file, and the change records of the remote code warehouse can be distinguished by using the classification names of the interfaces, so that the formatted submission and the actual code change can be quickly and effectively distinguished, and the bug backtracking efficiency of developers is greatly improved.
It is worth mentioning that the dynamic generation of the canonical file based on Swagger in the scheme has small intrusiveness, only the file access address of Swagger needs to be configured, and the dependency of use is small, and only fs, path and http modules in the node are used. And the scheme can operate the preter to carry out code formatting after being called, and the generated file conforms to the submission specification of the project and can be directly put in storage.
Example two
Based on the same concept, referring to fig. 4, the present application further provides an apparatus for dynamically generating a specification file based on Swagger, including:
the data capturing module 301 is configured to invoke an api interface provided by swagger to obtain webjson-formatted document data, and convert the document data into a json-formatted data source;
an interface dynamic template generating module 302, configured to generate a corresponding interface definition based on the classification name of the api interface corresponding to the data source;
a Typescript file generating module 303, configured to generate a Typescript file based on the interface definition;
a file formatting module 304, configured to format the Typescript file into a specification file, where the specification file conforms to an esint specification and a prettier beautification specification.
The technical contents mentioned in the second embodiment are the same as those of the first embodiment, and the redundant parts are not described.
EXAMPLE III
The present embodiment further provides an electronic apparatus, referring to fig. 5, including a memory 404 and a processor 402, where the memory 404 stores a computer program, and the processor 402 is configured to execute the computer program to perform the steps in any of the above embodiments of the method for dynamically generating a specification file based on Swagger.
Specifically, the processor 402 may include a Central Processing Unit (CPU), or A Specific Integrated Circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiments of the present application.
Memory 404 may include, among other things, mass storage 404 for data or instructions. By way of example, and not limitation, memory 404 may include a hard disk drive (hard disk drive, HDD for short), a floppy disk drive, a solid state drive (SSD for short), flash memory, an optical disk, a magneto-optical disk, tape, or a Universal Serial Bus (USB) drive or a combination of two or more of these. Memory 404 may include removable or non-removable (or fixed) media, where appropriate. The memory 404 may be internal or external to the data processing apparatus, where appropriate. In a particular embodiment, the memory 404 is a Non-Volatile (Non-Volatile) memory. In particular embodiments, memory 404 includes Read-only memory (ROM) and Random Access Memory (RAM). The ROM may be mask-programmed ROM, Programmable ROM (PROM), Erasable PROM (EPROM), Electrically Erasable PROM (EEPROM), electrically rewritable ROM (EAROM), or FLASH memory (FLASH), or a combination of two or more of these, where appropriate. The RAM may be a Static Random Access Memory (SRAM) or a Dynamic Random Access Memory (DRAM), where the DRAM may be a fast page mode dynamic random access memory 404 (FPMDRAM), an Extended Data Out Dynamic Random Access Memory (EDODRAM), a Synchronous Dynamic Random Access Memory (SDRAM), or the like.
The memory 404 may be used to store or cache various data files that need to be processed and/or used for communication, as well as computer program instructions for the possible Swagger-based dynamic specification file generation methods performed by the processor 402.
The processor 402 reads and executes the computer program instructions stored in the memory 404 to implement any one of the above-described embodiments of the method for dynamically generating a specification file based on Swagger.
Optionally, the electronic apparatus may further include a transmission device 406 and an input/output device 408, where the transmission device 406 is connected to the processor 402, and the input/output device 408 is connected to the processor 402.
The transmitting device 406 may be used to receive or transmit data via a network. Specific examples of the network described above may include wired or wireless networks provided by communication providers of the electronic devices. In one example, the transmission device includes a Network adapter (NIC) that can be connected to other Network devices through a base station to communicate with the internet. In one example, the transmitting device 406 may be a Radio Frequency (RF) module, which is used to communicate with the internet in a wireless manner.
The input and output devices 408 are used to input or output information. In the present embodiment, the input information may be unprocessed document data or the like, and the output information may be a specification file or the like.
Optionally, in this embodiment, the processor 402 may be configured to execute the following steps by a computer program:
calling an api interface provided by swagger to obtain web data in a webjson format, and converting the web data into a data source in the json data format;
generating a corresponding interface definition based on the classification name of the api interface corresponding to the data source;
generating a Typescript file based on the interface definition;
and formatting the Typescript file into a specification file, wherein the specification file conforms to an esint specification and a prettier beautifying specification.
It should be noted that, for specific examples in this embodiment, reference may be made to examples described in the foregoing embodiments and optional implementations, and details of this embodiment are not described herein again.
In general, the various embodiments may be implemented in hardware or special purpose circuits, software, logic or any combination thereof. Some aspects of the invention may be implemented in hardware, while other aspects may be implemented in firmware or software which may be executed by a controller, microprocessor or other computing device, although the invention is not limited thereto. While various aspects of the invention may be illustrated and described as block diagrams, flow charts, or using some other pictorial representation, it is well understood that these blocks, apparatus, systems, techniques or methods described herein may be implemented in, as non-limiting examples, hardware, software, firmware, special purpose circuits or logic, general purpose hardware or controller or other computing devices, or some combination thereof.
Embodiments of the invention may be implemented by computer software executable by a data processor of the mobile device, such as in a processor entity, or by hardware, or by a combination of software and hardware. Computer software or programs (also referred to as program products) including software routines, applets and/or macros can be stored in any device-readable data storage medium and they include program instructions for performing particular tasks. The computer program product may comprise one or more computer-executable components configured to perform embodiments when the program is run. The one or more computer-executable components may be at least one software code or a portion thereof. Further in this regard it should be noted that any block of the logic flow as in the figures may represent a program step, or an interconnected logic circuit, block and function, or a combination of a program step and a logic circuit, block and function. The software may be stored on physical media such as memory chips or memory blocks implemented within the processor, magnetic media such as hard or floppy disks, and optical media such as, for example, DVDs and data variants thereof, CDs. The physical medium is a non-transitory medium.
It should be understood by those skilled in the art that various technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, however, as long as there is no contradiction between the combinations of the technical features, the scope of the present description should be considered as being described in the present specification.
The above examples are merely illustrative of several embodiments of the present application, and the description is more specific and detailed, but not to be construed as limiting the scope of the present application. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present application shall be subject to the appended claims.