Method and device for generating local language OpenAPI document and implementation method

By intercepting annotations during Swagger's process of generating OpenAPI documents and obtaining localized descriptions in combination with the Spring framework's message source mechanism, the problem of API documents only supporting a single language in existing tools is solved, and multi-language support and localized document generation is realized, improving the availability and development convenience of API documents.

CN120066570APending Publication Date: 2025-05-30SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510118595.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-24
Publication Date
2025-05-30

AI Technical Summary

Technical Problem

The existing OpenAPI and Swagger tools do not provide multilingual switching and localization support during the generation of API documents, which makes it difficult to modify or expand the document content in different locales and cannot meet the needs of multilingual users.

Method used

By intercepting Swagger annotations in the Springfox framework's plug-in mechanism, and combining the Spring framework's message source mechanism, a localized description of API interface-related information is obtained from the externally configured language resource files, and applied to the OpenAPI document to realize the generation of the local language OpenAPI document.

Benefits of technology

Overcoming the limitations of existing tools that only support a single language, it provides multi-language support for API documents, allowing developers and users to obtain API documents in the local language in the local local environment, improving the usability of API documents and the convenience of software development and use.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120066570A_ABST
    Figure CN120066570A_ABST
Patent Text Reader

Abstract

The invention belongs to the field of software development, and particularly discloses a method and device for generating a local language OpenAPI document and an implementation method, and the method is implemented based on a plug-in mechanism of a Springfox framework and comprises the steps that Swagger annotations are intercepted in the process that Swagger generates the OpenAPI document, and the Swagger annotations are analyzed to obtain related information of an API interface; calling a message source mechanism of a Spring framework, and obtaining a localized description of the related information through an externally configured language resource file; and applying the localized description to the OpenAPI document generated by the Swager to replace the related information, so as to realize the generation of the local language OpenAPI document. According to the method, localized language conversion of the annotation information in the OpenAPI document is realized, and the convenience of software development and use is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of software development, and particularly relates to a method and device for generating a local language OpenAPI document and an implementation method. Background Art

[0002] In the field of software development, API documents are important tools for developers and users to understand and use software systems. API documents need to support multiple language functions so that developers and users can obtain detailed information about interface documents in different language environments.

[0003] Existing OpenAPI and Swagger tools are widely used API document generation tools, which provide powerful functions in aspects such as automatic generation and visualization of API documents. However, the default implementation of these tools usually only supports interface documents in a single language. In most cases, this single language is English because many documents and standards in the software development field use English as the main language.

[0004] For example, when developers use Swagger, they usually add various Swagger annotations in the code, such as @ApiOperation, @ApiImplicitParam, etc. The information of these annotations is presented in the generated API document in a single language set by the developer, and there is no simple mechanism to automatically convert this information into other languages. Existing OpenAPI and Swagger tools do not provide multi-language switching and localization support, which means that once the document content is determined during the generation of API documents, it is difficult to modify or expand it in different language environments, unable to meet the needs of multi-language users, and resulting in inconvenience in development and use in different language environments. Summary of the Invention

[0005] To solve the above problems, the present invention provides a method and device for generating a local language OpenAPI document and an implementation method, which realize the localization language conversion of annotation information in the OpenAPI document and improve the convenience of software development and use.

[0006] In a first aspect, the technical solution of the present invention provides a method for generating a local language OpenAPI document, which is implemented based on the plug-in mechanism of the Springfox framework and includes the following steps: S1, intercept Swagger annotations during the process of Swagger generating an OpenAPI document, and parse the Swagger annotations to obtain relevant information of the API interface; S2, call the message source mechanism of the Spring framework to obtain the localized description of the relevant information through the externally configured language resource file; S3, apply the localized description to the Swagger-generated OpenAPI document to replace the relevant information, and realize the generation of the OpenAPI document in the local language.

