OpenAPISchema.nullable constructor

OpenAPISchema.nullable(
  1. OpenAPISchema schema
)

Makes a schema nullable using OpenAPI 3.1 type: [T, 'null'].

Implementation

factory OpenAPISchema.nullable(OpenAPISchema schema) {
  if (schema case final bool value) {
    return value ? OpenAPISchema.anything() : OpenAPISchema.null_();
  }

  final json = schema as Map<String, Object?>;
  final type = json['type'];
  // Schemas without an explicit type ($ref, oneOf, anyOf, allOf, …) must not
  // have a sibling `type` array injected — that produces invalid OpenAPI 3.1.
  // Use anyOf to add null as an alternative instead.
  if (type == null) {
    return OpenAPISchema._({
      'anyOf': [schema, OpenAPISchema.null_()],
    });
  }
  final nullableType = switch (type) {
    final List<Object?> values =>
      values.contains('null') ? values : [...values, 'null'],
    final String value => [value, 'null'],
    _ => [type, 'null'],
  };

  return OpenAPISchema._({...json, 'type': nullableType});
}