CN114816364A - Method, device and application for dynamically generating template file based on Swagger - Google Patents

Method, device and application for dynamically generating template file based on Swagger Download PDF

Info

Publication number
CN114816364A
CN114816364A CN202210504287.6A CN202210504287A CN114816364A CN 114816364 A CN114816364 A CN 114816364A CN 202210504287 A CN202210504287 A CN 202210504287A CN 114816364 A CN114816364 A CN 114816364A
Authority
CN
China
Prior art keywords
interface
file
swagger
typescript
generating
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210504287.6A
Other languages
Chinese (zh)
Inventor
朱淑敏
沈文彦
文林
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
CCI China Co Ltd
Original Assignee
CCI China Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by CCI China Co Ltd filed Critical CCI China Co Ltd
Priority to CN202210504287.6A priority Critical patent/CN114816364A/en
Publication of CN114816364A publication Critical patent/CN114816364A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/31Programming languages or programming paradigms
    • G06F8/315Object-oriented languages
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/90Details of database functions independent of the retrieved data types
    • G06F16/95Retrieval from the web
    • G06F16/958Organisation or management of web site content, e.g. publishing, maintaining pages or automatic linking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/33Intelligent editors
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/30Creation or generation of source code
    • G06F8/38Creation or generation of source code for implementing user interfaces
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/43Checking; Contextual analysis

Abstract

The application provides a method, a device and an application for dynamically generating a template file based on Swagger, which are used for calling an api interface provided by Swagger to acquire webjson-format document data and converting the document data into a json-data-format data source; 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, so that a front-end developer can immediately generate the Typescript file through the tool after a back-end developer develops an api interface in a restfull format, and the front-end developer can timely adjust the incorrect type in the written code or the project operation error caused by parameter loss after the file is generated.

Description

