Skip to content

Commit 829448b

Browse files
Jonas Kellererfengelniederhammer
Jonas Kellerer
authored andcommitted
feat: add description to response schema
1 parent d183a0f commit 829448b

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

lapis2/src/main/kotlin/org/genspectrum/lapis/OpenApiDocs.kt

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,55 @@ import org.genspectrum.lapis.controller.REQUEST_SCHEMA_WITH_MIN_PROPORTION
1313
import org.genspectrum.lapis.controller.RESPONSE_SCHEMA_AGGREGATED
1414

1515
fun buildOpenApiSchema(sequenceFilterFields: SequenceFilterFields, databaseConfig: DatabaseConfig): OpenAPI {
16-
var properties = sequenceFilterFields.fields
16+
val filterProperties = sequenceFilterFields.fields
1717
.map { (fieldName, fieldType) -> fieldName to Schema<String>().type(fieldType.openApiType) }
1818
.toMap()
1919

20-
if (databaseConfig.schema.opennessLevel == OpennessLevel.PROTECTED) {
21-
properties = properties + ("accessKey" to accessKeySchema)
20+
val requestProperties = when (databaseConfig.schema.opennessLevel) {
21+
OpennessLevel.PROTECTED -> filterProperties + ("accessKey" to accessKeySchema)
22+
else -> filterProperties
2223
}
2324

25+
val responseProperties = filterProperties + mapOf(
26+
"count" to Schema<String>().type("number"),
27+
)
28+
2429
return OpenAPI()
2530
.components(
2631
Components().addSchemas(
2732
REQUEST_SCHEMA,
2833
Schema<String>()
2934
.type("object")
3035
.description("valid filters for sequence data")
31-
.properties(properties),
36+
.properties(requestProperties),
3237
).addSchemas(
3338
REQUEST_SCHEMA_WITH_MIN_PROPORTION,
3439
Schema<String>()
3540
.type("object")
3641
.description("valid filters for sequence data")
37-
.properties(properties + Pair(MIN_PROPORTION_PROPERTY, Schema<String>().type("number"))),
42+
.properties(requestProperties + Pair(MIN_PROPORTION_PROPERTY, Schema<String>().type("number"))),
3843
).addSchemas(
3944
REQUEST_SCHEMA_WITH_GROUP_BY_FIELDS,
4045
Schema<String>()
4146
.type("object")
4247
.description("valid filters for sequence data")
4348
.properties(
44-
properties + Pair(
49+
requestProperties + Pair(
4550
"fields",
46-
Schema<String>().type("array").items(Schema<String>().type("string")),
51+
fieldsSchema,
4752
),
4853
),
4954
).addSchemas(
5055
RESPONSE_SCHEMA_AGGREGATED,
5156
Schema<String>()
5257
.type("object")
53-
.description("aggregated sequence data")
58+
.description(
59+
"Aggregated sequence data. " +
60+
"If fields are specified, then these fields are also keys in the result. " +
61+
"The key 'count' is always present.",
62+
)
5463
.required(listOf("count"))
55-
.properties(
56-
properties +
57-
mapOf(
58-
"count" to Schema<String>().type("number"),
59-
),
60-
),
64+
.properties(responseProperties),
6165
),
6266
)
6367
}
@@ -69,3 +73,11 @@ private val accessKeySchema = Schema<String>()
6973
"There are two types or access keys: One only grants access to aggregated data, " +
7074
"the other also grants access to detailed data.",
7175
)
76+
77+
private val fieldsSchema = Schema<String>()
78+
.type("array")
79+
.items(Schema<String>().type("string"))
80+
.description(
81+
"The fields to stratify the result by. " +
82+
"The response will contain the fields specified here with their respective values.",
83+
)

siloLapisTests/test/aggregated.spec.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ describe('The /aggregated endpoint', () => {
2222
});
2323

2424
const resultWithoutUndefined = result.map((aggregatedResponse: AggregatedResponse) => {
25-
const responseWithoutUndefined: Partial<AggregatedResponse> = {};
26-
for (const [key, value] of Object.entries(aggregatedResponse)) {
27-
if (value !== undefined) {
28-
// @ts-ignore
29-
responseWithoutUndefined[key] = value;
30-
}
31-
}
32-
return responseWithoutUndefined;
25+
return Object.entries(aggregatedResponse)
26+
.filter(([, value]) => value !== undefined)
27+
.reduce(
28+
(accumulatedObject, [key, value]) => ({
29+
[key]: value,
30+
...accumulatedObject,
31+
}),
32+
{} as AggregatedResponse
33+
);
3334
});
3435

3536
expect(resultWithoutUndefined).to.have.deep.members(testCase.expected);

0 commit comments

Comments
 (0)