[0007] In an alternative embodiment, step S1 intercepts Swagger annotations during the process of Swagger generating the OpenAPI document, and parses the Swagger annotations to obtain the relevant information of the API interface, specifically including: S1.1, obtain the API operation annotation through the first context object lookup method, and store the lookup result in the first container object; S1.2, detect whether the first container object contains a non-null value. If it contains, execute step S1.3; otherwise, do nothing; S1.3, take out the data value in the first container object, and denote it as the first data value; S1.4, process the first data value through the system output printing method to obtain the operation description information and label information of the API interface; S1.5, obtain the API implicit parameter set annotation through the second context object lookup method, and store the lookup result in the second container object; S1.6, detect whether the second container object contains a non-null value. If it contains, execute step S1.7; otherwise, do nothing; S1.7, take out the data value in the second container object, and denote it as the second data value; S1.8, process the second data value through the system output printing method to obtain the parameter set information of the API interface; S1.9, obtain the API implicit parameter annotation through the third context object lookup method, and store the lookup result in the third container object; S1.10, detect whether the third container object contains a non-null value. If it contains, execute step S1.11; otherwise, do nothing; S1.11, take out the data value in the third container object, and denote it as the third data value; S1.12, process the third data value through the system output printing method to obtain the parameter information of the API interface.

[0008] In an alternative embodiment, step S2 calls the message source mechanism of the Spring framework to obtain the localized description of the relevant information through the externally configured language resource file, specifically including: S2.1. Detect whether the operation description information and tag information of the API interface are obtained. If so, execute step S2.2; otherwise, do nothing. S2.2. Detect whether the target identifier is included in the tag information of the API interface. If so, execute step S2.3; otherwise, do nothing. S2.3. Invoke the message source mechanism of the Spring framework to obtain the localized descriptions of the operation description information, parameter set information, and parameter information through an externally configured language resource file.

[0009] In an optional implementation, step S2.3 invokes the message source mechanism of the Spring framework to obtain the localized descriptions of the operation description information, parameter set information, and parameter information through an externally configured language resource file, specifically including: S2.31. Invoke the message source mechanism of the Spring framework to obtain the locale parameter according to the system default language setting. S2.32. Detect the file name of the externally configured language resource file, and filter out the language resource file whose file name is adapted to the locale parameter, that is, the localized language resource file. S2.33. Search for the localized descriptions adapted to the operation description information, parameter set information, and parameter information from the localized language resource text to obtain the localized descriptions of the operation description information, parameter set information, and parameter information.

[0010] In an optional implementation, step S2.33 searches for the localized descriptions adapted to the operation description information, parameter set information, and parameter information from the localized language resource text, specifically including: S2.33.1. According to the interface name and operation description information, construct an operation description information key according to a preset construction rule. S2.33.2. According to the interface name and parameter set name, construct a parameter set key according to a preset construction rule. S2.33.3. According to the interface name and parameter name, construct a parameter key according to a preset construction rule. S2.33.4. Search for the adapted key-value pairs from the localized language resource text according to the constructed operation description information key, parameter set key, and parameter key. S2.33.5. Obtain the localized descriptions adapted to the operation description information, parameter set information, and parameter information from the found key-value pairs.

[0011] In an optional implementation, step S3 applies the localized description to the Swagger to generate the OpenAPI document to replace the relevant information, thereby generating a local language OpenAPI document, specifically including: S3.1. Update the operation description information of the API interfaces in the OpenAPI document to the corresponding localized description through the context operation builder; S3.2. Update each parameter in the parameter set information of the API interfaces in the OpenAPI document to the corresponding localized description through the parameter builder; S3.3. Update the parameter information of the API interfaces in the OpenAPI document to the corresponding localized description through the parameter builder.

[0012] In a second aspect, the technical solution of the present invention provides a device for generating a local language OpenAPI document, including: An API interface information acquisition module, configured to intercept Swagger annotations during the process of Swagger generating the OpenAPI document, and parse the Swagger annotations to obtain the relevant information of the API interfaces; A localized description acquisition module, configured to call the message source mechanism of the Spring framework to obtain the localized description of the relevant information through an externally configured language resource file; A localized description application module, configured to apply the localized description to the Swagger-generated OpenAPI document to replace the relevant information, thereby realizing the generation of the local language OpenAPI document.

[0013] In a third aspect, the technical solution of the present invention provides an implementation method for generating a local language OpenAPI document, including the following steps: SS1. Build a language resource file for each language, and store the relevant information of the API interfaces and their translation descriptions in the language resource file; SS2. Store the language resource file locally; SS3. In the Spring configuration, define an instance of the message source mechanism through the Bean annotation and specify the storage location of the language resource file; SS4. Based on the Springfox framework, write a custom plugin by implementing the operation builder plugin interface, and override the application method in the plugin; SS5. Call the application method during the process of Swagger generating the OpenAPI document to implement the method for generating the local language OpenAPI document described in any one of the above.

