-
Notifications
You must be signed in to change notification settings - Fork 312
Extract RestEasy json body response schemas #9015
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
Merged
jandro996
merged 6 commits into
master
from
alejandro.gonzalez/api-sec-resteasy-response-schema
Jul 3, 2025
+163
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
84a6aa5
WIP
jandro996 eb974d1
Merge branch 'master' into alejandro.gonzalez/api-sec-resteasy-respon…
jandro996 9e0f4df
PR reviews
jandro996 617fbf0
Merge branch 'master' into alejandro.gonzalez/api-sec-resteasy-respon…
jandro996 37668f8
Merge branch 'master' into alejandro.gonzalez/api-sec-resteasy-respon…
AlexeyKuznetsov-DD a511ca0
fix codenarc
jandro996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
dd-smoke-tests/resteasy/src/main/java/smoketest/resteasy/App.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
dd-smoke-tests/resteasy/src/main/java/smoketest/resteasy/RequestBody.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package smoketest.resteasy; | ||
|
||
import java.util.List; | ||
|
||
public class RequestBody { | ||
private List<KeyValue> main; | ||
private Object nullable; | ||
|
||
public List<KeyValue> getMain() { | ||
return main; | ||
} | ||
|
||
public void setMain(List<KeyValue> main) { | ||
this.main = main; | ||
} | ||
|
||
public Object getNullable() { | ||
return nullable; | ||
} | ||
|
||
public void setNullable(Object nullable) { | ||
this.nullable = nullable; | ||
} | ||
|
||
public static class KeyValue { | ||
private String key; | ||
private Double value; | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public Double getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(Double value) { | ||
this.value = value; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
dd-smoke-tests/resteasy/src/test/groovy/smoketest/ResteasyAppsecSmokeTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package smoketest | ||
|
||
import datadog.smoketest.appsec.AbstractAppSecServerSmokeTest | ||
import datadog.trace.api.Platform | ||
import groovy.json.JsonOutput | ||
import groovy.json.JsonSlurper | ||
import okhttp3.MediaType | ||
import okhttp3.Request | ||
import okhttp3.RequestBody | ||
import okhttp3.Response | ||
|
||
import java.util.zip.GZIPInputStream | ||
|
||
|
||
class ResteasyAppsecSmokeTest extends AbstractAppSecServerSmokeTest { | ||
|
||
@Override | ||
ProcessBuilder createProcessBuilder() { | ||
String jarPath = System.getProperty("datadog.smoketest.resteasy.jar.path") | ||
|
||
List<String> command = new ArrayList<>() | ||
command.add(javaPath()) | ||
command.addAll(defaultJavaProperties) | ||
command.addAll(defaultAppSecProperties) | ||
if (Platform.isJavaVersionAtLeast(17)) { | ||
command.addAll(["--add-opens", "java.base/java.lang=ALL-UNNAMED"]) | ||
} | ||
command.addAll(["-jar", jarPath, Integer.toString(httpPort)]) | ||
ProcessBuilder processBuilder = new ProcessBuilder(command) | ||
processBuilder.directory(new File(buildDirectory)) | ||
} | ||
|
||
void 'API Security samples only one request per endpoint'() { | ||
given: | ||
def url = "http://localhost:${httpPort}/hello/api_security/sampling/200?test=value" | ||
def request = new Request.Builder() | ||
.url(url) | ||
.addHeader('X-My-Header', "value") | ||
.get() | ||
.build() | ||
|
||
when: | ||
List<Response> responses = (1..3).collect { | ||
client.newCall(request).execute() | ||
} | ||
|
||
then: | ||
responses.each { | ||
assert it.code() == 200 | ||
} | ||
waitForTraceCount(3) | ||
def spans = rootSpans.toList().toSorted { it.span.duration } | ||
spans.size() == 3 | ||
def sampledSpans = spans.findAll { | ||
it.meta.keySet().any { | ||
it.startsWith('_dd.appsec.s.req.') | ||
} | ||
} | ||
sampledSpans.size() == 1 | ||
def span = sampledSpans[0] | ||
span.meta.containsKey('_dd.appsec.s.req.query') | ||
span.meta.containsKey('_dd.appsec.s.req.params') | ||
span.meta.containsKey('_dd.appsec.s.req.headers') | ||
} | ||
|
||
|
||
void 'test response schema extraction'() { | ||
given: | ||
def url = "http://localhost:${httpPort}/hello/api_security/response" | ||
def body = [ | ||
"main" : [["key": "id001", "value": 1345.67], ["value": 1567.89, "key": "id002"]], | ||
"nullable": null, | ||
] | ||
def request = new Request.Builder() | ||
.url(url) | ||
.post(RequestBody.create(MediaType.get('application/json'), JsonOutput.toJson(body))) | ||
.build() | ||
|
||
when: | ||
final response = client.newCall(request).execute() | ||
waitForTraceCount(1) | ||
|
||
then: | ||
response.code() == 200 | ||
def span = rootSpans.first() | ||
span.meta.containsKey('_dd.appsec.s.res.headers') | ||
span.meta.containsKey('_dd.appsec.s.res.body') | ||
final schema = new JsonSlurper().parse(unzip(span.meta.get('_dd.appsec.s.res.body'))) | ||
assert schema == [["main": [[[["key": [8], "value": [16]]]], ["len": 2]], "nullable": [1]]] | ||
} | ||
|
||
private static byte[] unzip(final String text) { | ||
final inflaterStream = new GZIPInputStream(new ByteArrayInputStream(text.decodeBase64())) | ||
return inflaterStream.getBytes() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.