swift-openapi-generator icon indicating copy to clipboard operation
swift-openapi-generator copied to clipboard

Emit yaml processing errors that Xcode can display (#65)

Open macblazer opened this issue 1 year ago • 1 comments

Motivation

If there are any errors in the openapi.yaml file currently the errors show up in the build transcript but are not shown in the Xcode issue sidebar and are also not linked to the openapi.yaml file in the sidebar. This makes it hard to know what has failed, and what to do to fix the file.

Modifications

The YAML parser now catches decoding errors, and if they are common parsing or scanning errors it will emit a diagnostic message then throw a failure exit code. This required importing the swift-argument-parser module into the main _OpenAPIGeneratorCore.

Added absoluteFilePath and lineNumber optional properties to the Diagnostics struct to capture more info. Modified the Diagnostics.description method to build a properly formatted message that Xcode can parse to point out the file and line causing issues.

Result

In consuming projects, invalid yaml in the openapi.yaml file is now highlighted in the Xcode Issue Navigator after a build with this plugin.

Test Plan

Added unit tests to verify emitted diagnostic and thrown error. Also tested manually with an example project.

Resolves

  • Resolves #65.

macblazer avatar Jun 20 '23 05:06 macblazer

I'm not particularly happy with adding ArgumentParser as a dependency into the _OpenAPIGeneratorCore.

Without it, any thrown error is caught by ArgumentParser and the thrown error's localizedDescription is used with a prefix of "Error: " which completely blows away any other diagnostic that I was able to emit.

The code now throws an ArgumentParser.ExitCode.failure after emitting a diagnostic with the proper information for Xcode to pick up the filepath and line number. Using ArgumentParser.ExitCode was the only way I found to have the correct diagnostic message.

macblazer avatar Jun 20 '23 05:06 macblazer

@swift-server-bot add to allowlist

czechboy0 avatar Jun 22 '23 08:06 czechboy0