[0014] In an optional implementation manner, storing the relevant information of the API interfaces and their translation descriptions in the language resource file in step SS1 specifically includes: Storing the relevant information of the API interfaces and their translation descriptions in the language resource file in the form of key-value pairs.

[0015] In an optional embodiment, after constructing a language resource file for each language in step SS1, the method further includes: Naming the language resource file, where the naming name contains a language type identifier.

[0016] A method, device, and implementation method for generating a local language OpenAPI document provided by the present invention have the following beneficial effects compared with the prior art: Based on the plug-in mechanism of the Springfox framework, a plug-in is executed during the process of Swagger generating the OpenAPI document to intercept Swagger annotations, and in combination with the message source mechanism of the Spring framework, the Swagger annotations are converted into localized descriptions. The localized descriptions of API interface-related information are automatically obtained from an externally configured language resource file, and these localized descriptions are accurately applied to the generated OpenAPI document, replacing the original single-language information. The present invention overcomes the limitation that existing OpenAPI and Swagger tools only support a single language. By utilizing the plug-in mechanism of the Springfox framework and the message source mechanism of the Spring framework, it can provide localized language support for API documents, enabling developers and users to obtain detailed information of API documents in the local language environment, improving the usability of API documents, and enhancing the convenience of software development and use. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the technical solutions of the present invention, the drawings required for description will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0018] Figure 1 It is a schematic flowchart of an implementation method for generating a local language OpenAPI document provided by an embodiment of the present invention.

[0019] Figure 2 It is a schematic flowchart of a method for generating a local language OpenAPI document provided by an embodiment of the present invention.

[0020] Figure 3A It is a schematic flowchart of a specific embodiment of a method for generating a local language OpenAPI document provided by the present invention Figure 1 .

[0021] Figure 3B It is a schematic flowchart of a specific embodiment of a method for generating a local language OpenAPI document provided by the present invention Figure 2 .

[0022] Figure 4 A schematic block diagram of a device structure for generating a local language OpenAPI document provided by an embodiment of the present invention. Detailed implementation manners

[0023] To make the objectives, features, and advantages of the present invention more obvious and understandable, the technical solutions in the present invention will be clearly and completely described below with reference to the accompanying drawings in the specific embodiments of the present invention. Obviously, the embodiments described below are only a part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the scope of protection of this application.

[0024] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those of ordinary skill in the technical field to which the present invention belongs. The terms used in the description of the present invention in this specification are only for the purpose of describing specific embodiments, and are not intended to limit the present invention.

[0025] Figure 1 A schematic flowchart of an implementation method for generating a local language OpenAPI document provided by an embodiment of the present invention. In this embodiment, OpenAPI internationalization is implemented based on the Springfox framework. As a commonly used Swagger integration tool, Springfox can automatically generate API documents based on the OpenAPI standard, but its default implementation does not include internationalization functions. Therefore, in this embodiment, based on the Springfox framework plug-in mechanism, the MessageSource mechanism is combined to obtain the translation text in the corresponding language and replace the default document content, so as to realize the generation of multi-language documents. As Figure 1 shown, the implementation method specifically includes the following steps.

[0026] SS1. Build a language resource file for each language, and store the relevant information of the API interface and its translation description in the language resource file.

[0027] In some implementation manners, the user builds a language resource file for the required language according to the development requirements, and stores the relevant information of the API interface and its translation description in the language resource file. Here, the relevant information of the API interface refers to Swagger annotation information, such as the information included in @ApiOperation, @ApiImplicitParam, @ApiImplicitParams, etc. The translation description refers to the content obtained by translating the relevant information into the corresponding language. For example, translate api.operation.getUser.summary into Obtain user information.

[0028] SS2, store the language resource file locally.

[0029] SS3, in the Spring configuration, define an instance of the message source mechanism through the Bean annotation and specify the storage location of the language resource file.

[0030] SS4, based on the Springfox framework, write a custom plugin by implementing the operation builder plugin interface and override the apply method in this plugin.

[0031] SS5, call the overridden apply method during the process of Swagger generating the OpenAPI document to implement the generation of the local language OpenAPI document.

[0032] The overridden apply method mainly intercepts and parses the Swagger annotations on the API interface to obtain the description information and parameter explanations of the interface. By calling Spring's MessageSource, according to the language currently configured in the system, obtain the localized description text and replace the default document content. The method for generating the local language OpenAPI document will be described in detail in the following embodiments.

