Skip to content

Add protocol-level @available annotation inheritance to generated mocks#315

Merged
sidepelican merged 8 commits into
uber:masterfrom
Keitaro0226:fix-available-annotation-handling
May 1, 2026
Merged

Add protocol-level @available annotation inheritance to generated mocks#315
sidepelican merged 8 commits into
uber:masterfrom
Keitaro0226:fix-available-annotation-handling

Conversation

@Keitaro0226
Copy link
Copy Markdown
Contributor

Overview

Fixes #314

This draft PR addresses an issue where @available annotations on protocols are not inherited by the generated mock classes. This can cause compilation errors when the protocol uses types or members that are only available in specific OS versions.

Problem

Currently, when generating mocks for protocols with @available annotations, Mockolo does not apply these availability constraints to the generated mock. As a result, using the mock can trigger compile-time errors when availability-constrained types or members are involved.

Example

/// @mockable
@available(iOS 18.0, *)
protocol Foo: Sendable {
    func bar()
}

Incorrect mock (missing @available):

public final class FooMock: Foo, @unchecked Sendable { 
    // ❌ Missing @available
}

Correct mock:

@available(iOS 18.0, *)
public final class FooMock: Foo, @unchecked Sendable { 
}

Solution

  • Extract protocol-level @available attributes and apply them to generated mocks.

Benefits

  • Prevents availability-related compilation errors.
  • Aligns mock generation with Swift’s availability system.

Open to feedback!

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jul 10, 2025

CLA assistant check
All committers have signed the CLA.

@sidepelican
Copy link
Copy Markdown
Collaborator

Any updates on this? When this draft PR becomes ready?

@Keitaro0226
Copy link
Copy Markdown
Contributor Author

Sorry for the long silence. I got sidetracked and lost track of this.

I'll add tests and mark this PR as ready soon. Thanks for your patience.

@Keitaro0226 Keitaro0226 changed the title [Draft] Add protocol-level @available annotation inheritance to generated mocks Add protocol-level @available annotation inheritance to generated mocks Mar 26, 2026
@Keitaro0226 Keitaro0226 marked this pull request as ready for review March 26, 2026 15:39
Copy link
Copy Markdown
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Overall it looks good, but I noticed a few minor details that I'd like you to address before we merge.

Comment on lines +213 to +217
/// @mockable
@available(iOS 18.0, *)
public protocol Foo: Sendable {
func bar() -> String
}
Copy link
Copy Markdown
Collaborator

@sidepelican sidepelican Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to enforce a compilation error when @available isn't applied to a mock.

Suggested change
/// @mockable
@available(iOS 18.0, *)
public protocol Foo: Sendable {
func bar() -> String
}
@available(macOS 99.0, *)
struct Bar {}
/// @mockable
@available(macOS 99.0, *)
protocol Foo: Sendable {
func bar() -> Bar
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 2fa6c3d.
I updated the fixture to use an availability-gated return type (Bar) so the generated mock must inherit @available to compile.


func model() -> Model {
let metadata = entity.metadata
// Combine protocol-level attributes with member-level attributes
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Combine protocol-level attributes with member-level attributes

The meaning feels a bit off and it’s redundant, so let's remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 3e51751.
Removed the redundant comment.

Comment thread Tests/TestActor/FixtureActor.swift Outdated
}

@MainActor
/// @mockable
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange for @mockable to come along too.
How about just inheriting available?
You can change EntityNode.attributesDescription requirement to var attributes: [String] { get }.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 7f3a38b.
I changed declaration attribute handling to keep syntax attributes as separate entries, so @mockable trivia no longer gets carried along with @available.

return ""
}
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need a test case for protocol inheritance scenarios:

@available(macOS 100.0, *)
struct Bar {}

@available(macOS 90.0, *)
protocol Foo {
}

/// @mockable
@available(macOS 100.0, *)
protocol Foo2: Foo {
  var bar: Bar { get set }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 32dffb1.

@Keitaro0226
Copy link
Copy Markdown
Contributor Author

Thank you for the review.
I’ve addressed the feedback and pushed the updates. I’d appreciate it if you could take another look when you have a chance.
Thank you.

@Keitaro0226 Keitaro0226 requested a review from sidepelican April 18, 2026 07:59
Copy link
Copy Markdown
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late review.
It looks good. Thank you for your contribution!

@sidepelican sidepelican merged commit 59cbddf into uber:master May 1, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generated mocks do not inherit protocol-level @available annotations

3 participants