Skip to content

Commit

Permalink
Expand schema references during deserialization (compatibility w/3.x)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Sep 29, 2024
1 parent 7778ba8 commit e50854e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public Schema readObject(O node) {
IoLogging.logger.singleJsonObject("Schema");
String name = getName(node);
SchemaImpl schema = new SchemaImpl(name);
schema.setRef(jsonIO().getJsonString(node, PROP_REF));

if (openApiVersion() == OpenApiVersion.V3_1) {
String dialect = jsonIO().getString(node, SchemaConstant.PROP_SCHEMA_DIALECT);
Expand Down Expand Up @@ -158,7 +159,8 @@ private void populateSchemaObject(SchemaImpl schema, O node) {
for (Entry<String, V> entry : jsonIO().properties(node)) {
String name = entry.getKey();
V fieldNode = entry.getValue();
if (!PROPERTIES_DATA_TYPES.containsKey(name) && !name.equals(PROP_TYPE) && !name.equals(PROP_NAME)) {
if (!PROPERTIES_DATA_TYPES.containsKey(name) && !name.equals(PROP_TYPE) && !name.equals(PROP_NAME)
&& !name.equals(PROP_REF)) {
dataMap.put(name, jsonIO().fromJson(fieldNode));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public class SchemaConstant {

public static final String PROP_SCHEMA_DIALECT = "$schema";
public static final String PROP_COMMENT = "$comment";
public static final String PROP_REF = "$ref";
public static final String PROP_REF = ReferenceIO.REF;

public static final String PROP_IF = "if";
public static final String PROP_THEN = "then";
Expand Down Expand Up @@ -136,7 +136,6 @@ public class SchemaConstant {
Map<String, DataType> propertiesDataTypes30 = new HashMap<>();
Map<String, DataType> propertiesDataTypes = new HashMap<>();

propertiesDataTypes30.put(ReferenceIO.REF, type(String.class));
propertiesDataTypes30.put(SchemaConstant.PROP_DISCRIMINATOR, type(Discriminator.class));
propertiesDataTypes30.put(SchemaConstant.PROP_TITLE, type(String.class));
propertiesDataTypes30.put(SchemaConstant.PROP_DEFAULT, type(Object.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.smallrye.openapi.runtime.io.media;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayInputStream;

import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import io.smallrye.openapi.api.SmallRyeOASConfig;
import io.smallrye.openapi.api.SmallRyeOpenAPI;

class SchemaIOTest {

@ParameterizedTest
@ValueSource(strings = { "3.0.3", "3.1.0" })
void testSchemaReferenceExpansion(String oasVersion) {
var builder = SmallRyeOpenAPI.builder()
.withCustomStaticFile(() -> new ByteArrayInputStream((""
+ "{"
+ " \"openapi\": \"" + oasVersion + "\","
+ " \"components\": {"
+ " \"schemas\": {"
+ " \"s1\": { \"$ref\": \"s2\" },"
+ " \"s2\": { \"type\": \"string\" }"
+ " }"
+ " }"
+ "}").getBytes()));

System.setProperty(SmallRyeOASConfig.VERSION, oasVersion);
OpenAPI result;

try {
result = builder.build().model();
} finally {
System.clearProperty(SmallRyeOASConfig.VERSION);
}

assertEquals(oasVersion, result.getOpenapi());
assertEquals("#/components/schemas/s2", result.getComponents().getSchemas().get("s1").getRef());
}

}

0 comments on commit e50854e

Please sign in to comment.