[0033] This embodiment solves the internationalization problem of API documents in a multi-language environment by extending Springfox. Usually, API documents (such as those generated by Swagger) are default written in English or other single languages and cannot dynamically provide localized document descriptions and parameter information according to the user's language environment. For this reason, this embodiment uses a custom plugin to intervene during the document generation process, adopts Spring's internationalization mechanism, and realizes multi-language support for API documents by configuring different language resource files.

[0034] In an alternative embodiment, the language resource file is stored in the.properties format, with each language corresponding to a file. The file content includes the relevant information of the API interface and its translation description, in the form of key-value pairs, as shown in the following example: Chinese: api.operation.getUser.summary=Get user information api.operation.getUser.param.query.userId.description=User ID English: api.operation.getUser.summary=Get User Infomation api.operation.getUser.param.query.userId.description=User ID In an alternative embodiment, after constructing a language resource file for each language, the language resource file is named, and the naming name contains a language type identifier. Subsequently, the message source mechanism filters out the language resource files of the required local languages through the naming of the language resource files. Exemplarily, the suffixes of the resource file names for different languages are different. For example, the Chinese resource file is named messages_zh.properties, and the English resource file is messages_en.properties. Through these resource files, the system can flexibly expand any number of languages.

[0035] In an alternative embodiment, in the Spring configuration, the developer can define a MessageSource instance through the @Bean annotation and specify the location of the language resource file. Exemplarily: @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } In this way, the system can load the corresponding resource file according to the current Locale parameter and provide the translated text. It should be noted that the developer can set the default language environment by modifying the openapi.default-language property in the system configuration file.

[0036] In an alternative embodiment, by implementing the OperationBuilderPlugin interface, a custom plugin CustomSwaggerPlugin is written, and the apply method is overridden in the plugin. The apply method is called during the Swagger document generation process and is responsible for intercepting the generation operation of each API interface and obtaining relevant information of the interface, such as the interface name, the operation description of the interface, the interface parameters, etc.

[0037] Specifically, the Swagger plugin class overrides the OperationBuilderPlugin (operation builder plugin interface). The OperationBuilderPlugin class is a plugin interface class in Springfox, an open-source framework used by Spring Boot to automatically generate RESTful API documentation (Swagger). The operation information in the generated Swagger documentation can be customized or enhanced by overriding the apply method therein.

[0038] The main implementation logic of the apply method is as follows: find the annotation with @ApiOperation, filter according to the OpenAPI tags, read the language in the configuration and initialize the locale parameter, use the MessageSource.getMessage method in the Spring framework context to obtain the corresponding message, and assign the message to the summary of the operation and the description of the parameters according to the context parameters, so as to achieve the purpose of internationalization according to the configured language.

[0039] Part of the code of the apply method is as follows: public void apply(OperationContext context) { Optional <apioperation>annotation = context.findAnnotation(ApiOperation.class); if (annotation.isPresent()) { List <string>tags = Arrays.asList(annotation.get().tags()); if (tags.contains("OpenAPI")) { Locale locale = new Locale(defaultLanguage, ""); if ("".equals(locale.toString())) { locale.setDefault(new Locale("zh", "")); } String key = "api.operation." + context.getName() + ".summary"; String localizedSummary = messageSource.getMessage(key, null,locale); Optional <apiimplicitparams>apiImplicitParams = context.findAnnotation(ApiImplicitParams.class); List <parameter>parameters = new ArrayList<>(); if (apiImplicitParams.isPresent()) { List <apiimplicitparam>apiImplicitParamList = Arrays.asList(apiImplicitParams.get().value()); for (ApiImplicitParam apiImplicitParam : apiImplicitParamList) { Parameter parameter = getParameter(context, locale,apiImplicitParam); parameters.add(parameter); } } Optional <apiimplicitparam>apiImplicitParam = context.findAnnotation(ApiImplicitParam.class); if (apiImplicitParam.isPresent()) { ApiImplicitParam apiImplicitParamValue = apiImplicitParam.get(); Parameter parameter = getParameter(context, locale, apiImplicitParamValue); parameters.add(parameter); } context.operationBuilder().summary(localizedSummary).parameters(parameters).build(); } } } Figure 2 Schematic diagram of a method for generating a local language OpenAPI document provided by an embodiment of the present invention, where Figure 2 The execution subject may be a device for generating a local language OpenAPI document. The method for generating a local language OpenAPI document provided by an embodiment of the present invention is executed by a computer device. Correspondingly, the device for generating a local language OpenAPI document runs in the computer device.

