Add openapi-normalizer rule to set tags to vendor extension (#20713)

This commit is contained in:
abrevet-dev 2025-02-26 09:18:17 +01:00 committed by GitHub
parent 5581a2dd2c
commit 8040e9aae9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 106 additions and 2 deletions

View File

@ -573,6 +573,13 @@ Example:
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/java-okhttp/ --openapi-normalizer SET_TAGS_TO_OPERATIONID=true
```
- `SET_TAGS_TO_VENDOR_EXTENSION`: when set to a string value, tags will be set to the value of the provided vendor extension
Example:
```
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SET_TAGS_TO_VENDOR_EXTENSION=x-tags
```
- `ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE`: when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1) by adding x-unsigned to the schema
Example:

View File

@ -91,6 +91,10 @@ public class OpenAPINormalizer {
final String SET_TAGS_TO_OPERATIONID = "SET_TAGS_TO_OPERATIONID";
String setTagsToOperationId;
// when set to a string value, tags will be set to the value of the provided vendor extension
final String SET_TAGS_TO_VENDOR_EXTENSION = "SET_TAGS_TO_VENDOR_EXTENSION";
String setTagsToVendorExtension;
// when set to true, tags in all operations will be set to operationId or "default" if operationId
// is empty
final String FIX_DUPLICATED_OPERATIONID = "FIX_DUPLICATED_OPERATIONID";
@ -158,6 +162,7 @@ public class OpenAPINormalizer {
ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION);
ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS);
ruleNames.add(SET_TAGS_TO_OPERATIONID);
ruleNames.add(SET_TAGS_TO_VENDOR_EXTENSION);
ruleNames.add(FIX_DUPLICATED_OPERATIONID);
ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE);
ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY);
@ -224,6 +229,11 @@ public class OpenAPINormalizer {
rules.put(SET_TAGS_FOR_ALL_OPERATIONS, true);
}
setTagsToVendorExtension = inputRules.get(SET_TAGS_TO_VENDOR_EXTENSION);
if (setTagsToVendorExtension != null) {
rules.put(SET_TAGS_TO_VENDOR_EXTENSION, true);
}
if (inputRules.get(FILTER) != null) {
rules.put(FILTER, true);
@ -375,6 +385,8 @@ public class OpenAPINormalizer {
processSetTagsToOperationId(operation);
processSetTagsToVendorExtension(operation);
processFixDuplicatedOperationId(operation);
}
@ -885,8 +897,7 @@ public class OpenAPINormalizer {
}
/**
* Keep only first tag in the operation if the operation has more than
* one tag.
* Remove/hide the x-internal in operations and model.
*
* @param operation Operation
*/
@ -955,6 +966,34 @@ public class OpenAPINormalizer {
}
}
/**
* Set the tag name to the value of the provided vendor extension
*
* @param operation Operation
*/
private void processSetTagsToVendorExtension(Operation operation) {
if (StringUtils.isEmpty(setTagsToVendorExtension)) {
return;
}
if (operation.getExtensions() == null) {
return;
}
if (operation.getExtensions().containsKey(setTagsToVendorExtension)) {
operation.setTags(null);
Object argObj = operation.getExtensions().get(setTagsToVendorExtension);
if (argObj instanceof List) {
List<String> tags = (List<String>) argObj;
for (String tag : tags) {
operation.addTagsItem(tag);
}
} else {
operation.addTagsItem(String.valueOf(argObj));
}
}
}
private void processFixDuplicatedOperationId(Operation operation) {
if (!getRule(FIX_DUPLICATED_OPERATIONID)) {
return;

View File

@ -294,6 +294,21 @@ public class OpenAPINormalizerTest {
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "delete");
}
@Test
public void testOpenAPINormalizerSetTagsToVendorExtension() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml");
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 2);
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getExtensions().size(), 1);
Map<String, String> options = new HashMap<>();
options.put("SET_TAGS_TO_VENDOR_EXTENSION", "x-tags");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
openAPINormalizer.normalize();
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 1);
}
@Test
public void testAddUnsignedToIntegerWithInvalidMaxValue() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/addUnsignedToIntegerWithInvalidMaxValue_test.yaml");

View File

@ -0,0 +1,43 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/person/display/{personId}:
get:
x-tags:
- display
tags:
- person
- basic
parameters:
- name: personId
in: path
required: true
description: The id of the person to retrieve
schema:
type: string
operationId: list
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Person"
components:
schemas:
Person:
description: person
type: object
properties:
$_type:
type: string
lastName:
type: string
firstName:
type: string