Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -158,6 +162,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
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);
Expand Down Expand Up @@ -224,6 +229,11 @@ public void processRules(Map<String, String> inputRules) {
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);

Expand Down Expand Up @@ -375,6 +385,8 @@ private void normalizeOperation(Operation operation) {

processSetTagsToOperationId(operation);

processSetTagsToVendorExtension(operation);

processFixDuplicatedOperationId(operation);
}

Expand Down Expand Up @@ -885,8 +897,7 @@ private void processUseAllOfRefAsParent(Schema schema) {
}

/**
* 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
*/
Expand Down Expand Up @@ -955,6 +966,34 @@ private void processSetTagsToOperationId(Operation operation) {
}
}

/**
* 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ public void testOpenAPINormalizerSetTagsToOperationId() {
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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Loading