From 5ec6dce489b766b15e7275810896e37adf765829 Mon Sep 17 00:00:00 2001 From: Timon Back Date: Fri, 30 Oct 2026 17:53:42 +0100 Subject: [PATCH] feat(core): use openapi 3.1 spec fields --- .../asyncapi/schemas/SwaggerSchemaMapper.java | 20 ++++++++++++++++--- ...faultComponentsServiceIntegrationTest.java | 16 ++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SwaggerSchemaMapper.java b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SwaggerSchemaMapper.java index 2d9a3d318..088ab3319 100644 --- a/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SwaggerSchemaMapper.java +++ b/springwolf-core/src/main/java/io/github/springwolf/core/asyncapi/schemas/SwaggerSchemaMapper.java @@ -211,10 +211,24 @@ private static void assignType(Schema swaggerSchema, SchemaObject.SchemaObjectBu Set types = schemaTypes == null ? new HashSet<>() : new HashSet<>(schemaTypes); String schemaType = swaggerSchema.getType(); + if (schemaType == null) { + if (types.isEmpty()) { + // Leave type unset when the schema is composed and does not declare a concrete type + // (e.g. oneOf/anyOf/allOf without a base type). + return; + } + if (isNullable) { + types.add("null"); + } + builder.type(types); + return; + } + if (!types.contains(schemaType)) { - // required while swagger v2 does not populate types. Leave type unset when the schema is composed - // and does not declare a concrete type (e.g. oneOf/anyOf/allOf without a base type). - if (schemaType != null) { + // required while swagger v2 does not populate types + if (isNullable) { + builder.type(Set.of(schemaType, "null")); + } else { builder.type(Set.of(schemaType)); } return; diff --git a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultComponentsServiceIntegrationTest.java b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultComponentsServiceIntegrationTest.java index 0463d7cd5..850edd90e 100644 --- a/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultComponentsServiceIntegrationTest.java +++ b/springwolf-core/src/test/java/io/github/springwolf/core/asyncapi/components/DefaultComponentsServiceIntegrationTest.java @@ -70,6 +70,21 @@ void getStringSchemas() { .build()); } + @Test + void getOpenApi31NullTypeSchemas() { + io.swagger.v3.oas.models.media.Schema swaggerSchema = new io.swagger.v3.oas.models.media.Schema<>(); + Set types = Set.of(SchemaType.STRING.getValue(), SchemaType.NULL.getValue()); + swaggerSchema.setTypes(types); + SwaggerSchemaMapper mapper = new SwaggerSchemaMapper(configProperties); + + assertThat(mapper.mapSchema(swaggerSchema).getSchema()) + .isEqualTo(SchemaObject.builder().type(types).build()); + + swaggerSchema.setType(SchemaType.STRING.getValue()); + assertThat(mapper.mapSchema(swaggerSchema).getSchema()) + .isEqualTo(SchemaObject.builder().type(types).build()); + } + @Test void getArraySchemas() { componentsService.resolvePayloadSchema(ArrayFoo.class, CONTENT_TYPE_APPLICATION_JSON); @@ -235,7 +250,6 @@ private static class NotSupportedFoo { @Schema( hidden = true, // currently not supported defaultValue = "default", // currently not supported - nullable = true, // currently not supported contains = Void.class, // currently not supported additionalItems = Void.class, // currently not supported additionalProperties = Schema.AdditionalPropertiesValue.TRUE, // currently not supported