Skip to content

Commit b82a63a

Browse files
committed
Implementation of RemoveFilter in OpenaAPINormalizer
1 parent 0252f31 commit b82a63a

17 files changed

Lines changed: 2692 additions & 53 deletions

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 545 additions & 53 deletions
Large diffs are not rendered by default.

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@
1616

1717
package org.openapitools.codegen;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1921
import io.swagger.v3.oas.models.OpenAPI;
2022
import io.swagger.v3.oas.models.Operation;
2123
import io.swagger.v3.oas.models.PathItem;
2224
import io.swagger.v3.oas.models.media.*;
2325
import io.swagger.v3.oas.models.parameters.Parameter;
2426
import io.swagger.v3.oas.models.responses.ApiResponse;
2527
import io.swagger.v3.oas.models.security.SecurityScheme;
28+
import lombok.Data;
29+
import org.openapitools.codegen.serializer.SerializerUtils;
2630
import org.openapitools.codegen.utils.ModelUtils;
31+
import org.opentest4j.AssertionFailedError;
32+
import org.testng.annotations.DataProvider;
2733
import org.testng.annotations.Test;
2834

35+
import java.io.*;
36+
import java.net.URL;
2937
import java.util.*;
3038

3139
import static org.testng.Assert.*;
@@ -1267,4 +1275,67 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
12671275
}
12681276
}
12691277

1278+
/**
1279+
* get a list of all files matching openapi_normalizer/*_config.yaml.
1280+
*/
1281+
@DataProvider(name="testConfigs")
1282+
public Object[][] getTestConfigs() {
1283+
URL path = getClass().getClassLoader().getResource("openapi_normalizer");
1284+
File[] files = new File(path.getFile())
1285+
.listFiles(file -> file.getName().endsWith("_config.yaml"));
1286+
return Arrays.stream(files)
1287+
.map(file -> new Object[]{"src/test/resources/openapi_normalizer/" +file.getName()})
1288+
.toArray(Object[][]::new);
1289+
}
1290+
1291+
@Test(dataProvider = "testConfigs")
1292+
public void executeAllTests(String specPath) {
1293+
OpenapiNormalizerTestConfig config = OpenapiNormalizerTestConfig.fromFile(specPath);
1294+
OpenAPI openAPI = TestUtils.parseSpec(config.inputSpec);
1295+
OpenAPINormalizer openAPINormalizer = OpenAPINormalizer.createNormalizer(openAPI, config.inputRules);
1296+
openAPINormalizer.normalize();
1297+
String expected = SerializerUtils.toYamlString(TestUtils.parseSpec(config.normalizedSpec));
1298+
String after = SerializerUtils.toYamlString(openAPI);
1299+
if (!expected.equals(after)) {
1300+
throw new AssertionFailedError("Unexpected normalized result for\n" + config, expected, after);
1301+
}
1302+
}
1303+
1304+
/**
1305+
* Custom remove filter used by custom_remove_filter_config.yaml
1306+
*/
1307+
public static class CustomRemoveFilter extends OpenAPINormalizer {
1308+
public CustomRemoveFilter(OpenAPI openAPI, Map<String, String> inputRules) {
1309+
super(openAPI, inputRules);
1310+
}
1311+
1312+
@Override
1313+
protected RemoveFilter createRemoveFilter(String filter) {
1314+
return new RemoveFilter(filter) {
1315+
protected boolean matchOperation(String path, PathItem.HttpMethod method, Operation operation) {
1316+
return operation.getExtensions() != null && "admin".equals(operation.getExtensions().get("x-role"));
1317+
}
1318+
};
1319+
}
1320+
}
1321+
1322+
/**
1323+
* Normalizer configuration in yaml files.
1324+
*/
1325+
@Data
1326+
static class OpenapiNormalizerTestConfig {
1327+
Map<String, String> inputRules;
1328+
String inputSpec;
1329+
String normalizedSpec;
1330+
1331+
static OpenapiNormalizerTestConfig fromFile(String specPath) {
1332+
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
1333+
try (InputStream inputStream = new FileInputStream(new File(specPath))) {
1334+
return objectMapper.readValue(inputStream, OpenapiNormalizerTestConfig.class);
1335+
} catch (IOException e) {
1336+
throw new RuntimeException("Unable to read from " + specPath, e);
1337+
}
1338+
}
1339+
}
1340+
12701341
}

