Skip to content

Commit 875b2ca

Browse files
vgalloyfrantuma
authored andcommitted
Json deserialization process should not remove extension for SecurityScheme object
Motivation According to the specification the "Security Scheme Object" may be extended with Specification Extensions. Modification * Add test to highlight the bug * SecuritySchemeDeserializer now parse other field
1 parent 9e2593e commit 875b2ca

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

modules/swagger-core/src/main/java/io/swagger/v3/core/util/SecuritySchemeDeserializer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import java.io.IOException;
1313
import java.util.Arrays;
14+
import java.util.Iterator;
15+
import java.util.List;
1416

1517
public class SecuritySchemeDeserializer extends JsonDeserializer<SecurityScheme> {
1618

@@ -62,6 +64,15 @@ public SecurityScheme deserialize(JsonParser jp, DeserializationContext ctxt)
6264
result
6365
.type(SecurityScheme.Type.MUTUALTLS);
6466
}
67+
final Iterator<String> fieldNames = node.fieldNames();
68+
while(fieldNames.hasNext()) {
69+
final String fieldName = fieldNames.next();
70+
if(fieldName.startsWith("x-")) {
71+
final JsonNode fieldValue = node.get(fieldName);
72+
final Object value = Json.mapper().treeToValue(fieldValue, Object.class);
73+
result.addExtension(fieldName, value);
74+
}
75+
}
6576
}
6677

6778
return result;

modules/swagger-core/src/test/java/io/swagger/v3/core/serialization/SecurityDefinitionTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
import io.swagger.v3.oas.models.security.SecurityRequirement;
2525
import io.swagger.v3.oas.models.security.SecurityScheme;
2626
import io.swagger.v3.oas.models.servers.Server;
27+
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
import org.testng.Assert;
31+
import static org.testng.Assert.assertEquals;
32+
import static org.testng.Assert.assertNotNull;
2733
import org.testng.annotations.Test;
2834

2935
import java.io.IOException;
@@ -104,4 +110,14 @@ public void createModelWithSecurityRequirements() throws IOException {
104110
final String json = ResourceUtils.loadClassResource(getClass(), "ModelWithSecurityRequirements.json");
105111
SerializationMatchers.assertEqualsToJson(oas, json);
106112
}
113+
114+
@Test(description = "Security Scheme deserialization should not remove extensions")
115+
public void doNotRemoveExtensions() {
116+
final SecurityScheme securityScheme = TestUtils.deserializeJsonFileFromClasspath("specFiles/securitySchemaWithExtension.json", SecurityScheme.class);
117+
118+
final Map<String, Object> extensions = securityScheme.getExtensions();
119+
final Map<String, Object> expected = Collections.singletonMap("x-custom", Collections.singletonMap("key-string", "value-one"));
120+
121+
assertEquals(extensions, expected);
122+
}
107123
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"type": "oauth2",
3+
"flows": {
4+
"implicit": {
5+
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
6+
"scopes": {
7+
"write:pets": "modify pets in your account",
8+
"read:pets": "read your pets"
9+
}
10+
}
11+
},
12+
"x-custom": {
13+
"key-string": "value-one"
14+
}
15+
}

0 commit comments

Comments
 (0)