Method, device and application for dynamically generating template file based on Swagger
Technical Field
The application relates to the field of big data mining, in particular to a method, a device and application for dynamically generating a template file based on Swagger.
Background
With the separation of the front end and the back end of the web development, each work type is more detailed, and the process of the web development is as follows: the method comprises the steps that the backend develops the api which accords with the restfull type, provides a corresponding docking document, defines the type of the input parameter of the api interface, the Chinese definition, whether the input parameter needs to be filled, and returns the format, the content and the Chinese definition of data, and then provides the data to a front-end developer, and the front-end developer reads the document, writes the document into a Typescript file which is suitable for the front-end development, defines the input parameter and the output parameter of a corresponding function which accords with the Typescript specification, and can effectively restrain the front-end developer to transmit parameters and analyze parameters according to the parameter type provided by the document. Backend personnel often use Swagger to generate docking documents, Swagger can provide web versions of documents, and can also download corresponding doc files for viewing. Front-end development can efficiently write Typescript files using this document.
However, the requirement of software development is usually changing continuously, in order to cope with such many changes, the backend development needs to continuously modify and change the restfull interface, the backend development can rapidly release new documents by using the document system of Swagger, and the front-end developer needs to pay attention to the adjustment of the restfull interface by the backend in real time. In the process of project development, once a project development period is prolonged or a software developer changes, no one can be familiar with the docking modes or changes of all the API interfaces, so that a back-end developer needs to read a source code again to distinguish which part of the API interface is changed or reconstructed, the error rate in the process of API docking is increased, the back-end developer is most likely to select reconstruction, the front-end developer needs to repeatedly check the difference between a Typescript file and a Swagger file, otherwise, situations such as field loss, field type errors or complete interface replacement occur, and the project development progress is influenced.
For front-end development, because different developers use different editors or different plug-ins, corresponding formatting may be performed during code saving, so that in the process of multi-user development, a developer a may use a format plug-in of the elint specification to perform code formatting, and a developer B may use a preter. If developers A and B modify the Typescript file and upload the Typescript file to a code management warehouse, a plurality of records only modifying the format are generated due to format differences, but in essence, no meaningful modification is achieved, so that the developers cannot quickly locate problem files during bug backtracking on the one hand, and ignore key codes in the project during code review on the other hand. If the problem needs to be solved, a uniform code formatting tool needs to be used for formatting the code, so that different editors or plug-ins can output the same type script file, and syntax error check of the editors cannot be triggered. Meanwhile, certain code attractiveness can be guaranteed, and developers can check the code conveniently.
Therefore, a tool or a method capable of parsing a generated web document into a corresponding Typescript file is urgently needed, and meanwhile, the output formats of the files can be specified in a unified formatting mode, so that confusion and errors when codes are uploaded to a code warehouse are prevented.
Disclosure of Invention
The embodiment of the application provides a method, a device and an application for dynamically generating a specification file based on Swagger, effectively solves the problems in the background art, and facilitates the interface butt joint of java back end and web front end development.
In a first aspect, an embodiment of the present application provides a method for dynamically generating a specification file based on Swagger, including the following steps:
calling an api interface provided by swagger to obtain webjson-format document data, and converting the document data into a json-data-format data source;
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.
In a second aspect, an embodiment of the present application provides an apparatus for dynamically generating a specification file based on Swagger, including:
the data capturing module is used for calling an api interface provided by swagger to acquire webjson-format document data and converting the document data into a json-data-format data source;
the interface dynamic template generating module is used for generating a corresponding interface definition based on the classification name of the api interface corresponding to the data source;
a Typescript file generating module, configured to generate a Typescript file based on the interface definition;
and the file formatting module is used for formatting the Typescript file into a specification file, wherein the specification file conforms to the esint specification and the prettier beautifying specification.
In a third aspect, an embodiment of the present application provides an electronic apparatus, which includes a memory and a processor, where the memory stores a computer program, and the processor is configured to execute the computer program to perform the method for dynamically generating a specification file based on Swagger.
In a fourth aspect, the present application provides a computer program product, which includes a software code portion, and when the computer program product is executed on a computer, the software code portion is configured to execute the method for dynamically generating a specification file based on Swagger.
In a fifth aspect, the present application provides a readable storage medium, where a computer program is stored in the readable storage medium, where the computer program includes program code for controlling a process to execute the process, where the process includes the method for dynamically generating a specification file based on Swagger.
The main contributions and innovation points of the invention are as follows:
according to the method for dynamically generating the specification file based on the Swagger, the effect that after a back-end developer develops an api interface in a restfull format is achieved, the front-end developer can immediately generate a Typescript file through the tool, and after the file is generated, the front-end developer can timely adjust the type of the written code to be incorrect or the project operation error caused by parameter loss. And generally, the operation can be realized only by defining the document address of the Swagger, so that the time consumed by front-end developers to write the Typescript file is reduced, the back-end developers do not need to inform the front-end developers which api interfaces are modified, the front-end developers do not need to confirm the modification of the Typescript file written by the front-end developers and a new Swagger document one by one, and the useless time consumed by the front-end developers and the back-end developers when the interface parameters are in butt joint is greatly saved.
Because the tool can dynamically generate the interface, when software development is carried out by front-end personnel, intelligent prompts are given in an editor to prompt fields of the array, which fields are required items and which are not required items, and meanwhile, if the fields which do not accord with the defined parameters exist, the project can not normally run, and the field type error which actually occurs at the position of the code is output in a control console for project running, so that the development personnel is restrained to carry out high-quality code development. This function is accomplished through the native semantic and lexical analysis capabilities of the Vscode editor.
In addition, the generated API interfaces are classified according to the classification names and are respectively placed in folders named by the classification names, and when codes are submitted, the submission caused by interface change can be obviously distinguished, and the submission caused by file formatting can be obviously distinguished. For developers at the front end and the back end, named interface names are often complicated, moreover, the api interfaces with the same type and similar functions need to be named, the corresponding interfaces cannot be effectively distinguished, only one set of names are defined by the developers at the back end by using the tool, interface names at the front end can be automatically generated, and the same names are automatically isolated. The mental burden of the developer for naming is reduced.
The details of one or more embodiments of the application are set forth in the accompanying drawings and the description below to provide a more thorough understanding of the application.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this application, illustrate embodiment(s) of the application and together with the description serve to explain the application and not to limit the application. In the drawings:
FIG. 1 is a schematic illustration of a conventional process for modifying a Typescript file;
FIG. 2 is a schematic illustration of a modification of a Typescript file according to the present scheme;
FIG. 3 is a flowchart of a method for dynamically generating a specification file based on Swagger according to an embodiment of the present application;
fig. 4 is a block diagram of a method and an apparatus for dynamically generating a specification file based on Swagger according to an embodiment of the present application;
fig. 5 is a schematic diagram of a hardware structure of an electronic device according to an embodiment of the present application.
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.

