Skip to content

Commit 9ed5a45

Browse files
committed
fix: wrong transformation of object properties
Signed-off-by: Pascal Krause <[email protected]>
1 parent 18ce801 commit 9ed5a45

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/main/java/io/vertx/openapi/validation/transformer/ParameterTransformer.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.vertx.core.json.JsonObject;
2525
import io.vertx.json.schema.common.dsl.SchemaType;
2626
import io.vertx.openapi.contract.Parameter;
27+
import java.util.function.Supplier;
2728

2829
public abstract class ParameterTransformer {
2930

@@ -118,18 +119,30 @@ public Object transformObject(Parameter parameter, String rawValue) {
118119
}
119120
JsonObject object = new JsonObject();
120121
for (int i = 0; i < keysAndValues.length; i = i + 2) {
121-
object.put(keysAndValues[i], transformPrimitive(parameter.getSchemaType(), keysAndValues[i + 1]));
122+
String propertyName = keysAndValues[i];
123+
SchemaType propertySchema = getObjectPropertySchemaType(parameter, propertyName);
124+
object.put(propertyName, transformPrimitive(propertySchema, keysAndValues[i + 1]));
122125
}
123126
return object;
124127
}
125128

126129
// VisibleForTesting
127-
public SchemaType getArrayItemSchemaType(Parameter parameter) {
128-
String itemsType = parameter.getSchema().get("items", new JsonObject()).getString("type");
129-
if (itemsType == null) {
130+
public SchemaType getArrayItemSchemaType(Parameter arrayParameter) {
131+
return getSchemaType(() -> arrayParameter.getSchema().get("items", new JsonObject()).getString("type"));
132+
}
133+
134+
public SchemaType getObjectPropertySchemaType(Parameter objectParameter, String propertyName) {
135+
String propertyType = objectParameter.getSchema().get("properties", new JsonObject())
136+
.getJsonObject(propertyName, new JsonObject()).getString("type");
137+
return getSchemaType(() -> propertyType);
138+
}
139+
140+
private SchemaType getSchemaType(Supplier<String> getType) {
141+
String type = getType.get();
142+
if (type == null) {
130143
// This allows everything
131144
return SchemaType.OBJECT;
132145
}
133-
return SchemaType.valueOf(itemsType.toUpperCase());
146+
return SchemaType.valueOf(type.toUpperCase());
134147
}
135148
}

src/test/java/io/vertx/tests/validation/transformer/FormTransformerTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
package io.vertx.tests.validation.transformer;
1414

1515
import static com.google.common.truth.Truth.assertThat;
16+
import static io.vertx.json.schema.common.dsl.Schemas.objectSchema;
17+
import static io.vertx.json.schema.common.dsl.Schemas.stringSchema;
1618
import static io.vertx.openapi.contract.Location.COOKIE;
1719
import static io.vertx.openapi.contract.Style.FORM;
1820
import static io.vertx.openapi.impl.Utils.EMPTY_JSON_ARRAY;
@@ -74,14 +76,20 @@ private static Stream<Arguments> provideValidArrayValues() {
7476
private static Stream<Arguments> provideValidObjectValues() {
7577
String complexRaw = "string,foo,number,13.37,integer,42,boolean,true";
7678
String complexExplodedRaw = "string=foo&number=13.37&integer=42&boolean=true";
77-
JsonObject expected =
79+
JsonObject expectedComplex =
7880
new JsonObject().put("string", "foo").put("integer", 42).put("boolean", true).put("number", 13.37);
7981

82+
String stringWithIntValue = "string,42";
83+
JsonObject expectedStringWithIntValue = new JsonObject().put("string", "42");
84+
JsonSchema stringWithIntSchema = JsonSchema.of(objectSchema().property("string", stringSchema()).toJson());
85+
Parameter stringWIthIntParam = mockFormParameter(stringWithIntSchema, false);
86+
8087
return Stream.of(
8188
Arguments.of("empty", OBJECT_PARAM, "", EMPTY_JSON_OBJECT),
8289
Arguments.of("empty (exploded)", OBJECT_PARAM_EXPLODE, "", EMPTY_JSON_OBJECT),
83-
Arguments.of(complexRaw, OBJECT_PARAM, complexRaw, expected),
84-
Arguments.of(complexExplodedRaw + " (exploded)", OBJECT_PARAM_EXPLODE, complexExplodedRaw, expected));
90+
Arguments.of(complexRaw, OBJECT_PARAM, complexRaw, expectedComplex),
91+
Arguments.of(stringWithIntValue, stringWIthIntParam, stringWithIntValue, expectedStringWithIntValue),
92+
Arguments.of(complexExplodedRaw + " (exploded)", OBJECT_PARAM_EXPLODE, complexExplodedRaw, expectedComplex));
8593
}
8694

8795
@ParameterizedTest(name = "{index} Transform \"Cookie\" parameter of style \"from\" with primitive value: {0}")

src/test/java/io/vertx/tests/validation/transformer/ParameterTransformerTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.vertx.tests.validation.transformer;
22

33
import static com.google.common.truth.Truth.assertThat;
4-
import static io.vertx.json.schema.common.dsl.SchemaType.STRING;
4+
import static io.vertx.json.schema.common.dsl.SchemaType.*;
55
import static io.vertx.json.schema.common.dsl.Schemas.arraySchema;
66
import static io.vertx.json.schema.common.dsl.Schemas.booleanSchema;
77
import static io.vertx.json.schema.common.dsl.Schemas.intSchema;
@@ -18,6 +18,7 @@
1818

1919
import io.vertx.core.json.DecodeException;
2020
import io.vertx.json.schema.JsonSchema;
21+
import io.vertx.json.schema.common.dsl.ObjectSchemaBuilder;
2122
import io.vertx.json.schema.common.dsl.SchemaBuilder;
2223
import io.vertx.json.schema.common.dsl.SchemaType;
2324
import io.vertx.openapi.contract.Parameter;
@@ -92,4 +93,19 @@ void testGetArrayItemSchemaType() {
9293
Parameter arrayParam = buildSimplePathParameter(arraySchema().items(stringSchema()));
9394
assertThat(TRANSFORMER.getArrayItemSchemaType(arrayParam)).isEqualTo(STRING);
9495
}
96+
97+
@Test
98+
void testGetObjectPropertySchemaType() {
99+
ObjectSchemaBuilder schema = objectSchema()
100+
.property("string", stringSchema())
101+
.property("number", numberSchema())
102+
.property("integer", intSchema())
103+
.property("boolean", booleanSchema());
104+
Parameter objectParam = buildSimplePathParameter(schema);
105+
106+
assertThat(TRANSFORMER.getObjectPropertySchemaType(objectParam, "string")).isEqualTo(STRING);
107+
assertThat(TRANSFORMER.getObjectPropertySchemaType(objectParam, "number")).isEqualTo(NUMBER);
108+
assertThat(TRANSFORMER.getObjectPropertySchemaType(objectParam, "integer")).isEqualTo(INTEGER);
109+
assertThat(TRANSFORMER.getObjectPropertySchemaType(objectParam, "boolean")).isEqualTo(BOOLEAN);
110+
}
95111
}

0 commit comments

Comments
 (0)