[0040] This method is implemented based on the plugin mechanism of the Springfox framework. As Figure 2 shown, this method includes the following steps.

[0041] S1, Intercept Swagger annotations during the process of Swagger generating an OpenAPI document, and parse the Swagger annotations to obtain relevant information of the API interface.

[0042] S2, Invoke the message source mechanism of the Spring framework to obtain the localized description of the relevant information through an externally configured language resource file.

[0043] S3, Apply the localized description to the Swagger-generated OpenAPI document to replace the relevant information, and realize the generation of a local language OpenAPI document.

[0044] Based on the plug-in mechanism of the Springfox framework, this embodiment executes a plug-in during the process of Swagger generating the OpenAPI document, intercepts Swagger annotations, and combines the message source mechanism of the Spring framework to convert Swagger annotations into localized descriptions. It automatically obtains the localized descriptions of API interface-related information from an externally configured language resource file and accurately applies these localized descriptions to the generated OpenAPI document, replacing the original single-language information. The present invention overcomes the limitation that existing OpenAPI and Swagger tools only support a single language. By utilizing the plug-in mechanism of the Springfox framework and the message source mechanism of the Spring framework, it can provide localized language support for API documents, enabling developers and users to obtain detailed information about API documents in the local language environment, improving the usability of API documents, and enhancing the convenience of software development and use.

[0045] To further understand the present invention, the following provides a specific embodiment to elaborate in detail on the method for generating a localized language OpenAPI document of the present invention. Figure 3A and Figure 3B is the schematic flow diagram of this specific embodiment.

[0046] S1. During the process of Swagger generating the OpenAPI document, intercept Swagger annotations and parse the Swagger annotations to obtain relevant information about the API interface. Step S1 specifically includes the following sub-steps.

[0047] S1.1. Obtain API operation annotations through the first context object lookup method and store the lookup result in the first container object.

[0048] In the custom plug-in, first obtain the @ApiOperation annotation (API operation annotation) on the interface through the context.findAnnotation(ApiOperation.class) method. This annotation contains the description information of the API operation (such as summary) and the tags of this interface.

[0049] The context.findAnnotation(ApiOperation.class) method will return an Optional <apioperation>The object, because in the actual code, developers may not add the ApiOperation annotation to API operations (usually controller methods). Therefore, the findAnnotation method may not find the annotation. At this time, an Optional object without a value is returned instead of null. Using Optional can avoid NullPointerException.

[0050] S1.2, Detect whether the first container object contains a non-null value. If it does, execute step S1.3; otherwise, do nothing.

[0051] To use the ApiOperation annotation information obtained from Optional, it is necessary to first check whether the annotation exists. Specifically, the isPresent() method is used to check whether the Optional object contains a non-null value. If it does, the get() method can be used to safely obtain the value. If it does not, isPresent() is false and the get() method will not be called, thus avoiding operations on null and also avoiding NullPointerException.

[0052] S1.3, Take out the data value in the first container object and record it as the first data value.

[0053] If the ApiOperation annotation exists, use the get() method to obtain the annotation from the Optional object.

[0054] The get() method is a method of the Optional class. When the isPresent() method returns true, the get() method can be used to obtain the actually stored value from the Optional object. However, it should be noted that if isPresent() is false, calling get() may cause an exception. Therefore, isPresent() is usually used for checking before using get().

[0055] S1.4, Process the first data value through the system output printing method to obtain the operation description information and label information of the API interface.

[0056] Exemplarily, information acquisition is implemented by executing System.out.println(apiOperation.summary()). Among them, System.out.println() is a statement in Java for outputting information to the console, and apiOperation.summary() is to call the summary() method of the apiOperation object to obtain its summary information, so as to output the summary information of the apiOperation object to the console.

[0057] S1.5, obtain the API implicit parameter set annotation through the second context object lookup method, and store the lookup result in the second container object.

[0058] Obtain the @ApiImplicitParams annotation (API implicit parameter set annotation) on the interface through the context.findAnnotation(ApiImplicitParams.class) method. The processing of the API implicit parameter set annotation in steps S1.5 - S1.8 is similar to the processing of the API operation annotation in steps S1.1 - S1.4, and will not be elaborated here.