Claims (16)

1. A method for dynamically generating a specification file based on Swagger is characterized by comprising 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.
2. The method according to claim 1, wherein in the step of "generating corresponding interface definitions based on the group name of the data source", the api interfaces with the same class name corresponding to the data source are classified into the same class, the data source is analyzed to determine a request type of each api interface, and a reference is obtained based on the request type, and the interface definitions are generated based on the reference.
3. The Swagger-based method for dynamically generating a specification file according to claim 2, wherein in the step of "classifying the api interfaces corresponding to the data sources and having the same class name into the same class", if the api interfaces corresponding to the data sources do not have class names, the data sources are merged into a global common api interface.
4. The Swagger-based method for dynamically generating specification files according to claim 2, wherein in the step of determining that the request type of each api interface obtains an 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 determined to be the get or post request according to the type contained in the method field of the data source, and if the request is the get request, a parameter containing a query field is filtered and generated as 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.
5. The Swagger-based method for dynamically generating specification files according to claim 2, wherein in the step of generating interface definitions based on entry parameters, if a plurality of entry parameters exist, the request types are cyclically traversed, corresponding data transmission objects are obtained by comparing fields in definitions, and a cache array is defined to cache the obtained data transmission objects in a memory.
6. The method for dynamically generating a specification file based on Swagger according to claim 2, wherein in the step of generating an interface definition based on the input parameters, it is determined whether there are a plurality of input parameters, if there are a plurality of input parameters, the request type is represented as an array, at this time, the array is traversed in a loop, a corresponding data transmission object is obtained by comparing fields in definitions, a cache array is defined, and the obtained data transmission object is cached in a memory; 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.
7. The method for dynamically generating the specification file based on the Swagger according to claim 1, which comprises the following steps: and promoting the interface definition which appears for many times to be a global interface, uniformly defining a Deepcommon character string for the global interface and placing the Deepcommon character string in a temporary array.
8. The Swagger-based dynamic specification file generation method according to claim 1, wherein the generated interface definition outputs are spliced into a character string, wherein an interface name is added to the character string header information, and an interface comment and a public packet are added.
9. The Swagger-based dynamic specification file generation method according to claim 8, wherein text of/of the interface annotation is converted to null by a regular full amount.
10. The Swagger-based dynamic specification file generation method according to claim 8, wherein interface names are uniformly generated according to the interface title, and if the names of at least two api interfaces are consistent, the operationId is added after the interface name of each api interface.
11. The Swagger-based method for dynamically generating specification files according to claim 1, wherein in the "generating Typescript files based on the interface definitions" step, a fs module of a node is used to generate Typescript files with a suffix name of ts for the interface definitions, and the Typescript files generated by the interface definitions with the same classification name are placed in the same folder.
12. The Swagger-based dynamic specification file generating method according to claim 1, wherein in the "formatting the Typescript file into a specification file" step, a preter module in call npm is used to format the Typescript file into a specification file.
13. An apparatus for dynamically generating a specification file based on Swagger, comprising:
the data capturing module is used for calling an api interface provided by swagger to acquire web data in a webjson format and converting the web data into a data source in a json data format;
the interface dynamic template generating module is used for generating a corresponding interface definition based on the classification name of the api interface corresponding to the data source;
a Typescript file generating module, configured to generate a Typescript file based on the interface definition; and the file formatting module is used for formatting the Typescript file into a specification file, wherein the specification file conforms to the esint specification and the prettier beautifying specification.
14. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the computer program to perform the method for dynamically generating a specification file based on Swagger according to any one of claims 1 to 12.
15. A computer program product, characterized in that it comprises software code portions for performing the method for Swagger-based dynamic generation of specification files according to any of claims 1-12, when said computer program product is run on a computer.
16. A readable storage medium, wherein a computer program is stored in the readable storage medium, the computer program comprising program code for controlling a process to execute the process, the process comprising the Swagger-based dynamic specification file generation method according to any one of claims 1-12.
CN202210504287.6A 2022-05-10 2022-05-10 Method, device and application for dynamically generating template file based on Swagger Pending CN114816364A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202210504287.6A CN114816364A (en) 2022-05-10 2022-05-10 Method, device and application for dynamically generating template file based on Swagger

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210504287.6A CN114816364A (en) 2022-05-10 2022-05-10 Method, device and application for dynamically generating template file based on Swagger

