OpenAPISwiftCodeGenerate is an extension of ServiceModelSwiftCodeGenerate that can be used to generate code based on OpenAPI/Swagger service models. This library can be integrated into higher level code generation applications.
OpenAPISwiftCodeGenerate uses the Swift Package Manager. To use the framework, add the following dependency
to your Package.swift
:
dependencies: [
.package(url: "https://github.com/amzn/openapi-swift-code-generate.git", .upToNextMajor(from: "0.1.0"))
]
OpenAPIServiceModel
and SwaggerServiceModel
both implement the ServiceModel
protocol so they can be passed into the ServiceModelGenerate.generateFromModel
function defined in ServiceModelSwiftCodeGenerate.
For example, given the following code generator:
public struct MyCodeGeneration {
static let asyncResultType = AsyncResultType(typeName: "HTTPResult",
libraryImport: "SmokeHTTPClient")
public static func generateFromModel<ModelType: ServiceModel>(
modelFilePath: String,
modelType: ModelType.Type,
customizations: CodeGenerationCustomizations,
applicationDescription: ApplicationDescription,
modelOverride: ModelOverride?,
...) throws {
func generatorFunction(codeGenerator: ServiceModelCodeGenerator,
serviceModel: ModelType) throws {
try codeGenerator.generateFromModel(serviceModel: serviceModel, ...)
}
try ServiceModelGenerate.generateFromModel(
modelFilePath: modelFilePath,
customizations: customizations,
applicationDescription: applicationDescription,
modelOverride: modelOverride,
generatorFunction: generatorFunction)
}
}
OpenAPI and Swagger files can be parsed as follows:
let modelFilePath = ...
let customizations = ...
let applicationDescription = ...
let modelOverride = ...
// For OpenAPI files
try MyCodeGeneration.generateFromModel(
modelFilePath: modelFilePath,
modelType: OpenAPIServiceModel.Type, // Defined in OpenAPIServiceModel target
customizations: customizations,
applicationDescription: applicationDescription,
modelOverride: modelOverride
)
// For Swagger files
try MyCodeGeneration.generateFromModel(
modelFilePath: modelFilePath,
modelType: SwaggerServiceModel.Type, // Defined in OpenAPIServiceModel target
customizations: customizations,
applicationDescription: applicationDescription,
modelOverride: modelOverride
)
See CONTRIBUTING for more information.
This project is licensed under the Apache-2.0 License.