Method, system and equipment for automatically generating message data according to interface definition
Through the combination of built-in functions and data templates, the packet data is automatically generated according to the API interface definition, which solves the efficiency and accuracy of packet data generation in the existing technology, and realizes flexible, stable and efficient packet data generation.
Patent Information
- Application Number
- CN202411865469.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-17
- Publication Date
- 2025-04-18
AI Technical Summary
It is difficult for the prior art to automatically generate packet data that meets service needs based on API interface definitions, and the packet data needs to be manually updated when the interface definition changes, which makes it time-consuming, error-prone and affects system stability.
Create data generation rules through built-in functions, analyze interface definition description to generate data templates, and fill the templates according to the data type and generation rules to generate message data. Supports custom built-in functions and interface definitions that are separated from self-generated programs, improving flexibility and universality.
It realizes automatic generation of packet data based on interface definition, reduces manual intervention and errors, improves development efficiency and system stability, and adapts to different business scenarios and data exchange standards.
Smart Images

Figure CN119847489A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the technical field of message data generation, and in particular relates to a method, system and device for automatically generating message data according to interface definition. Background Art
[0002] With the rapid development of information technology, the number of API interfaces and business complexity are constantly increasing, the logic complexity of interfaces and the number of fields are increasing dramatically, and the field level nesting is deepening. This has led to the inability of manually writing message data to meet the needs of efficient and accurate interface data, especially in scenarios such as writing interface documents, providing interface message samples, and implementing interface data simulation, which has brought unprecedented challenges to developers.
[0003] On the one hand, each API interface has its own unique definition and rules, including parameter types, hierarchical structures, data types, etc. During the development and testing process, developers need to manually write these message data, which is not only time-consuming and labor-intensive, but also prone to errors. As the number of interfaces continues to increase, manually processing the data of these interfaces becomes extremely cumbersome and inefficient, seriously restricting the development progress and quality of the project. On the other hand, as business scenarios continue to change, the definition of API interfaces will also be adjusted frequently. When the interface definition changes, developers need to manually update all related message data, which is time-consuming and prone to omissions, seriously affecting the stability and reliability of the system. In addition, with the increase in system interactions, the data exchange standards between different systems are also different, which makes the generation and verification of sample message data more complicated.
[0004] JSON (JavaScript Object Notation) is a lightweight data exchange format that is easy for humans to read and write, and is also easy for machines to parse and generate. It is a very common data transmission and storage format, especially in Web development, mobile applications, and network services. The standardization of message data provides a theoretical possibility for implementing the device of this application.
[0005] Existing technical frameworks, such as JSON schema fake, can help generate random data, but the data generated is relatively general and cannot meet the professional data in specific businesses, which reduces the accuracy of the generated message data to a certain extent.
[0006] The Chinese patent "A method and system for generating simulated data based on TypeScript type" with publication number CN117149656A discloses a method for automatically generating messages, which automatically generates a mock message by reading the interface declaration node to construct the interface message format, and using the custom method to generate the message value using the callback function. In practical applications, this solution has the following shortcomings: 1. The custom function needs to be rewritten, and the application is cumbersome and complicated; 2. The interface definition cannot be separated from the self-generated program, and the interface declaration node must be defined in the program using TypeScript. The program is rigid, and the defined message format lacks universality.
[0007] Therefore, how to develop a method that can automatically generate message data according to interface definitions, automatically generate message data that meets business needs by understanding the definitions and rules of API interfaces, and at the same time be able to adapt to changes in interface definitions and data exchange standards between different systems to improve development efficiency, reduce errors, and ensure system stability and reliability has become a technical problem that needs to be solved urgently. Summary of the invention
[0008] In response to the above problems, the present application provides a method, system and device for automatically generating message data according to interface definition.
[0009] According to a first aspect of the present application, a method for automatically generating message data according to an interface definition is provided, the method comprising:
[0010] Create data generation rules through built-in functions;
[0011] Define and describe the message by creating message fields;
[0012] Generate a data template by parsing the definition description of the message;
[0013] Fill in the data template according to the data type and data generation rules to generate message data.
[0014] Optionally, in the method of automatically generating message data according to interface definition of the present application, data generation rules are created through built-in functions, including: writing a built-in function class that uniformly inherits the business data processing interface, setting the data generation method in the written built-in function class, and configuring a unique method name for the set data generation method.
[0015] Optionally, in the method of automatically generating message data according to interface definition of the present application, creating data generation rules through built-in functions also includes: creating data generation rules through encoding using built-in functions according to business data types.
[0016] Optionally, in the method of automatically generating message data according to the interface definition of the present application, the message is defined and described by creating fields of the message, including:
[0017] Create a field by creating field information of a message, where the field information includes a field name, a field type attribute, a field default value attribute, and a field data type attribute;
[0018] Determine whether the created field is a complex type field according to the field information. If the created field is not a complex type field, continue to create the next field by creating the next field information of the message.
[0019] When the created field is a complex type field, determine whether the field type of the complex type field is an object based on the field information. If the field type of the complex type field is an object, create a field sub-attribute for the complex type field, create a sub-field description for the sub-field in the created field sub-attribute, and create a sub-field based on the created sub-field description.
[0020] If the field type of the complex type field is an array, create an element description attribute for the complex type field, create a field description for the array field in the created element description attribute, and create an array field according to the created field description;
[0021] If the field type of the complex type field is neither an object nor an array, continue to create the next field by creating the next field information of the message.
[0022] Optionally, in the method of automatically generating message data according to interface definition of the present application, the message is defined and described by creating fields of the message, and also includes: the definition description of the data structure of the message is saved in json format.
[0023] Optionally, in the method of automatically generating message data according to interface definition of the present application, a data template is generated by parsing the definition description of the message, including: creating a data filling object, parsing the definition description of the message, adding the parsing result to the created data filling object, and generating a data template.
[0024] Optionally, in the method for automatically generating message data according to the interface definition of the present application, filling in the data template according to the data type and the data generation rule to generate the message data includes:
[0025] Traverse the definition description of the message to obtain the description information of the field, which includes the field name, field type attribute, field sub-attribute, element description attribute, field default value attribute and field data type attribute of the field;
[0026] Create a field in the data template according to the description information of the single field obtained, and determine whether the data type of the created field is a basic type. If the data type of the created field is a basic type, obtain the description information of the field, and determine whether the field data type attribute has a value according to the description information of the field obtained;
[0027] If the field data type attribute has a value, use the corresponding function to generate data according to the value of the field data type attribute through the Java reflection mechanism, and use the generated data to assign values to the fields in the data template;
[0028] If the field data type attribute has no value, determine whether the field default value attribute has a value. If the field default value attribute has a value, use the value of the field default value attribute to assign a value to the field in the data template.
[0029] If the field data type attribute has no value, generate random data that conforms to the field type attribute, and use the generated random data to assign values to the fields in the data template.
[0030] Optionally, in the method of automatically generating message data according to the interface definition of the present application, filling in the data template according to the data type and the data generation rule to generate the message data further includes:
[0031] If the data type of the created field is not a basic type, determine whether the field type of the field is an object;
[0032] If the field type of the complex type field is an object, obtain the field sub-attribute of the field, traverse the values of the obtained field sub-attribute, create a sub-field of the current field, and reflect and fill the corresponding data value into the data template according to the field data type of the sub-field;
[0033] If the field type of the complex type field is an array, obtain the element description attribute of the field, create a field description based on the array field in the element description attribute, create an array field based on the created field description, and reflect and fill the corresponding data value into the data template based on the field data type of the array field.
[0034] According to a second aspect of the present application, a system for automatically generating message data according to an interface definition is provided, the system comprising a message data generation server, the message data generation server comprising:
[0035] Built-in function module, used to create data generation rules through built-in functions;
[0036] The message definition module is used to define and describe the message by creating message fields;
[0037] The data generation module is used to generate a data template by parsing the definition description of the message; fill the data template according to the data type and data generation rules to generate message data.
[0038] According to a third aspect of the present application, a computer device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the method of the first aspect of the present application when executing the program.
[0039] The method for automatically generating message data according to interface definition in the embodiment of the present application has the following beneficial technical effects:
[0040] 1. Use the dataType field in the message definition to define the business type of the value. You can flexibly adjust the data generation rules according to the changes in business scenarios to meet different data sample generation requirements;
[0041] 2. In addition to the default built-in functions, it also supports custom built-in functions, which improves the scalability of the device; the data generated by more built-in functions have business meanings, which are more in line with the actual business of civil aviation. At the same time, it solves the problem of rewriting custom functions in the existing technology, making it easier to use;
[0042] 3. Interface definition can be separated from the self-generated program, as long as the description of the interface definition conforms to the standard definition specification, which reduces the need to define the interface declaration node in the program using TypeScript, improves the flexibility of the program and the universality of the defined message format;
[0043] 4. The corresponding message data can be quickly generated according to different interface definitions without manual intervention and repetitive work.
[0044] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or be understood by practicing the present application. The purpose and other advantages of the present application can be realized and obtained by the structures indicated in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0046] Figure 1This is an example diagram of the architecture of a system for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0047] Figure 2 This is an example diagram of the architecture of a message data generation server of a system for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0048] Figure 3 A flowchart of the steps of a method for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0049] Figure 4 An example diagram of field creation according to a method for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0050] Figure 5 An example diagram of creating complex type field attributes according to a method for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0051] Figure 6 An example diagram of a process for creating a single field template and generating data according to a method for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0052] Figure 7 An example diagram of a process for creating complex type fields and generating data according to a method for automatically generating message data according to an interface definition in accordance with an embodiment of the present application;
[0053] Figure 8 An example diagram of a process for creating basic type fields and generating data according to a method for automatically generating message data according to an interface definition according to an embodiment of the present application;
[0054] Fig. 9 A schematic diagram of the structure of the device provided in this application. DETAILED DESCRIPTION
[0055] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0056] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.
[0057] The technical principles of this application are as follows:
[0058] The method and system for automatically generating message data according to interface definition of this embodiment, through automated means, automatically generates message data that conforms to interface definition according to standard format interface definition, and can help developers configure the rules for generating data by customizing built-in functions, so as to generate data that is more suitable for the business. By allowing developers to customize the rules for generating data, and construct the definition of interface message, parse the definition file (json format) of API interface, extract the key information of the interface, and then generate the corresponding data template based on this information; by calling the preset data generation rules, fill the data template with specific data, and generate data that conforms to the definition.
[0059] Figure 1 FIG. 1 is an example diagram of the architecture of a system for automatically generating message data according to an interface definition according to an embodiment of the present application, such as Figure 1 As shown, the system may include a message data generation server 101, a communication network 102 and / or one or more message data generation clients 103. Figure 1 The example in the figure is a client 103 generating multiple message data.
[0060] The message data generation server 101 can be any suitable server for storing information, data, programs and / or any other suitable type of content. In some embodiments, the message data generation server 101 can perform appropriate functions. For example, in some embodiments, the message data generation server 101 can be used to customize the sorting of freight search results. As an optional example, in some embodiments, the message data generation server 101 can be used to: create data generation rules through built-in functions; define and describe the message by creating fields of the message; generate a data template by parsing the definition description of the message; fill in the data template according to the data type and data generation rule to generate message data.
[0061] As another example, in some embodiments, the message data generation server 101 may send a method for automatically generating message data according to the interface definition to the message data generation client 103 for user use based on a request of the message data generation client 103 .
[0062] Figure 2 This is an example diagram of the architecture of a message data generation server of a system for automatically generating message data according to an interface definition according to an embodiment of the present application, such as Figure 2 As shown, the message data generation server of this embodiment includes:
[0063] The built-in function module is used to create data generation rules through built-in functions. The built-in function module of this embodiment can generate data rules through a predetermined default built-in function, and can also obtain personalized data generation rules through a custom function.
[0064] For example, in order to generate more personalized business data and improve the convenience, scalability and flexibility of message data generation, the built-in function module of this embodiment includes two modes: default built-in function and custom built-in function. The method name of the method defined by the built-in function module is used for configuration in the message definition, such as describing the value of the field data type attribute.
[0065] In this embodiment, the default built-in function is generated by the user according to the data types commonly used in the business field and the data generation rules that meet the business specifications according to the encoding method. For example, when the built-in function module of this embodiment is applied to the interface test field of civil aviation business, it may involve but is not limited to the default built-in functions shown in Table 1 below.
[0066] Table 1
[0067] Built-in function name describe Example Value flightNo Flight number CA1431A、MU1884F airline Airlines CA, MU cabin Cabin F, Y, H
[0068] In this embodiment, the user can use the custom built-in function to implement the data generation rules according to the business. For example, this embodiment provides an extended entry for data generation rules, and the data generation logic can be implemented by writing a built-in function class that uniformly inherits the IBusinessData interface. The built-in function class contains one or more methods for generating business data. The method name is used for configuration in the interface definition. The method name of the built-in function class that inherits the IBusinessData interface is not allowed to be repeated. In this embodiment, the IBusinessData interface is an interface in business process management and enterprise services, which is used to store and process the business data of the process.
[0069] The message definition module is used to define and describe the message by creating the fields of the message. For example, the message definition module of this embodiment is used to describe the data structure of the interface response message, describe the field composition and detailed information of the field in the response message, including the type, hierarchical structure, default value and generation rules of the field, and the generation rules include default values, references to built-in functions, etc.
[0070] For example, the message definition module in this embodiment is used to explain an interface message in detail, which is provided or managed by the interface definition party. Specifically, the message definition module is used to explain the format and hierarchical structure of the interface message, as well as the detailed information of the field. The detailed information of the field includes: the type and value method of the field. The properties included are: field name (name), field type (type), field subnode (properties), element description of the field (items), field default value (default), field data type (dataType). Among them, the field with the field type (type) value of object has field sub-properties (properties); the field with the field type (type) value of array has element descriptions (items). In this embodiment, a complete tree-structured message definition description is formed with properties and items attributes. Through the message definition description, a certain message template can be formed.
[0071] The data generation module is used to generate a data template by parsing the definition description of the message; fill the data template according to the data type and data generation rules to generate message data. For example, the data generation module is used to parse the message data structure defined by the message definition module, generate a data template, and generate the final message data according to the data type and data generation rules. In actual applications, by parsing the message definition, adding fields and generating data templates are completed, and according to the dataType type, the data value is filled into the data template by reflection, and finally a complete message data is generated.
[0072] As an optional example, in some embodiments, the message data generation client 103 is used to provide a visual message data generation interface, which is used to receive a user's selection input operation to automatically generate message data according to an interface definition, and to, in response to the selection input operation, obtain a message data generation interface corresponding to the option selected by the selection input operation from the message data generation server 101 and display the message data generation interface, wherein the message data generation interface at least displays information on automatically generating message data according to the interface definition and operation options for the information on automatically generating message data according to the interface definition.
[0073] In some embodiments, the communication network 102 can be any suitable combination of one or more wired and / or wireless networks. For example, the communication network 102 can include any one or more of the following: the Internet, an intranet, a wide area network (WAN), a local area network (LAN), a wireless network, a digital subscriber line (DSL) network, a frame relay network, an asynchronous transfer mode (ATM) network, a virtual private network (VPN), and / or any other suitable communication network. The message data generation client 103 can be connected to the communication network 102 via one or more communication links (e.g., communication link 104), and the communication network 102 can be linked to the message data generation service end 101 via one or more communication links (e.g., communication link 105). The communication link can be any communication link suitable for transmitting data between the message data generation client 103 and the message data generation service end 101, such as a network link, a dial-up link, a wireless link, a hard-wired link, any other suitable communication link, or any suitable combination of such links.
[0074] The message data generating client 103 may include any one or more clients that present an interface related to customizing and sorting the freight rate search results in an appropriate form for use and operation by the user. In some embodiments, the message data generating client 103 may include any suitable type of device. For example, in some embodiments, the message data generating client 103 may include a mobile device, a tablet computer, a laptop computer, a desktop computer, and / or any other suitable type of client device.
[0075] Although the message data generation server 101 is illustrated as one device, in some embodiments, any appropriate number of devices may be used to perform the functions performed by the message data generation server 101. For example, in some embodiments, multiple devices may be used to implement the functions performed by the message data generation server 101. Alternatively, a cloud service may be used to implement the functions of the message data generation server 101.
[0076] Based on the above system, an embodiment of the present application provides a method for automatically generating message data according to an interface definition, which is illustrated by the following embodiment.
[0077] Figure 3 1 is a flowchart of a method for automatically generating message data according to an interface definition according to an embodiment of the present application. The method for automatically generating message data according to an interface definition of this embodiment can be executed at a message data generation server, such as Figure 3 As shown, the method for automatically generating message data according to the interface definition includes the following steps:
[0078] Step S201: Create data generation rules through built-in functions.
[0079] As an optional example, this embodiment writes a built-in function class that uniformly inherits the business data processing interface, sets a data generation method in the written built-in function class, and configures a unique method name for the set data generation method. In addition, this embodiment can also use built-in functions to create data generation rules through coding according to the type of business data. In the prior art, data can be generated according to the description through json schema, but it is impossible to generate fields with business significance. This embodiment can generate fields with actual business significance by presetting related built-in variables, and generate message data that is more in line with reality.
[0080] Step S202: define and describe the message by creating fields of the message.
[0081] Figure 4 This is an example diagram of creating fields according to a method for automatically generating message data according to an interface definition in accordance with an embodiment of the present application. Figure 5 This is an example diagram of creating complex type field attributes according to a method for automatically generating message data according to an interface definition in accordance with an embodiment of the present application.
[0082] like Figure 4 and Figure 5 As shown, as an optional example, this embodiment creates a field by creating field information of a message, where the field information includes a field name, a field type attribute, a field default value attribute, and a field data type attribute.
[0083] It is determined whether the created field is a complex type field according to the field information. If the created field is not a complex type field, the next field is created by continuing to create the next field information of the message.
[0084] When the created field is a complex type field, determine whether the field type of the complex type field is an object based on the field information. If the field type of the complex type field is an object, create a field sub-attribute for the complex type field, create a sub-field description for the sub-field in the created field sub-attribute, and create a sub-field based on the created sub-field description.
[0085] If the field type of the complex type field is an array, create an element description attribute for the complex type field, create a field description for the array field in the created element description attribute, and create an array field according to the created field description.
[0086] If the field type of the complex type field is neither an object nor an array, continue to create the next field by creating the next field information of the message.
[0087] As an optional example, in this embodiment, the definition description of the data structure of the message is saved in json format. In this embodiment, the added interface message definition finally exists in json format, and the format of json is compatible with json schema, and the dataType attribute is extended on the basis of json schema, which is used to generate business data with specific fields using built-in functions, ensuring that the method and system of this embodiment are extensible and open. json schema is a language that describes json data structures, and provides a standardized method for describing the expected json structure. It allows the definition of the type, format, constraints and other information of json data to ensure that the data maintains a compatible and consistent format when it is interactively transmitted in various subsystems.
[0088] Step S203: Generate a data template by parsing the definition description of the message.
[0089] As an optional example, this embodiment creates a data filling object, parses the definition description of the message, adds the parsing result to the created data filling object, and generates a data template. For example, this embodiment can create a json type data filling object as a data template, generate fields into the json type data filling object, and complete the creation of the data template. In actual applications, this embodiment can fill data into the data template according to different data generation rules until the creation of the entire data is completed.
[0090] Step S204: Fill in the data template according to the data type and data generation rule to generate message data.
[0091] This embodiment traverses the message definition, obtains the description information of a single field, creates a complex type field and generates data if the field is a complex type, and fills the data if the field is a basic type. In this embodiment, the field type attribute value is not an object or data field, but a basic type field.
[0092] Figure 6 An example diagram of a process for creating a single field template and generating data according to a method for automatically generating message data according to an interface definition according to an embodiment of the present application; Figure 7 An example diagram of a process for creating complex type fields and generating data according to a method for automatically generating message data according to an interface definition in accordance with an embodiment of the present application; Figure 8 An example flow chart of creating basic type fields and generating data for a method of automatically generating message data according to an interface definition according to an embodiment of the present application.
[0093] like Figure 6 , Figure 7 and Figure 8As shown, as an optional example, this embodiment traverses the definition description of the message to obtain the description information of the field, which includes the field name, field type attribute, field sub-attribute, element description attribute, field default value attribute and field data type attribute of the field.
[0094] Create a field in the data template according to the description information of the single field obtained, determine whether the data type of the created field is the basic type, if the data type of the created field is the basic type, obtain the description information of the field, and determine whether the field data type attribute has a value according to the description information of the field obtained. If the field data type attribute has a value, use the corresponding function to generate data according to the value of the field data type attribute through the Java reflection mechanism, and use the generated data to assign values to the fields in the data template; if the field data type attribute has no value, determine whether the field default value attribute has a value, if the field default value attribute has a value, use the value of the field default value attribute to assign values to the fields in the data template; if the field data type attribute has no value, generate random data that conforms to the field type attribute, and use the generated random data to assign values to the fields in the data template.
[0095] For example, this embodiment uses the mock.js framework to generate random data that conforms to the field type attribute. The mock.js framework is a JavaScript library for generating random data and intercepting Ajax requests, and can generate various types of random data, such as strings, numbers, dates, objects, arrays, etc.
[0096] As an optional example, in this embodiment, if the data type of the created field is not a basic type, it is determined whether the field type of the field is an object.
[0097] If the field type of the complex type field is an object, obtain the field sub-attribute of the field. In actual applications, there are multiple values in the field sub-attribute, each value is an object, which represents a specific description of each sub-field of the current field; traverse the values of the obtained field sub-attribute, create the sub-field of the current field, and reflect and fill the corresponding data value into the data template according to the field data type of the sub-field. For example, execute the logic of creating a single field and generating data in the method of this embodiment, create the sub-field of the current field, and complete the recursive operation.
[0098] If the field type of the complex type field is an array, obtain the element description attribute of the field, the value of the element description attribute is one and only one, which represents the specific description of the array element; create a field description according to the array field in the element description attribute, create an array field according to the created field description, and reflect the corresponding data value into the data template according to the field data type of the array field. For example, execute the logic of creating a single field and generating data in the method of this embodiment, create the elements of the current array field, and complete the recursive operation.
[0099] Figure 8 FIG. 1 is a timing diagram of a method for automatically generating message data according to an interface definition according to an embodiment of the present application for generating message data. Figure 8 As shown, the user prepares the required built-in functions in advance, describes the message definition of the interface, forms definition structure data, and introduces the pre-prepared built-in function name in the description. The data template is generated by parsing according to the message definition of the interface, and the field data is generated according to the values of the three attributes of dataType, default, and type in each field. The priority of the generated data is from high to low: dataType, default, type, and finally the complete message data is formed.
[0100] The following is a further detailed description of a method for automatically generating message data according to an interface definition in a specific scenario in an embodiment of the present application.
[0101] In this scenario, the generation of message data is performed and implemented according to the following steps:
[0102] 1. Create a built-in function
[0103] Built-in functions can be pre-prepared default functions, or they can be defined as different types of data generation methods by implementing the tag interface IBusinessData to achieve personalized generation of business data. For example, the pseudo code for defining a built-in function for flight numbers is as follows:
[0104]
[0105] 2. Define interface message
[0106] The interface message definition is a prerequisite for the implementation of the method of this embodiment. It needs to be in a complete json format and can describe the name, type, level, data type and default value of each node. Built-in functions help generate business data that is more in line with business scenarios and are used in the dataType attribute of the interface definition. The interface definition and built-in functions are used as input to generate the final message data through the logic of data generation. The following is an example of a user-maintained message definition:
[0107]
[0108]
[0109]
[0110] 3. Message data generation
[0111] According to the interface message definition prepared above, the steps to generate complete message data are as follows:
[0112] (1) Create a data object: Get the attribute value of the requestStruct node, create an empty data object, and synchronously traverse the definition object to fill in the attributes and values of the data object. If the type is object, execute the following step (2); if the type is a basic type (string, number, boolean, and null), execute the following step (3); if the type is array, execute the following step (4).
[0113] (2) Create node data of the object type in the definition: traverse the definition object, obtain the name attribute, type attribute and properties attribute of the current requestStruct node, and add a req node to the data object according to the name attribute and type attribute. The node type is object, and a child node is added to it. And loop through the properties attribute and recursively execute step (1). Recursively obtain the definition node with name info and generate a data object. The data object generated according to the definition in the above steps is as follows:
[0114]
[0115] (3) Create node data of the basic type in the definition: the value of the basic type type is: string, number, boolean, null; and generate data that conforms to the business type according to dataType. The type of dataType corresponds to the built-in function of the program. The class where the function is located integrates the IBusinessData interface. The name of the interface is consistent with the value of dataType, such as flightNo (indicating the flight number); a flight number is randomly generated according to the custom logic of generating flight numbers, such as CA1234; if the definition node does not have a dataType attribute, a value is randomly generated according to the type type, such as the attribute param1. If there is a value attribute in the definition node, the generated attribute takes the value according to value, such as the attribute param2. The data generated according to the definition in the above steps is as follows:
[0116]
[0117]
[0118] The pseudo code for generating data according to dataType is as follows:
[0119]
[0120] (4) Create node data of the array type in the definition: If the type of the current node is array, create a data node of array type, and obtain the items attribute of the current definition node to generate the elements in the array. Determine the type attribute in the items attribute. If it is a basic type, generate the corresponding data according to step (3); if the type attribute is object, execute (2). And determine the number of generated elements according to the elementCount attribute. The data generated according to the definition in the above steps is as follows:
[0121]
[0122] Through the above steps, according to the complete interface definition, the final generated message is as follows:
[0123]
[0124] The method for automatically generating message data according to interface definition in the embodiment of the present application has the following beneficial technical effects:
[0125] 1. Use the dataType field in the message definition to define the business type of the value. You can flexibly adjust the data generation rules according to the changes in business scenarios to meet different data sample generation requirements;
[0126] 2. In addition to the default built-in functions, it also supports custom built-in functions, which improves the scalability of the device; the data generated by more built-in functions have business meanings, which are more in line with the actual business of civil aviation. At the same time, it solves the problem of rewriting custom functions in the existing technology, making it easier to use;
[0127] 3. Interface definition can be separated from the self-generated program, as long as the description of the interface definition conforms to the standard definition specification, which reduces the need to define the interface declaration node in the program using TypeScript, improves the flexibility of the program and the universality of the defined message format;
[0128] 4. The corresponding message data can be quickly generated according to different interface definitions without manual intervention and repetitive work.
[0129] like Fig. 9As shown, the present application also provides a device, including a processor 310, a communication interface 320, a memory 330 for storing a processor executable computer program, and a communication bus 340. The processor 310, the communication interface 320, and the memory 330 communicate with each other through the communication bus 340. The processor 310 implements the above-mentioned method of automatically generating message data according to the interface definition by running the executable computer program.
[0130] Among them, the computer program in the memory 330 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk.
[0131] The system embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, i.e., they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected based on actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art may understand and implement it without creative effort.
[0132] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods of each embodiment or some parts of the embodiment.
[0133] Although the present application has been described in detail with reference to the aforementioned embodiments, a person of ordinary skill in the art should understand that it is still possible to modify the technical solutions described in the aforementioned embodiments, or to make equivalent replacements for some of the technical features therein; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for automatically generating message data according to interface definition, characterized in that: The method comprises: Create data generation rules through built-in functions; Define and describe the message by creating message fields; Generate a data template by parsing the definition description of the message; Fill in the data template according to the data type and data generation rules to generate message data.
2. The method for automatically generating message data according to interface definition according to claim 1, characterized in that: Creating data generation rules through built-in functions includes: writing a built-in function class that uniformly inherits a business data processing interface, setting a data generation method in the written built-in function class, and configuring a unique method name for the set data generation method.
3. The method for automatically generating message data according to interface definition according to claim 1, characterized in that: Creating data generation rules through built-in functions also includes: creating data generation rules through coding using built-in functions according to business data types.
4. The method for automatically generating message data according to interface definition according to claim 1, characterized in that: Define and describe the message by creating message fields, including: Create a field by creating field information of a message, where the field information includes a field name, a field type attribute, a field default value attribute, and a field data type attribute; Determine whether the created field is a complex type field according to the field information. If the created field is not a complex type field, continue to create the next field by creating the next field information of the message. When the created field is a complex type field, determine whether the field type of the complex type field is an object based on the field information. If the field type of the complex type field is an object, create a field sub-attribute for the complex type field, create a sub-field description for the sub-field in the created field sub-attribute, and create a sub-field based on the created sub-field description. If the field type of the complex type field is an array, create an element description attribute for the complex type field, create a field description for the array field in the created element description attribute, and create an array field according to the created field description; If the field type of the complex type field is neither an object nor an array, continue to create the next field by creating the next field information of the message.
5. The method for automatically generating message data according to interface definition according to claim 1, characterized in that: The message is defined and described by creating message fields, and also includes: the definition and description of the data structure of the message is saved in JSON format.
6. The method for automatically generating message data according to interface definition according to claim 1, characterized in that: The data template is generated by parsing the definition description of the message, including: creating a data filling object, parsing the definition description of the message, adding the parsing result to the created data filling object, and generating the data template.
7. The method for automatically generating message data according to interface definition according to claim 1, characterized in that: Fill in the data template according to the data type and data generation rules to generate message data, including: Traverse the definition description of the message to obtain the description information of the field, which includes the field name, field type attribute, field sub-attribute, element description attribute, field default value attribute and field data type attribute of the field; Create a field in the data template according to the description information of the single field obtained, and determine whether the data type of the created field is a basic type. If the data type of the created field is a basic type, obtain the description information of the field, and determine whether the field data type attribute has a value according to the description information of the field obtained; If the field data type attribute has a value, use the corresponding function to generate data according to the value of the field data type attribute through the Java reflection mechanism, and use the generated data to assign values to the fields in the data template; If the field data type attribute has no value, determine whether the field default value attribute has a value. If the field default value attribute has a value, use the value of the field default value attribute to assign a value to the field in the data template. If the field data type attribute has no value, generate random data that conforms to the field type attribute, and use the generated random data to assign values to the fields in the data template.
8. The method for automatically generating message data according to interface definition according to claim 7, characterized in that: Fill in the data template according to the data type and data generation rules to generate message data, including: If the data type of the created field is not a basic type, determine whether the field type of the field is an object; If the field type of the complex type field is an object, obtain the field sub-attribute of the field, traverse the values of the obtained field sub-attribute, create a sub-field of the current field, and reflect and fill the corresponding data value into the data template according to the field data type of the sub-field; If the field type of the complex type field is an array, obtain the element description attribute of the field, create a field description based on the array field in the element description attribute, create an array field based on the created field description, and reflect and fill the corresponding data value into the data template based on the field data type of the array field.
9. A system for automatically generating message data according to interface definition, characterized in that: The system includes a message data generation server, and the message data generation server includes: Built-in function module, used to create data generation rules through built-in functions; The message definition module is used to define and describe the message by creating message fields; The data generation module is used to generate a data template by parsing the definition description of the message; fill the data template according to the data type and data generation rules to generate message data.
10. A computer device, characterized in that: The computer device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the steps of the method according to any one of claims 1 to 8 when executing the program.
Citation Information
Patent Citations
Simulation data generation method and system based on TypeScript type
CN117149656A