Skip to content

Commit 2c52179

Browse files
authored
Add support for .yml extension (openapi.yml and openapi-generator-config.yml) (#77)
### Motivation Some openapi file use `.yml` as the yaml file extension, we should support this instead of forcing user to rename it. > https://github.com/discourse/discourse_api_docs/blob/main/openapi.yml ### Modifications Add yml file extension support ### Result - Users can provide `openapi.yml` instead of `openapi.yaml` - Users can provide `openapi-generator-config.yml` instead of `openapi-generator-config.yml`
1 parent cfe0780 commit 2c52179

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

Plugins/OpenAPIGenerator/plugin.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct SwiftOpenAPIGeneratorPlugin {
2020
case incompatibleTarget(targetName: String)
2121
case noConfigFound(targetName: String)
2222
case noDocumentFound(targetName: String)
23+
case multiConfigFound(targetName: String, files: [Path])
24+
case multiDocumentFound(targetName: String, files: [Path])
2325

2426
var description: String {
2527
switch self {
@@ -28,10 +30,16 @@ struct SwiftOpenAPIGeneratorPlugin {
2830
"Incompatible target called '\(targetName)'. Only Swift source targets can be used with the Swift OpenAPI generator plugin."
2931
case .noConfigFound(let targetName):
3032
return
31-
"No config found in the target named '\(targetName)'. Add a file called 'openapi-generator-config.yaml' to the target's source directory. See documentation for details."
33+
"No config file found in the target named '\(targetName)'. Add a file called 'openapi-generator-config.yaml' or 'openapi-generator-config.yml' to the target's source directory. See documentation for details."
3234
case .noDocumentFound(let targetName):
3335
return
34-
"No OpenAPI document found in the target named '\(targetName)'. Add a file called 'openapi.yaml' or 'openapi.json' (can also be a symlink) to the target's source directory. See documentation for details."
36+
"No OpenAPI document found in the target named '\(targetName)'. Add a file called 'openapi.yaml', 'openapi.yml' or 'openapi.json' (can also be a symlink) to the target's source directory. See documentation for details."
37+
case .multiConfigFound(let targetName, let files):
38+
return
39+
"Multiple config files found in the target named '\(targetName)', but exactly one is required. Found \(files.map(\.description).joined(separator: " "))."
40+
case .multiDocumentFound(let targetName, let files):
41+
return
42+
"Multiple OpenAPI documents found in the target named '\(targetName)', but exactly one is required. Found \(files.map(\.description).joined(separator: " "))."
3543
}
3644
}
3745

@@ -40,30 +48,33 @@ struct SwiftOpenAPIGeneratorPlugin {
4048
}
4149
}
4250

51+
private var supportedConfigFiles: Set<String> { Set(["yaml", "yml"].map { "openapi-generator-config." + $0 }) }
52+
private var supportedDocFiles: Set<String> { Set(["yaml", "yml", "json"].map { "openapi." + $0 }) }
53+
4354
func createBuildCommands(
4455
pluginWorkDirectory: PackagePlugin.Path,
4556
tool: (String) throws -> PackagePlugin.PluginContext.Tool,
4657
sourceFiles: FileList,
4758
targetName: String
4859
) throws -> [Command] {
4960
let inputFiles = sourceFiles
50-
guard let config = inputFiles.first(where: { $0.path.lastComponent == "openapi-generator-config.yaml" })?.path
51-
else {
61+
let matchedConfigs = inputFiles.filter { supportedConfigFiles.contains($0.path.lastComponent) }.map(\.path)
62+
guard matchedConfigs.count > 0 else {
5263
throw Error.noConfigFound(targetName: targetName)
5364
}
54-
guard
55-
let doc = inputFiles.first(where: {
56-
switch $0.path.lastComponent {
57-
case "openapi.yaml", "openapi.json":
58-
return true
59-
default:
60-
return false
61-
}
62-
})?
63-
.path
64-
else {
65+
guard matchedConfigs.count == 1 else {
66+
throw Error.multiConfigFound(targetName: targetName, files: matchedConfigs)
67+
}
68+
let config = matchedConfigs[0]
69+
70+
let matchedDocs = inputFiles.filter { supportedDocFiles.contains($0.path.lastComponent) }.map(\.path)
71+
guard matchedDocs.count > 0 else {
6572
throw Error.noDocumentFound(targetName: targetName)
6673
}
74+
guard matchedDocs.count == 1 else {
75+
throw Error.multiDocumentFound(targetName: targetName, files: matchedDocs)
76+
}
77+
let doc = matchedDocs[0]
6778
let genSourcesDir = pluginWorkDirectory.appending("GeneratedSources")
6879
let outputFiles: [Path] = GeneratorMode.allCases.map { genSourcesDir.appending($0.outputFileName) }
6980
return [

Sources/swift-openapi-generator/Documentation.docc/Articles/Configuring-the-generator.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ The command-line tool also uses the same configuration file.
1010

1111
### Create a configuration file
1212

13-
The configuration file is named `openapi-generator-config.yaml` and must exist in the target source directory.
13+
The configuration file is named `openapi-generator-config.yaml` or `openapi-generator-config.yml` and must exist in the target source directory.
14+
15+
> In the following tutorial, we will use `openapi-generator-config.yaml` as an example.
1416
1517
```
1618
.

scripts/check-license-headers.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ read -ra PATHS_TO_CHECK_FOR_LICENSE <<< "$( \
6262
":(exclude)**/Package.resolved" \
6363
":(exclude)**/README.md" \
6464
":(exclude)**/openapi.yaml" \
65+
":(exclude)**/openapi.yml" \
6566
":(exclude)**/petstore.yaml" \
6667
":(exclude)**/openapi-generator-config.yaml" \
68+
":(exclude)**/openapi-generator-config.yml" \
6769
| xargs -0 \
6870
)"
6971

scripts/run-swift-format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if [ "${SWIFT_FORMAT_RC}" -ne 0 ]; then
3737
3838
To fix, run the following command:
3939
40-
% swift-format format --parallel --recursive --in-place Examples IntegrationTests Plugins Sources Tests
40+
% swift-format format --parallel --recursive --in-place Examples IntegrationTest Plugins Sources Tests
4141
"
4242
exit "${SWIFT_FORMAT_RC}"
4343
fi

0 commit comments

Comments
 (0)