[0059] S1.6, detect whether the second container object contains non - null values. If it does, execute step S1.7; otherwise, do nothing.

[0060] S1.7, take out the data value in the second container object and denote it as the second data value.

[0061] S1.8, process the second data value through the system output printing method to obtain the parameter set information of the API interface.

[0062] S1.9, obtain the API implicit parameter annotation through the third context object lookup method, and store the lookup result in the third container object.

[0063] Obtain the @ApiImplicitParam annotation (API implicit parameter annotation) on the interface through the context.findAnnotation(ApiImplicitParam.class) method. The processing of the API implicit parameter set annotation in steps S1.9 - S1.12 is similar to the processing of the API operation annotation in steps S1.1 - S1.4, and will not be elaborated here.

[0064] S1.10, detect whether the third container object contains non - null values. If it does, execute step S1.11; otherwise, do nothing.

[0065] S1.11, take out the data value in the third container object and denote it as the third data value.

[0066] S1.12, process the third data value through the system output printing method to obtain the parameter information of the API interface.

[0067] S2, call the message source mechanism of the Spring framework to obtain the localized description of the relevant information through the externally configured language resource file. Step S2 specifically includes the following sub-steps.

[0068] S2.1, detect whether the operation description information and label information of the API interface are obtained. If so, execute step S2.2; otherwise, do nothing.

[0069] S2.2, detect whether the target identifier is included in the label information of the API interface. If so, execute step S2.3; otherwise, do nothing.

[0070] In some embodiments, the target identifier is OpenAPI. If the label of the interface contains "OpenAPI", it means that the interface needs to be internationalized. At this time, the plugin will call MessageSource to obtain the localized interface summary and parameter description.

[0071] S2.3, call the message source mechanism of the Spring framework to obtain the localized descriptions of the operation description information, parameter set information, and parameter information through the externally configured language resource file.

[0072] S2.31, call the message source mechanism of the Spring framework to obtain the locale parameter according to the system default language setting.

[0073] It should be noted that developers can set the default language environment by modifying the openapi.default-language property in the system configuration file. The messageSource (message source) mechanism loads the corresponding resource file according to the current Locale and provides the translated text.

[0074] S2.32, detect the file name of the externally configured language resource file, and filter out the language resource file whose file name is adapted to the locale parameter, that is, the localized language resource file.

[0075] It should be noted that the file name of the language resource file contains a language identifier. Exemplarily, messages_zh.properties, messages_en.properties, etc. zh represents Chinese, and en represents English. If the locale parameter indicates that the local language is Chinese, then messages_zh.properties is loaded, and this file is the localized language resource file.

[0076] S2.33. Search for the localized descriptions in the localized language resource text that match the operation description information, parameter set information, and parameter information, and obtain the localized descriptions of the operation description information, parameter set information, and parameter information. This step specifically includes the following sub-steps.

[0077] S2.33.1. According to the interface name and operation description information, construct an operation description information key according to the preset construction rules.

[0078] S2.33.2. According to the interface name and parameter set name, construct a parameter set key according to the preset construction rules.

[0079] S2.33.3. According to the interface name and parameter name, construct a parameter key according to the preset construction rules.

[0080] S2.33.4. Search for the matching key-value pairs in the localized language resource text according to the constructed operation description information key, parameter set key, and parameter key.

[0081] S2.33.5. Obtain the localized descriptions that match the operation description information, parameter set information, and parameter information from the found key-value pairs.

[0082] The relevant content is stored in the language resource text in the form of key-value pairs. To obtain the localized descriptions, the keys in the language file (such as api.operation.[controller method name].summary or api.operation. [controller method name].param.query.[parameter name].description) are constructed through the information such as the interface name and parameter name defined in the annotation, and then the matching key-value pairs are found from the localized language resource file through the constructed keys, and then the localized descriptions are obtained.

[0083] S3. Apply the localized descriptions to the Swagger to generate the OpenAPI document and replace the relevant information to generate the local language OpenAPI document.

[0084] S3.1. Update the operation description information of the API interface in the OpenAPI document to the corresponding localized description through the context operation builder.

[0085] In the apply method, after finding the ApiOperation annotation and obtaining the localized description, update it to the information of the API operation. Use context.operationBuilder() to update the summary and description of the operation.

[0086] S3.2. Update each parameter in the parameter set information of the API interface in the OpenAPI document to its corresponding localized description through the parameter builder.