Publications (1)

Publication Number Publication Date
CN114816364A true CN114816364A (en) 2022-07-29

Family

ID=82513223

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210504287.6A Pending CN114816364A (en) 2022-05-10 2022-05-10 Method, device and application for dynamically generating template file based on Swagger

Country Status (1)

Country Link
CN (1) CN114816364A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116107628A (en) * 2022-12-21 2023-05-12 山东未来网络研究院(紫金山实验室工业互联网创新应用基地) Document sorting method based on node. Js

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116107628A (en) * 2022-12-21 2023-05-12 山东未来网络研究院(紫金山实验室工业互联网创新应用基地) Document sorting method based on node. Js
CN116107628B (en) * 2022-12-21 2023-09-08 山东未来网络研究院(紫金山实验室工业互联网创新应用基地) Document sorting method based on node. Js

Similar Documents

Publication Publication Date Title
US10698682B1 (en) Computerized software development environment with a software database containing atomic expressions
US8091071B2 (en) Method and system for template-based code generation
US9218269B2 (en) Testing multiple target platforms
US9256638B2 (en) Instance management of code in a database
US8997070B2 (en) Extension mechanism for scripting language compiler
US20070169008A1 (en) External programmatic interface for IOS CLI compliant routers
CN110688307B (en) JavaScript code detection method, device, equipment and storage medium
CN107783766B (en) Method and device for cleaning files of application program
US20100042585A1 (en) Command Interpretation
WO2020206837A1 (en) Code segment positioning method and device, computer apparatus, and storage medium
US20110239200A1 (en) Method for compiling a computer program
US8171045B2 (en) Record based code structure
CN115599386A (en) Code generation method, device, equipment and storage medium
WO2021022703A1 (en) Software project reconstruction method and device, and computer device and storage medium
CN114816364A (en) Method, device and application for dynamically generating template file based on Swagger
CN114138748A (en) Database mapping file generation method, device, equipment and storage medium
US11550556B1 (en) Efficient semantic analysis of program code
EP2535813B1 (en) Method and device for generating an alert during an analysis of performance of a computer application
US9747085B2 (en) Source code generation from prototype source
CN107577476A (en) A kind of Android system source code difference analysis method, server and medium based on Module Division
CN112860265A (en) Method and device for detecting operation abnormity of source code database
CN113760291A (en) Log output method and device
US11256602B2 (en) Source code file retrieval
CN117667089A (en) Front-end form generation method and device, storage medium and electronic equipment
CN117348880A (en) Compression processing method and device for applet, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination