apple / swift-openapi-generator

Generate Swift client and server code from an OpenAPI document.
https://swiftpackageindex.com/apple/swift-openapi-generator/documentation
Apache License 2.0
1.45k stars 120 forks source link

Enforce error diagnostics by aborting execution #607

Closed PARAIPAN9 closed 2 months ago

PARAIPAN9 commented 3 months ago

Motivation

Modifications

Result

Test Plan

PARAIPAN9 commented 3 months ago

I have a question because I'm a bit confused about what to do after the creation of the wrapper. Should we use the wrapper in place of the extensions from DiagnosticCollector?

czechboy0 commented 3 months ago

I think DiagnosticCollector's emit method should become throwing, and then create a new concrete collector implementation that looks something like this:

struct ErrorThrowingDiagnosticCollector: DiagnosticCollector {
    var upstream: any DiagnosticCollector

    func emit(_ diagnostic: Diagnostic) throws {
        upstream.emit(diagnostic)
        if diagnostic.severity == .error { throw diagnostic }
    }
}

And then use this collector by default, wrapping today's collector.

PARAIPAN9 commented 3 months ago

I think DiagnosticCollector's emit method should become throwing, and then create a new concrete collector implementation that looks something like this:

struct ErrorThrowingDiagnosticCollector: DiagnosticCollector {
    var upstream: any DiagnosticCollector

    func emit(_ diagnostic: Diagnostic) throws {
        upstream.emit(diagnostic)
        if diagnostic.severity == .error { throw diagnostic }
    }
}

And then use this collector by default, wrapping today's collector.

It makes sense, perfect!!

czechboy0 commented 3 months ago

@swift-server-bot test this please

czechboy0 commented 3 months ago

Ok the code looks good, would you be able to add one test that constructs the pipeline with a malformed input and ends up throwing the error? Just to verify that it's really working?

PARAIPAN9 commented 3 months ago

Sure.

czechboy0 commented 3 months ago

@swift-server-bot test this please

czechboy0 commented 3 months ago

Got some build failures on Linux:

/code/Tests/OpenAPIGeneratorTests/Test_GenerateOptions.swift:38:34: error: cannot call value of non-function type 'String'
        let arguments = [docPath.path(), "--config", configPath.path]
                                 ^   ~~

/code/Tests/OpenAPIGeneratorTests/Test_GenerateOptions.swift:42:94: error: 'nil' requires a contextual type
            try await generator.runGenerator(outputDirectory: outputDirectory, pluginSource: nil, isDryRun: false)

You can fix the first one by using docPath.path (the property) instead. For the second, just pass the "build plugin" plugin source case.

czechboy0 commented 3 months ago

@swift-server-bot test this please

czechboy0 commented 3 months ago
** Running /code/scripts/check-license-headers.sh...
** ERROR: Unsupported file extension for file (exclude or update this script): Tests/OpenAPIGeneratorTests/Resources/Docs/malformed-openapi.yaml

You'll need to add the file to the license check ignore list.

czechboy0 commented 3 months ago

Hmm this actually breaks 5.9.0, because it creates a dependency of a test target on an executable target. https://ci.swiftserver.group/job/swift-openapi-generator-swift590-prb/246/console

To fix this, you'll need to start depending on _OpenAPIGeneratorCore instead of swift-openapi-generator, and move _YamlFileDiagnosticsCollector to _OpenAPIGeneratorCore, and create a method called e.g. preparedDiagnosticsCollector(...) -> DiagnosticCollector & Sendable that lives in _OpenAPIGeneratorCore. Then just test that method, and call the method from the swift-openapi-generator code.

I don't think we'll be able to make the new dependency work otherwise.

czechboy0 commented 3 months ago

@swift-server-bot test this please

czechboy0 commented 2 months ago

@swift-server-bot test this please

czechboy0 commented 2 months ago

@swift-server-bot test this please

czechboy0 commented 2 months ago

@PARAIPAN9 The CI is happy, but I believe we still need to move the remaining diagnostic collection code into the new prepare... function, so that it's called from the CLI and also from tests. Right now we're still re-creating the collectors using different code in the CLI vs the tests.