[0087] S3.3. Update the parameter information of the API interface in the OpenAPI document to its corresponding localized description through the parameter builder.

[0088] For parameters, after finding the ApiImplicitParam or ApiImplicitParams annotation, use the ParameterBuilder to update the description of the parameter. First, obtain the original parameter information, and then replace it with the localized description. When there are multiple parameters, use the ApiImplicitParams annotation and iterate through the list of ApiImplicitParam to update the description of each parameter.

[0089] In the apply method, apply the updated operation summary, operation description, and parameter information to the OperationBuilder, and use the build() method to complete the construction of the operation information. After completing the above update operations, Springfox will continue to generate other parts of the OpenAPI document and finally generate a complete localized OpenAPI document. This process is the internal process of the Springfox framework. After replacing the localized information of the operations and parameters, Springfox will integrate this information into the final document according to the modifications.

[0090] In the above text, an embodiment of a method for generating a localized language OpenAPI document is described in detail. Based on the method for generating a localized language OpenAPI document described in the above embodiment, an embodiment of the present invention also provides an apparatus for generating a localized language OpenAPI document corresponding to this method.

[0091] Figure 4 FIG. is a schematic block diagram of the structure of an apparatus for generating a localized language OpenAPI document provided by an embodiment of the present invention. In this embodiment, the apparatus for generating a localized language OpenAPI document can be divided into multiple functional modules according to the functions it performs. The module referred to in the present invention means a series of computer program segments that can be executed by at least one processor and can complete a fixed function, and is stored in the memory.

[0092] The API interface information acquisition module is used to intercept Swagger annotations during the process of Swagger generating the OpenAPI document, and parse the Swagger annotations to obtain the relevant information of the API interface.

[0093] The localization description acquisition module is used to call the message source mechanism of the Spring framework and obtain the localization description of the relevant information through a language resource file configured externally.

[0094] The localization description application module is used to apply the localization description to the Swagger-generated OpenAPI document to replace the relevant information, thereby realizing the generation of an OpenAPI document in the local language.

[0095] The device for generating an OpenAPI document in the local language in this embodiment is used to implement the foregoing method for generating an OpenAPI document in the local language. Therefore, the specific implementation in this device can be seen in the embodiment part of the method for generating an OpenAPI document in the local language in the foregoing text. Therefore, its specific implementation can refer to the descriptions of the corresponding various part embodiments and will not be elaborated here.

[0096] In addition, since the device for generating an OpenAPI document in the local language in this embodiment is used to implement the foregoing method for generating an OpenAPI document in the local language, its function corresponds to the function of the above method and will not be elaborated here.

[0097] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to those skilled in the art. The general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to these embodiments shown herein, but rather will be accorded the widest scope consistent with the principles and novel features disclosed herein.< / apioperation> < / apiimplicitparam> < / apiimplicitparam> < / parameter> < / apiimplicitparams> < / string> < / apioperation>

Claims

1. A method for generating a local language OpenAPI document, characterized in that: This method is implemented based on the plug-in mechanism of the Springfox framework and includes the following steps: S1, intercepts Swagger annotations during the process of generating OpenAPI documents by Swagger, and parses the Swagger annotations to obtain relevant information of the API interface; S2, calling the message source mechanism of the Spring framework, and obtaining the localized description of the relevant information through the externally configured language resource file; S3, applying the localized description to the OpenAPI document generated by Swagger to replace the relevant information and realize the generation of the local language OpenAPI document.

2. The method for generating a local language OpenAPI document according to claim 1, characterized in that: Step S1 intercepts Swagger annotations during the process of generating OpenAPI documents by Swagger, and parses the Swagger annotations to obtain relevant information of the API interface, including: S1.1, obtaining API operation annotations through a first context object search method, and storing the search results in a first container object; S1.2, check whether the first container object contains a non-null value, if so, execute step S1.3, otherwise, do nothing; S1.3, taking out the data value in the first container object, and recording it as the first data value; S1.4, processing the first data value through the system output printing method to obtain operation description information and label information of the API interface; S1.5, obtaining the API implicit parameter set annotation through the second context object search method, and storing the search result in the second container object; S1.6, check whether the second container object contains a non-null value, if so, execute step S1.7, otherwise, do nothing; S1.7, taking out the data value in the second container object and recording it as the second data value; S1.8, processing the second data value by a system output printing method to obtain parameter set information of the API interface; S1.9, obtaining the API implicit parameter annotation through the third context object search method, and storing the search result in the third container object; S1.10, check whether the third container object contains a non-null value, if so, execute step S1.11, otherwise, do nothing; S1.11, extract the data value in the third container object and record it as the third data value; S1.12, processing the third data value through the system output printing method to obtain parameter information of the API interface.