modules/openapi-generator/src/test/resources/3_0/allOf_composition_discriminator.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ paths:
3030
application/json:
3131
schema:
3232
$ref: '#/components/schemas/MyPets'
33+
/a:
34+
get:
35+
responses:
36+
'200':
37+
description: desc
38+
content:
39+
application/json:
40+
schema:
41+
$ref: '#/components/schemas/A'
3342
components:
3443
schemas:
3544
Pet:
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
openapi: 3.0.0
2+
info:
3+
version: 1.0.0
4+
servers:
5+
- url: http://petstore.swagger.io/v2
6+
paths:
7+
/pet:
8+
post:
9+
x-role: admin
10+
tags:
11+
- pet
12+
operationId: addPet
13+
responses:
14+
'200':
15+
description: successful operation
16+
content:
17+
application/json:
18+
schema:
19+
$ref: '#/components/schemas/Pet'
20+
requestBody:
21+
$ref: '#/components/requestBodies/Pet'
22+
'/pet/{petId}':
23+
get:
24+
operationId: getPetById
25+
parameters:
26+
- name: petId
27+
in: path
28+
description: ID of pet to return
29+
required: true
30+
schema:
31+
type: integer
32+
format: int64
33+
responses:
34+
'200':
35+
description: successful operation
36+
content:
37+
application/json:
38+
schema:
39+
$ref: '#/components/schemas/Pet'
40+
components:
41+
responses:
42+
Order:
43+
description: successful operation
44+
content:
45+
application/json:
46+
schema:
47+
$ref: '#/components/schemas/Order'
48+
requestBodies:
49+
UserArray:
50+
content:
51+
application/json:
52+
schema:
53+
type: array
54+
items:
55+
$ref: '#/components/schemas/User'
56+
description: List of user object
57+
required: true
58+
Pet:
59+
content:
60+
application/json:
61+
schema:
62+
$ref: '#/components/schemas/Pet'
63+
description: Pet object that needs to be added to the store
64+
required: true
65+
Order:
66+
content:
67+
application/json:
68+
schema:
69+
$ref: '#/components/schemas/Order'
70+
description: order placed for purchasing the pet
71+
required: true
72+
schemas:
73+
Order:
74+
title: Pet Order
75+
description: An order for a pets from the pet store
76+
type: object
77+
properties:
78+
id:
79+
type: integer
80+
format: int64
81+
petId:
82+
type: integer
83+
format: int64
84+
quantity:
85+
type: integer
86+
format: int32
87+
shipDate:
88+
type: string
89+
format: date-time
90+
status:
91+
type: string
92+
description: Order Status
93+
enum:
94+
- placed
95+
- approved
96+
- delivered
97+
complete:
98+
type: boolean
99+
default: false
100+
Category:
101+
title: Pet category
102+
description: A category for a pet
103+
type: object
104+
properties:
105+
id:
106+
type: integer
107+
format: int64
108+
name:
109+
type: string
110+
pattern: '^[a-zA-Z0-9]+[a-zA-Z0-9\.\-_]*[a-zA-Z0-9]+$'
111+
User:
112+
title: a User
113+
description: A User who is purchasing from the pet store
114+
type: object
115+
properties:
116+
id:
117+
type: integer
118+
format: int64
119+
username:
120+
type: string
121+
firstName:
122+
type: string
123+
lastName:
124+
type: string
125+
email:
126+
type: string
127+
password:
128+
type: string
129+
phone:
130+
type: string
131+
userStatus:
132+
type: integer
133+
format: int32
134+
description: User Status
135+
Tag:
136+
title: Pet Tag
137+
description: A tag for a pet
138+
type: object
139+
properties:
140+
id:
141+
type: integer
142+
format: int64
143+
name:
144+
type: string
145+
Pet:
146+
title: a Pet
147+
description: A pet for sale in the pet store
148+
type: object
149+
required:
150+
- name
151+
- photoUrls
152+
properties:
153+
id:
154+
type: integer
155+
format: int64
156+
category:
157+
$ref: '#/components/schemas/Category'
158+
name:
159+
type: string
160+
example: doggie
161+
photoUrls:
162+
type: array
163+
items:
164+
type: string
165+
tags:
166+
type: array
167+
items:
168+
$ref: '#/components/schemas/Tag'
169+
status:
170+
type: string
171+
description: pet status in the store
172+
deprecated: true
173+
enum:
174+
- available
175+
- pending
176+
- sold
177+
ApiResponse:
178+
title: An uploaded response
179+
description: Describes the result of uploading an image resource
180+
type: object
181+
properties:
182+
code:
183+
type: integer
184+
format: int32
185+
type:
186+
type: string
187+
message:
188+
type: string
189+
parameters:
190+
orderId:
191+
name: orderId
192+
in: path
193+
required: true
194+
schema:
195+
type: string
196+
offset:
197+
in: query
198+
name: offset
199+
required: false
200+
x-internal: true
201+
deprecated: true
202+
schema:
203+
type: integer
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
inputSpec: src/test/resources/openapi_normalizer/custom_remove_filter.yaml
2+
normalizedSpec: src/test/resources/openapi_normalizer/custom_remove_filter_response.yaml
3+
inputRules:
4+
NORMALIZER_CLASS: org.openapitools.codegen.OpenAPINormalizerTest$CustomRemoveFilter
5+
REMOVE_FILTER: 'unused'

0 commit comments

Comments
 (0)