You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Could @Schema(nullable = true) work when outputting OpenAPI 3.1.0? #4555
Due to the removal of nullable: true from schema in 3.1.0, it appears that the nullable = true property on the @Schema annotation no longer works. That isn't obvious in the documentation for the annotation, and it's inconvenient to boot ;-)
I think it will be quite easy to continue to support the nullable attribute under 3.1.0, I suggest a little change to Schema.getTypes:
Ugh, nullable properties that use a $ref aren't that simple... it seems they need to transform into:
allOf:
- $ref: '#/...'
- { type: "null" }
Heh, I'm not immediately a fan of OpenAPI 3.1 :-D
Instead I've solved this with a filter. If you, the maintainers, think that support for Schema.nullable is nice for OpenAPI 3.1 support, I'd be happy to work on a PR, otherwise I'll leave this filter here for anyone else in this predicament to enjoy and please close this issue!
importjava.util.List;
importjava.util.Map;
importjava.util.Optional;
importio.swagger.v3.core.filter.AbstractSpecFilter;
importio.swagger.v3.core.util.AnnotationsUtils;
importio.swagger.v3.oas.models.SpecVersion;
importio.swagger.v3.oas.models.media.Schema;
/** * A filter to add support for {@link Schema#getNullable()} under OpenAPI 3.1.0. As of swagger-core 2.2.19 that property * is ignored when serializing to OpenAPI 3.1.0. This filter modifies property schemas to translate nullable to * OpenAPI 3.1.0. * <p> * OpenAPI 3.1.0 doesn't directly support {@code nullable} anymore; instead we must add {@code null} to the array of types. */publicclassNullableToOpenAPI31FilterextendsAbstractSpecFilter {
@OverridepublicOptional<Schema> filterSchemaProperty(Schemaproperty, Schemaschema, StringpropName,
Map<String, List<String>> params, Map<String, String> cookies, Map<String, List<String>> headers) {
if (property.getNullable() != null && property.getNullable().booleanValue()) {
if (property.get$ref() != null) {
Schemaclone = AnnotationsUtils.clone(property, isOpenAPI31Filter());
clone.$ref(null);
clone.addAnyOfItem(newSchema(property.getSpecVersion()).$ref(property.get$ref()));
clone.addAnyOfItem(newNullSchema(property.getSpecVersion()));
returnOptional.of(clone);
} else {
Schemaclone = AnnotationsUtils.clone(property, isOpenAPI31Filter());
if (!clone.getTypes().contains("null")) {
clone.addType("null");
}
returnOptional.of(clone);
}
} else {
returnsuper.filterSchemaProperty(property, schema, propName, params, cookies, headers);
}
}
@OverridepublicbooleanisOpenAPI31Filter() {
returntrue;
}
publicstaticclassNullSchemaextendsSchema {
publicNullSchema(SpecVersionspecVersion) {
super("null", null, specVersion);
}
}
}
Activity
karlvr commentedon Nov 24, 2023
Ugh,
nullableproperties that use a$refaren't that simple... it seems they need to transform into:Heh, I'm not immediately a fan of OpenAPI 3.1 :-D
Instead I've solved this with a filter. If you, the maintainers, think that support for
Schema.nullableis nice for OpenAPI 3.1 support, I'd be happy to work on a PR, otherwise I'll leave this filter here for anyone else in this predicament to enjoy and please close this issue!zeldigas commentedon Jun 13, 2024
@karlvr how do you apply this filter?
karlvr commentedon Jul 9, 2024
@zeldigas sorry for the slow reply, like this in my class that extends
javax.ws.rs.core.Application:squinker commentedon Aug 26, 2025
Does the Schema annotation have the capability to express this yet?
5001: Add support for @nullable annotations in OpenAPI 3.1 schemas (#…
ewaostrowska commentedon Feb 20, 2026
Hi! This issues should be resolved with #5018 and it will be part of the next release :)