3. The method for generating a local language OpenAPI document according to claim 2, characterized in that: Step S2 calls the message source mechanism of the Spring framework and obtains the localized description of the relevant information through the externally configured language resource file, specifically including: S2.1, check whether the operation description information and label information of the API interface are obtained. If so, proceed to step S2.2, otherwise no processing is performed; S2.2, check whether the tag information of the API interface contains the target identifier, if so, proceed to step S2.3, otherwise, no processing; S2.3, calls the message source mechanism of the Spring framework, and obtains the operation description information, parameter set information, and localized description of parameter information through the externally configured language resource file.

4. The method for generating a local language OpenAPI document according to claim 3, characterized in that: Step S2.3 calls the message source mechanism of the Spring framework to obtain the operation description information, parameter set information, and localized description of parameter information through the externally configured language resource file, specifically including: S2.31, calling the message source mechanism of the Spring framework, and obtaining the locale parameters according to the system default language settings; S2.32, detecting the file name of the externally configured language resource file, and selecting the language resource file whose file name is adapted to the regional setting parameters, i.e., the localized language resource file; S2.33, searching the localized description adapted to the operation description information, parameter set information, and parameter information from the localized language resource text, and acquiring the localized description of the operation description information, parameter set information, and parameter information.

5. The method for generating a local language OpenAPI document according to claim 4, characterized in that: Step S2.33 searches the localized language resource text for a localized description that matches the operation description information, parameter set information, and parameter information, specifically including: S2.33.1, construct an operation description information key according to the interface name and the operation description information according to the preset construction rules; S2.33.2, construct a parameter set key according to the interface name and parameter set name according to a preset construction rule; S2.33.3, construct parameter keys according to the interface name and parameter name according to preset construction rules; S2.33.4, searching for an adapted key-value pair from the localized language resource text according to the build operation description information key, parameter set key, and parameter key; S2.33.5, obtain a localized description adapted to the operation description information, parameter set information, and parameter information from the searched key-value pair.

6. The method for generating a local language OpenAPI document according to claim 5, characterized in that: Step S3 applies the localized description to the OpenAPI document generated by Swagger to replace the relevant information, thereby generating the local language OpenAPI document, which specifically includes: S3.1, update the operation description information of the API interface in the OpenAPI document to the corresponding localized description through the context operation builder; S3.2, updating each parameter in the parameter set information of the API interface in the OpenAPI document to a corresponding localized description through the parameter builder; S3.3, update the parameter information of the API interface in the OpenAPI document to the corresponding localized description through the parameter builder.

7. A device for generating a local language OpenAPI document, characterized in that: include: The API interface information acquisition module is used to intercept Swagger annotations during the process of Swagger generating OpenAPI documents, and parse the Swagger annotations to obtain relevant information about the API interface; A localized description acquisition module is used to call the message source mechanism of the Spring framework and obtain the localized description of the relevant information through an externally configured language resource file; The localized description application module is used to apply the localized description to the OpenAPI document generated by Swagger to replace the relevant information and realize the generation of the local language OpenAPI document.

8. A method for generating a local language OpenAPI document, characterized in that: The following steps are involved: SS1, build a language resource file for each language, which stores the relevant information of the API interface and its translation description; SS2, store the language resource file locally; SS3, in the Spring configuration, define a message source mechanism instance through Bean annotation and specify the storage location of the language resource file; SS4, based on the Springfox framework, implements the action builder plugin interface, writes a custom plugin, and overrides the application method in the plugin; SS5, calling the application method in the process of Swagger generating an OpenAPI document to implement the method described in any one of claims 1-6.

9. The method for generating a local language OpenAPI document according to claim 8, characterized in that: In step SS1, the language resource file stores the relevant information of the API interface and its translation description, including: Store the API interface information and its translation description in the language resource file in the form of key-value pairs.

10. The method for generating a local language OpenAPI document according to claim 9, characterized in that: After building a language resource file for each language in step SS1, it also includes: Name the language resource file, including the language type identifier in the name.