├── .editorconfig ├── .github ├── ISSUE_TEMPLATE │ ├── bug-report.yml │ ├── config.yml │ ├── feature-request.yml │ ├── support-request.yml │ └── task.yml ├── PULL_REQUEST_TEMPLATE.md ├── release.yml └── workflows │ ├── main.yml │ ├── pull_request.yml │ └── pull_request_label.yml ├── .gitignore ├── .licenseignore ├── .spi.yml ├── .swift-format ├── .swiftformatignore ├── .unacceptablelanguageignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── CONTRIBUTORS.txt ├── Examples ├── .gitignore ├── HelloWorldiOSClientAppExample │ ├── HelloWorldiOSClientApp.xcodeproj │ │ ├── project.pbxproj │ │ ├── project.xcworkspace │ │ │ ├── contents.xcworkspacedata │ │ │ └── xcshareddata │ │ │ │ └── IDEWorkspaceChecks.plist │ │ └── xcshareddata │ │ │ └── xcschemes │ │ │ └── HelloWorldiOSClientApp.xcscheme │ ├── HelloWorldiOSClientApp.xctestplan │ ├── HelloWorldiOSClientApp │ │ ├── Assets.xcassets │ │ │ ├── AccentColor.colorset │ │ │ │ └── Contents.json │ │ │ ├── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ │ └── Contents.json │ │ ├── ContentView.swift │ │ ├── HelloWorldiOSClientApp.swift │ │ ├── Preview Content │ │ │ └── Preview Assets.xcassets │ │ │ │ └── Contents.json │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml │ ├── HelloWorldiOSClientAppTests │ │ └── HelloWorldiOSClientAppTests.swift │ ├── HelloWorldiOSClientAppUITests │ │ └── HelloWorldiOSClientAppUITests.swift │ └── README.md ├── README.md ├── auth-client-middleware-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ ├── AuthenticationClientMiddleware │ │ └── AuthenticationClientMiddleware.swift │ │ └── HelloWorldURLSessionClient │ │ ├── HelloWorldURLSessionClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── auth-server-middleware-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ ├── AuthenticationServerMiddleware │ │ └── AuthenticationServerMiddleware.swift │ │ └── HelloWorldVaporServer │ │ ├── HelloWorldVaporServer.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── bidirectional-event-streams-client-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── BidirectionalEventStreamsClient │ │ ├── BidirectionalEventStreamsClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── bidirectional-event-streams-server-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── BidirectionalEventStreamsServer │ │ ├── BidirectionalEventStreamsServer.swift │ │ ├── GreetingStream.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── command-line-client-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── CommandLineClient │ │ ├── CommandLineClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── curated-client-library-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ └── CuratedLibraryClient │ │ │ ├── CuratedLibraryClient.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ └── Tests │ │ └── CuratedLibraryClientTests │ │ └── CuratedLibraryClientTests.swift ├── event-streams-client-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── EventStreamsClient │ │ ├── EventStreamsClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── event-streams-server-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── EventStreamsServer │ │ ├── EventStreamsServer.swift │ │ ├── GreetingStream.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── hello-world-async-http-client-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── HelloWorldAsyncHTTPClient │ │ ├── HelloWorldAsyncHTTPClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── hello-world-hummingbird-server-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ └── HelloWorldHummingbirdServer │ │ │ ├── HelloWorldHummingbirdServer.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ └── Tests │ │ └── HelloWorldHummingbirdServerTests │ │ └── HelloWorldHummingbirdServerTests.swift ├── hello-world-urlsession-client-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── HelloWorldURLSessionClient │ │ ├── HelloWorldURLSessionClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── hello-world-vapor-server-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ └── HelloWorldVaporServer │ │ │ ├── HelloWorldVaporServer.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ └── Tests │ │ └── HelloWorldVaporServerTests │ │ └── HelloWorldVaporServerTests.swift ├── logging-middleware-oslog-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ ├── HelloWorldURLSessionClient │ │ │ ├── HelloWorldURLSessionClient.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ │ └── LoggingMiddleware │ │ │ └── LoggingMiddleware.swift │ └── Tests │ │ └── LoggingMiddlewareTests │ │ └── LoggingMiddlewareTests.swift ├── logging-middleware-swift-log-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ ├── HelloWorldURLSessionClient │ │ │ ├── HelloWorldURLSessionClient.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ │ └── LoggingMiddleware │ │ │ └── LoggingMiddleware.swift │ └── Tests │ │ └── LoggingMiddlewareTests │ │ └── LoggingMiddlewareTests.swift ├── manual-generation-generator-cli-example │ ├── .gitignore │ ├── Makefile │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ └── ManualGeneratorInvocationClient │ │ │ ├── Generated │ │ │ ├── Client.swift │ │ │ └── Types.swift │ │ │ └── ManualGeneratorInvocationClient.swift │ ├── openapi-generator-config.yaml │ └── openapi.yaml ├── manual-generation-package-plugin-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── CommandPluginInvocationClient │ │ ├── CommandPluginInvocationClient.swift │ │ ├── GeneratedSources │ │ ├── Client.swift │ │ └── Types.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── metrics-middleware-example │ ├── .dockerignore │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ ├── HelloWorldVaporServer │ │ │ ├── HelloWorldVaporServer.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ │ └── MetricsMiddleware │ │ │ └── MetricsMiddleware.swift │ └── docker │ │ ├── Dockerfile │ │ ├── docker-compose.yaml │ │ └── prometheus.yml ├── postgres-database-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ └── PostgresDatabaseServer │ │ │ ├── PostgresDatabaseServer.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ └── docker-compose.yaml ├── retrying-middleware-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ ├── HelloWorldURLSessionClient │ │ ├── HelloWorldURLSessionClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml │ │ └── RetryingClientMiddleware │ │ └── RetryingClientMiddleware.swift ├── shared-types-client-server-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ ├── Client │ │ ├── HelloWorldURLSessionClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml │ │ ├── Server │ │ ├── HelloWorldHummingbirdServer.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml │ │ ├── Types │ │ ├── Extensions.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml │ │ └── openapi.yaml ├── streaming-chatgpt-proxy │ ├── .devcontainer │ │ └── devcontainer.json │ ├── .env.example │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ ├── ChatGPT │ │ │ ├── Middleware.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ │ ├── ClientCLI │ │ │ ├── main.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ │ └── ProxyServer │ │ │ ├── main.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ └── players.txt ├── swagger-ui-endpoint-example │ ├── .gitignore │ ├── Package.swift │ ├── Public │ │ ├── openapi.html │ │ └── openapi.yaml │ ├── README.md │ └── Sources │ │ └── SwaggerUIEndpointsServer │ │ ├── SwaggerUIEndpointsServer.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml ├── tracing-middleware-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ │ ├── HelloWorldVaporServer │ │ │ ├── HelloWorldVaporServer.swift │ │ │ ├── openapi-generator-config.yaml │ │ │ └── openapi.yaml │ │ └── TracingMiddleware │ │ │ └── TracingMiddleware.swift │ └── docker │ │ ├── collector-config.yaml │ │ └── docker-compose.yaml ├── various-content-types-client-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ └── Sources │ │ └── ContentTypesClient │ │ ├── ContentTypesClient.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml └── various-content-types-server-example │ ├── .gitignore │ ├── Package.swift │ ├── README.md │ ├── Sources │ └── ContentTypesServer │ │ ├── ContentTypesServer.swift │ │ ├── openapi-generator-config.yaml │ │ └── openapi.yaml │ └── Tests │ └── ContentTypesServerTests │ └── ContentTypesServerTests.swift ├── IntegrationTest ├── .gitignore ├── Package.swift ├── README.md └── Sources │ ├── Client │ ├── Empty.swift │ ├── openapi-generator-config.yaml │ └── openapi.yaml │ ├── MockTransportClient │ └── Client.swift │ ├── MockTransportServer │ └── Server.swift │ ├── Server │ ├── Empty.swift │ ├── openapi-generator-config.yaml │ └── openapi.yaml │ ├── Types │ ├── Empty.swift │ ├── openapi-generator-config.yaml │ └── openapi.yaml │ └── openapi.yaml ├── LICENSE.txt ├── NOTICE.txt ├── Package.swift ├── Plugins ├── OpenAPIGenerator │ ├── PluginsShared │ └── plugin.swift ├── OpenAPIGeneratorCommand │ ├── PluginsShared │ └── plugin.swift └── PluginsShared │ ├── GeneratorMode.swift │ ├── PluginError.swift │ ├── PluginSource.swift │ └── PluginUtils.swift ├── README.md ├── SECURITY.md ├── Sources ├── PetstoreConsumerTestCore │ ├── Assertions.swift │ ├── Common.swift │ ├── TestClientTransport.swift │ └── TestServerTransport.swift ├── _OpenAPIGeneratorCore │ ├── Config.swift │ ├── Diagnostics.swift │ ├── DiagnosticsCollectorProvider.swift │ ├── Documentation.docc │ │ └── Documentation.md │ ├── Errors.swift │ ├── Extensions │ │ ├── Foundation.swift │ │ ├── OpenAPIKit.swift │ │ ├── String.swift │ │ └── SwiftStandardLibrary.swift │ ├── FeatureFlags.swift │ ├── GeneratorMode.swift │ ├── GeneratorPipeline.swift │ ├── GeneratorPipelineStage.swift │ ├── Hooks │ │ ├── DocumentFilter.swift │ │ └── FilteredDocument.swift │ ├── Layers │ │ ├── ParsedOpenAPIRepresentation.swift │ │ ├── RenderedSwiftRepresentation.swift │ │ └── StructuredSwiftRepresentation.swift │ ├── Parser │ │ ├── ParserProtocol.swift │ │ ├── YamsParser.swift │ │ └── validateDoc.swift │ ├── PlatformChecks.swift │ ├── Renderer │ │ ├── RendererProtocol.swift │ │ └── TextBasedRenderer.swift │ ├── Translator │ │ ├── ClientTranslator │ │ │ ├── ClientTranslator.swift │ │ │ └── translateClientMethod.swift │ │ ├── CommonTranslations │ │ │ ├── translateAllAnyOneOf.swift │ │ │ ├── translateArray.swift │ │ │ ├── translateCodable.swift │ │ │ ├── translateObjectStruct.swift │ │ │ ├── translateRawEnum.swift │ │ │ ├── translateRawRepresentableEnum.swift │ │ │ ├── translateSchema.swift │ │ │ ├── translateStructBlueprint.swift │ │ │ └── translateTypealias.swift │ │ ├── CommonTypes │ │ │ ├── Annotations.swift │ │ │ ├── CommentExtensions.swift │ │ │ ├── Constants.swift │ │ │ ├── DiscriminatorExtensions.swift │ │ │ ├── SchemaOverrides.swift │ │ │ └── StructBlueprint.swift │ │ ├── Content │ │ │ ├── CodingStrategy.swift │ │ │ ├── ContentInspector.swift │ │ │ ├── ContentType.swift │ │ │ └── SchemaContent.swift │ │ ├── FileTranslator+FeatureFlags.swift │ │ ├── FileTranslator.swift │ │ ├── Multipart │ │ │ ├── MultipartAdditionalProperties.swift │ │ │ ├── MultipartContent.swift │ │ │ ├── MultipartContentInspector.swift │ │ │ └── translateMultipart.swift │ │ ├── MultiplexTranslator.swift │ │ ├── Operations │ │ │ └── OperationDescription.swift │ │ ├── Parameters │ │ │ ├── TypedParameter.swift │ │ │ └── translateParameter.swift │ │ ├── Recursion │ │ │ ├── DeclarationRecursionDetector.swift │ │ │ └── RecursionDetector.swift │ │ ├── RequestBody │ │ │ ├── TypedRequestBody.swift │ │ │ └── translateRequestBody.swift │ │ ├── Responses │ │ │ ├── HTTPStatusCodes.swift │ │ │ ├── ResponseKind.swift │ │ │ ├── TypedResponse.swift │ │ │ ├── TypedResponseHeader.swift │ │ │ ├── acceptHeaderContentTypes.swift │ │ │ ├── translateResponse.swift │ │ │ ├── translateResponseHeader.swift │ │ │ └── translateResponseOutcome.swift │ │ ├── ServerTranslator │ │ │ ├── ServerTranslator.swift │ │ │ └── translateServerMethod.swift │ │ ├── TranslatorProtocol.swift │ │ ├── TypeAssignment │ │ │ ├── Builtins.swift │ │ │ ├── ReferenceStack.swift │ │ │ ├── SafeNameGenerator.swift │ │ │ ├── TypeAssigner.swift │ │ │ ├── TypeLocation.swift │ │ │ ├── TypeMatcher.swift │ │ │ ├── TypeName.swift │ │ │ ├── TypeUsage.swift │ │ │ └── isSchemaSupported.swift │ │ └── TypesTranslator │ │ │ ├── TypesFileTranslator.swift │ │ │ ├── translateAPIProtocol.swift │ │ │ ├── translateBoxedTypes.swift │ │ │ ├── translateComponentHeaders.swift │ │ │ ├── translateComponentParameters.swift │ │ │ ├── translateComponentRequestBodies.swift │ │ │ ├── translateComponentResponses.swift │ │ │ ├── translateComponents.swift │ │ │ ├── translateOperations.swift │ │ │ ├── translateSchemas.swift │ │ │ ├── translateServers.swift │ │ │ └── translateServersVariables.swift │ └── YamlFileDiagnosticsCollector.swift └── swift-openapi-generator │ ├── Documentation.docc │ ├── Articles │ │ ├── API-stability-of-generated-code.md │ │ ├── API-stability-of-the-generator.md │ │ ├── Checking-out-an-example-project.md │ │ ├── Configuring-the-generator.md │ │ ├── Contributing-to-Swift-OpenAPI-Generator.md │ │ ├── Frequently-asked-questions.md │ │ ├── Manually-invoking-the-generator-CLI.md │ │ ├── Practicing-spec-driven-API-development.md │ │ ├── Project-scope-and-goals.md │ │ ├── Supported-OpenAPI-features.md │ │ └── Useful-OpenAPI-patterns.md │ ├── Development │ │ ├── Converting-between-data-and-Swift-types.md │ │ ├── Documentation-for-maintainers.md │ │ ├── Generating-custom-Codable-conformance-methods.md │ │ ├── Handling-nullable-schemas.md │ │ └── Supporting-recursive-types.md │ ├── Info.plist │ ├── Proposals │ │ ├── Proposals.md │ │ ├── SOAR-0001.md │ │ ├── SOAR-0002.md │ │ ├── SOAR-0003.md │ │ ├── SOAR-0004.md │ │ ├── SOAR-0005.md │ │ ├── SOAR-0007.md │ │ ├── SOAR-0008.md │ │ ├── SOAR-0009.md │ │ ├── SOAR-0010.md │ │ ├── SOAR-0011.md │ │ ├── SOAR-0012.md │ │ ├── SOAR-0013.md │ │ └── SOAR-NNNN.md │ ├── Swift-OpenAPI-Generator.md │ └── Tutorials │ │ ├── Adding-openapi-and-swagger-ui-endpoints.tutorial │ │ ├── ClientSwiftPM.tutorial │ │ ├── ClientXcode.tutorial │ │ ├── ExploreOpenAPI.tutorial │ │ ├── ServerSwiftPM.tutorial │ │ ├── Swift OpenAPI Generator.tutorial │ │ └── _Resources │ │ ├── client.Package.0.swift │ │ ├── client.Package.1.swift │ │ ├── client.Package.2.swift │ │ ├── client.Package.3.swift │ │ ├── client.Package.4.swift │ │ ├── client.Package.5.swift │ │ ├── client.console.1.0.txt │ │ ├── client.console.1.1.txt │ │ ├── client.console.1.2.txt │ │ ├── client.console.2.0.txt │ │ ├── client.console.2.1.txt │ │ ├── client.console.2.2.txt │ │ ├── client.console.3.0.txt │ │ ├── client.console.4.0.txt │ │ ├── client.main.0.swift │ │ ├── client.main.1.swift │ │ ├── client.main.2.swift │ │ ├── client.main.3.swift │ │ ├── client.main.4.swift │ │ ├── client.main.5.swift │ │ ├── client.main.6.swift │ │ ├── client.main.7.swift │ │ ├── client.openapi-generator-config.yaml │ │ ├── client.openapi.2.yaml │ │ ├── client.openapi.yaml │ │ ├── client.xcode.0.swift │ │ ├── client.xcode.1.swift │ │ ├── client.xcode.2.swift │ │ ├── client.xcode.3.swift │ │ ├── client.xcode.4.swift │ │ ├── client.xcode.5.swift │ │ ├── client.xcode.6.2.swift │ │ ├── client.xcode.6.swift │ │ ├── client.xcode.7.swift │ │ ├── exploring-openapi.openapi.0.yaml │ │ ├── exploring-openapi.openapi.1.yaml │ │ ├── exploring-openapi.openapi.2.yaml │ │ ├── exploring-openapi.openapi.3.yaml │ │ ├── exploring-openapi.openapi.4.yaml │ │ ├── exploring-openapi.openapi.5.yaml │ │ ├── exploring-openapi.openapi.6.yaml │ │ ├── exploring-openapi.openapi.7.yaml │ │ ├── image.png │ │ ├── server-openapi-endpoints.console.0.txt │ │ ├── server-openapi-endpoints.console.1.txt │ │ ├── server-openapi-endpoints.console.2.txt │ │ ├── server-openapi-endpoints.main.0.swift │ │ ├── server-openapi-endpoints.main.1.swift │ │ ├── server-openapi-endpoints.main.2.swift │ │ ├── server-openapi-endpoints.openapi.0.yaml │ │ ├── server-openapi-endpoints.openapi.1.yaml │ │ ├── server-openapi-endpoints.openapi.html │ │ ├── server.Package.0.swift │ │ ├── server.Package.1.swift │ │ ├── server.Package.2.swift │ │ ├── server.Package.3.swift │ │ ├── server.Package.4.swift │ │ ├── server.Package.5.swift │ │ ├── server.console.1.0.txt │ │ ├── server.console.1.1.txt │ │ ├── server.console.1.2.txt │ │ ├── server.console.2.txt │ │ ├── server.console.3.txt │ │ ├── server.main.0.swift │ │ ├── server.main.1.1.swift │ │ ├── server.main.1.2.swift │ │ ├── server.main.2.swift │ │ ├── server.openapi-generator-config.yaml │ │ ├── server.openapi.0.yaml │ │ └── server.openapi.1.yaml │ ├── Extensions.swift │ ├── FilterCommand.swift │ ├── GenerateCommand.swift │ ├── GenerateOptions+runGenerator.swift │ ├── GenerateOptions.swift │ ├── PluginSource.swift │ ├── Tool.swift │ ├── UserConfig.swift │ └── runGenerator.swift ├── Tests ├── OpenAPIGeneratorCoreTests │ ├── Extensions │ │ └── Test_SwiftSafeNames.swift │ ├── Hooks │ │ └── Test_FilteredDocument.swift │ ├── Parser │ │ ├── Test_YamsParser.swift │ │ └── Test_validateDoc.swift │ ├── Renderer │ │ └── Test_TextBasedRenderer.swift │ ├── StructureHelpers.swift │ ├── TestUtilities.swift │ ├── Test_Config.swift │ ├── Test_DiagnosticsCollectorProvider.swift │ └── Translator │ │ ├── CommonTranslations │ │ ├── Test_translateCodable.swift │ │ ├── Test_translateStringEnum.swift │ │ └── Test_translateStructBlueprint.swift │ │ ├── CommonTypes │ │ └── Test_DiscriminatorExtensions.swift │ │ ├── Content │ │ └── Test_ContentType.swift │ │ ├── Multipart │ │ ├── Test_MultipartAdditionalProperties.swift │ │ └── Test_MultipartContentInspector.swift │ │ ├── Operations │ │ └── Test_OperationDescription.swift │ │ ├── TypeAssignment │ │ ├── Test_RecursionDetector_Generic.swift │ │ ├── Test_TypeAssigner.swift │ │ ├── Test_TypeMatcher.swift │ │ └── Test_isSchemaSupported.swift │ │ └── TypesTranslator │ │ └── Test_translateSchemas.swift ├── OpenAPIGeneratorReferenceTests │ ├── CompatabilityTest.swift │ ├── FileBasedReferenceTests.swift │ ├── Helpers.swift │ ├── Resources │ │ ├── Docs │ │ │ └── petstore.yaml │ │ └── ReferenceSources │ │ │ └── Petstore │ │ │ ├── Client.swift │ │ │ ├── Server.swift │ │ │ └── Types.swift │ ├── SnippetBasedReferenceTests.swift │ └── XCTestDiagnosticCollector.swift ├── OpenAPIGeneratorTests │ ├── Resources │ │ └── Docs │ │ │ ├── malformed-openapi.yaml │ │ │ └── openapi-generator-config.yaml │ └── Test_GenerateOptions.swift └── PetstoreConsumerTests │ ├── Common.swift │ ├── Generated │ ├── TestClient.swift │ ├── TestServer.swift │ ├── Test_Client.swift │ ├── Test_Playground.swift │ ├── Test_Server.swift │ └── Test_Types.swift └── scripts ├── run-integration-test.sh └── test-examples.sh /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/support-request.yml: -------------------------------------------------------------------------------- 1 | name: 🙋 Ask a question 2 | description: > 3 | Ask a question about or get help with Swift OpenAPI Generator. Beginner 4 | questions welcome! 5 | labels: [kind/support, status/triage] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: > 10 | This repository hosts issues for the Swift OpenAPI generator, the Swift 11 | OpenAPI runtime library, the Swift OpenAPI URLSession transport library, 12 | and the AsyncHTTPClient transport library. 13 | It does *not* track feedback on Xcode and other closed source Apple 14 | developer software such as URLSession itself; please direct it to 15 | [Feedback Assistant](https://developer.apple.com/bug-reporting) instead. 16 | 17 | ___ 18 | 19 | Please use this issue template to ask a question. 20 | 21 | You may also like to check out our 22 | [FAQ](https://swiftpackageindex.com/apple/swift-openapi-generator/documentation/swift-openapi-generator/frequently-asked-questions) 23 | for solutions to frequently asked questions and common workflows. 24 | - type: textarea 25 | attributes: 26 | label: Question 27 | description: > 28 | What are you trying to achieve? What have you tried? What didn't work as expected? 29 | validations: 30 | required: true 31 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/task.yml: -------------------------------------------------------------------------------- 1 | name: ⚙️ Track a task 2 | description: > 3 | Tasks can be used to track internal work, extract individual subtasks from a 4 | larger issue, and can serve as umbrella issues themselves. 5 | labels: [kind/enhancement, status/triage] 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: > 10 | This repository hosts issues for the Swift OpenAPI generator, the Swift 11 | OpenAPI runtime library, the Swift OpenAPI URLSession transport library, 12 | and the AsyncHTTPClient transport library. 13 | It does *not* track feedback on Xcode and other closed source Apple 14 | developer software such as URLSession itself; please direct it to 15 | [Feedback Assistant](https://developer.apple.com/bug-reporting) instead. 16 | 17 | ___ 18 | - type: textarea 19 | attributes: 20 | label: Description 21 | description: > 22 | A comprehensive description of the task, in human language. 23 | validations: 24 | required: true 25 | - type: textarea 26 | attributes: 27 | label: Additional information 28 | description: > 29 | Any complementary information that could be valuable to an implementor. 30 | validations: 31 | required: false 32 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Motivation 2 | 3 | _[Explain here the context, and why you're making that change. What is the problem you're trying to solve.]_ 4 | 5 | ### Modifications 6 | 7 | _[Describe the modifications you've made.]_ 8 | 9 | ### Result 10 | 11 | _[After your change, what will change.]_ 12 | 13 | ### Test Plan 14 | 15 | _[Describe the steps you took, or will take, to qualify the change - such as adjusting tests and manual testing.]_ 16 | -------------------------------------------------------------------------------- /.github/release.yml: -------------------------------------------------------------------------------- 1 | changelog: 2 | categories: 3 | - title: SemVer Major 4 | labels: 5 | - ⚠️ semver/major 6 | - title: SemVer Minor 7 | labels: 8 | - 🆕 semver/minor 9 | - title: SemVer Patch 10 | labels: 11 | - 🔨 semver/patch 12 | - title: Other Changes 13 | labels: 14 | - semver/none 15 | -------------------------------------------------------------------------------- /.github/workflows/pull_request_label.yml: -------------------------------------------------------------------------------- 1 | name: PR label 2 | 3 | on: 4 | pull_request: 5 | types: [labeled, unlabeled, opened, reopened, synchronize] 6 | 7 | jobs: 8 | semver-label-check: 9 | name: Semantic version label check 10 | runs-on: ubuntu-latest 11 | timeout-minutes: 1 12 | steps: 13 | - name: Checkout repository 14 | uses: actions/checkout@v4 15 | with: 16 | persist-credentials: false 17 | - name: Check for Semantic Version label 18 | uses: apple/swift-nio/.github/actions/pull_request_semver_label_checker@main 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | .swiftpm 13 | -------------------------------------------------------------------------------- /.licenseignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | .licenseignore 3 | .swiftformatignore 4 | .unacceptablelanguageignore 5 | .spi.yml 6 | .swift-format 7 | .github/* 8 | CODE_OF_CONDUCT.md 9 | CONTRIBUTING.md 10 | CONTRIBUTORS.txt 11 | LICENSE.txt 12 | NOTICE.txt 13 | Package.swift 14 | Package.resolved 15 | README.md 16 | SECURITY.md 17 | scripts/unacceptable-language.txt 18 | Tests/PetstoreConsumerTests/Generated 19 | Tests/OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/* 20 | docker/* 21 | **/*.docc/* 22 | **/.gitignore 23 | **/Package.swift 24 | **/Package.resolved 25 | **/README.md 26 | **/openapi.yaml 27 | **/malformed-openapi.yaml 28 | **/openapi.yml 29 | **/petstore.yaml 30 | **/openapi-generator-config.yaml 31 | **/openapi-generator-config.yml 32 | **/docker-compose.yaml 33 | **/docker/* 34 | **/.dockerignore 35 | Plugins/OpenAPIGenerator/PluginsShared 36 | Plugins/OpenAPIGeneratorCommand/PluginsShared 37 | Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp.* 38 | Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/Assets.xcassets/* 39 | Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/Preview* 40 | Examples/**/Generated* 41 | Examples/streaming-chatgpt-proxy/.env.example 42 | Examples/streaming-chatgpt-proxy/players.txt 43 | **/Makefile 44 | **/*.html 45 | .editorconfig 46 | -------------------------------------------------------------------------------- /.spi.yml: -------------------------------------------------------------------------------- 1 | version: 1 2 | builder: 3 | configs: 4 | - documentation_targets: 5 | - swift-openapi-generator 6 | -------------------------------------------------------------------------------- /.swiftformatignore: -------------------------------------------------------------------------------- 1 | Tests/OpenAPIGeneratorReferenceTests/Resources 2 | Sources/swift-openapi-generator/Documentation.docc 3 | Examples/**/Generated/* 4 | Examples/**/GeneratedSources/* 5 | Examples/streaming-chatgpt-proxy/** 6 | **Package.swift 7 | -------------------------------------------------------------------------------- /.unacceptablelanguageignore: -------------------------------------------------------------------------------- 1 | Examples/streaming-chatgpt-proxy/Sources/ChatGPT/openapi.yaml 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | The code of conduct for this project can be found at https://swift.org/code-of-conduct. 4 | 5 | 6 | -------------------------------------------------------------------------------- /CONTRIBUTORS.txt: -------------------------------------------------------------------------------- 1 | For the purpose of tracking copyright, this is the list of individuals and 2 | organizations who have contributed source code to SwiftOpenAPIGenerator. 3 | 4 | For employees of an organization/company where the copyright of work done 5 | by employees of that company is held by the company itself, only the company 6 | needs to be listed here. 7 | 8 | ## COPYRIGHT HOLDERS 9 | 10 | - Apple Inc. (all contributors with '@apple.com') 11 | 12 | ### Contributors 13 | 14 | - George Barnett 15 | - Gus Cairo 16 | - Honza Dvorsky 17 | - Mario Sangiorgio 18 | - Si Beaumont 19 | 20 | **Updating this list** 21 | 22 | Please do not edit this file manually. It is generated using `bash ./scripts/generate-contributors-list.sh`. If a name is misspelled or appearing multiple times: add an entry in `./.mailmap` 23 | -------------------------------------------------------------------------------- /Examples/.gitignore: -------------------------------------------------------------------------------- 1 | **/Package.resolved 2 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "89964AC6-C28B-4C2A-9CF4-824B569065D0", 5 | "name" : "Test Scheme Action", 6 | "options" : { 7 | "targetForVariableExpansion" : null 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "targetForVariableExpansion" : { 13 | "containerPath" : "container:HelloWorldiOSClientApp.xcodeproj", 14 | "identifier" : "3A133CDE2B1F28870008DD5E", 15 | "name" : "HelloWorldiOSClientApp" 16 | } 17 | }, 18 | "testTargets" : [ 19 | { 20 | "target" : { 21 | "containerPath" : "container:HelloWorldiOSClientApp.xcodeproj", 22 | "identifier" : "3A133CEE2B1F28890008DD5E", 23 | "name" : "HelloWorldiOSClientAppTests" 24 | } 25 | }, 26 | { 27 | "target" : { 28 | "containerPath" : "container:HelloWorldiOSClientApp.xcodeproj", 29 | "identifier" : "3A133CF82B1F28890008DD5E", 30 | "name" : "HelloWorldiOSClientAppUITests" 31 | } 32 | } 33 | ], 34 | "version" : 1 35 | } 36 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "platform" : "ios", 6 | "size" : "1024x1024" 7 | } 8 | ], 9 | "info" : { 10 | "author" : "xcode", 11 | "version" : 1 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/HelloWorldiOSClientApp.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import SwiftUI 16 | 17 | @main struct HelloWorldiOSClientApp: App { var body: some Scene { WindowGroup { ContentView() } } } 18 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientApp/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientAppTests/HelloWorldiOSClientAppTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import XCTest 16 | @testable import HelloWorldiOSClientApp 17 | 18 | final class HelloWorldiOSClientAppTests: XCTestCase { 19 | 20 | func testMockClient() async throws { 21 | let mockClient = MockClient() 22 | let response = try await mockClient.getGreeting(query: .init(name: "Test")) 23 | let message = try response.ok.body.json.message 24 | XCTAssertEqual(message, "(Mock) Hello, Test!") 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Examples/HelloWorldiOSClientAppExample/HelloWorldiOSClientAppUITests/HelloWorldiOSClientAppUITests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | import XCTest 16 | 17 | final class HelloWorldiOSClientAppUITests: XCTestCase { 18 | 19 | func testGreeting() throws { 20 | let app = XCUIApplication() 21 | // Comment out the following line to have the app running in the simulator try to connect 22 | // to the server running on your Mac. 23 | app.launchEnvironment["USE_MOCK_CLIENT"] = "true" 24 | app.launch() 25 | let textField = app.textFields["Name"] 26 | textField.tap() 27 | textField.doubleTap() 28 | textField.typeKey(XCUIKeyboardKey.delete.rawValue, modifierFlags: []) 29 | textField.typeText("Test") 30 | app.buttons["Refresh greeting"].tap() 31 | XCTAssertEqual(app.staticTexts["greeting-label"].label, "(Mock) Hello, Test!") 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Examples/auth-client-middleware-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/auth-client-middleware-example/README.md: -------------------------------------------------------------------------------- 1 | # Auth client middleware 2 | 3 | In this example we'll implement a `ClientMiddleware` that injects an authentication header into the request. 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | This example extends the [hello-world-urlsession-client-example](../hello-world-urlsession-client-example) 10 | with a new target, `AuthenticationClientMiddleware`, which is then used when creating 11 | the `Client`. 12 | 13 | NOTE: This example shows just one way of injecting authentication information in a middleware 14 | and is purely for illustrative purposes. 15 | 16 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 17 | 18 | The server can be started by running the `AuthenticationServerMiddleware` example locally. 19 | 20 | ## Usage 21 | 22 | Build and run the client CLI using: 23 | 24 | ```console 25 | % swift run HelloWorldURLSessionClient token_for_Frank 26 | Hello, Stranger! (Requested by: Frank) 27 | % swift run HelloWorldURLSessionClient invalid_token 28 | Unauthorized 29 | ``` 30 | -------------------------------------------------------------------------------- /Examples/auth-client-middleware-example/Sources/HelloWorldURLSessionClient/HelloWorldURLSessionClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIURLSession 15 | import Foundation 16 | import AuthenticationClientMiddleware 17 | 18 | @main struct HelloWorldURLSessionClient { 19 | static func main() async throws { 20 | let args = CommandLine.arguments 21 | guard args.count == 2 else { 22 | print("Requires a token") 23 | exit(1) 24 | } 25 | let client = Client( 26 | serverURL: URL(string: "http://localhost:8080/api")!, 27 | transport: URLSessionTransport(), 28 | middlewares: [AuthenticationMiddleware(authorizationHeaderFieldValue: args[1])] 29 | ) 30 | let response = try await client.getGreeting() 31 | switch response { 32 | case .ok(let okResponse): print(try okResponse.body.json.message) 33 | case .unauthorized: print("Unauthorized") 34 | case .undocumented(statusCode: let statusCode, _): print("Undocumented status code: \(statusCode)") 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Examples/auth-client-middleware-example/Sources/HelloWorldURLSessionClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/auth-client-middleware-example/Sources/HelloWorldURLSessionClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | '401': 27 | description: Authentication required. 28 | components: 29 | schemas: 30 | Greeting: 31 | type: object 32 | description: A value with the greeting contents. 33 | properties: 34 | message: 35 | type: string 36 | description: The string representation of the greeting. 37 | required: 38 | - message 39 | -------------------------------------------------------------------------------- /Examples/auth-server-middleware-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/auth-server-middleware-example/README.md: -------------------------------------------------------------------------------- 1 | # Auth server middleware 2 | 3 | In this example we'll implement a `ServerMiddleware` that verifies an authentication header in the request. 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | This example extends the [hello-world-vapor-server-example](../hello-world-vapor-server-example) 10 | with a new target, `AuthenticationServerMiddleware`, which is then used when registering the server handler with the transport. 11 | 12 | NOTE: This example shows just one way of varifying authentication information in a middleware and is purely for illustrative purposes. 13 | 14 | The tool uses the [Vapor](https://github.com/vapor/vapor) server framework to handle HTTP requests, wrapped in the [Swift OpenAPI Vapor Transport](https://github.com/swift-server/swift-openapi-vapor). 15 | 16 | The CLI starts the server on `http://localhost:8080` and can be invoked by running the `AuthenticationClientMiddleware` example client or on the command line using: 17 | 18 | ```console 19 | % curl -H 'Authorization: token_for_Frank' 'http://localhost:8080/api/greet?name=Jane' 20 | { 21 | "message" : "Hello, Jane! (Requested by: Frank)" 22 | } 23 | ``` 24 | 25 | ## Usage 26 | 27 | Build and run the server CLI using: 28 | 29 | ```console 30 | % swift run 31 | 2023-12-01T14:14:35+0100 notice codes.vapor.application : [Vapor] Server starting on http://127.0.0.1:8080 32 | ... 33 | ``` 34 | -------------------------------------------------------------------------------- /Examples/auth-server-middleware-example/Sources/HelloWorldVaporServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/auth-server-middleware-example/Sources/HelloWorldVaporServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | '401': 27 | description: Authentication required. 28 | components: 29 | schemas: 30 | Greeting: 31 | type: object 32 | description: A value with the greeting contents. 33 | properties: 34 | message: 35 | type: string 36 | description: The string representation of the greeting. 37 | required: 38 | - message 39 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-client-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-client-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "bidirectional-event-streams-client-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/swift-server/swift-openapi-async-http-client", from: "1.0.0"), 24 | ], 25 | targets: [ 26 | .executableTarget( 27 | name: "BidirectionalEventStreamsClient", 28 | dependencies: [ 29 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 30 | .product(name: "OpenAPIAsyncHTTPClient", package: "swift-openapi-async-http-client"), 31 | ], 32 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 33 | ) 34 | ] 35 | ) 36 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-client-example/README.md: -------------------------------------------------------------------------------- 1 | # Client handling bidirectional event streams 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A command-line tool that uses a generated client to show how to work with bidirectional event streams. 10 | 11 | Instead of [URLSession](https://developer.apple.com/documentation/foundation/urlsession), which will return stream only until at least “some” bytes of the body have also been received (see [comment](https://github.com/apple/swift-openapi-urlsession/blob/main/Tests/OpenAPIURLSessionTests/URLSessionBidirectionalStreamingTests/URLSessionBidirectionalStreamingTests.swift#L193-L206)), tool uses the [AsyncHTTPClient](https://github.com/swift-server/async-http-client) API to perform the HTTP call, wrapped in the [AsyncHTTPClient Transport for Swift OpenAPI Generator](https://github.com/swift-server/swift-openapi-async-http-client). A workaround for URLSession could be sending an `empty`, `.joined` or some kind of hearbeat message from server first when initialising a stream. 12 | 13 | The server can be started by running `bidirectional-event-streams-server-example` locally. 14 | 15 | ## Usage 16 | 17 | Build and run the client CLI using: 18 | 19 | ```console 20 | % swift run 21 | Sending and fetching back greetings using JSON Lines 22 | ... 23 | ``` 24 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-client-example/Sources/BidirectionalEventStreamsClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-client-example/Sources/BidirectionalEventStreamsClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: EventStreamsService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greetings: 10 | post: 11 | operationId: getGreetingsStream 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | requestBody: 20 | description: A body with a greetings stream. 21 | required: true 22 | content: 23 | application/jsonl: {} 24 | responses: 25 | '200': 26 | description: A success response with a greetings stream. 27 | content: 28 | application/jsonl: {} 29 | components: 30 | schemas: 31 | Greeting: 32 | type: object 33 | description: A value with the greeting contents. 34 | properties: 35 | message: 36 | type: string 37 | description: The string representation of the greeting. 38 | required: 39 | - message 40 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-server-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | 13 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-server-example/README.md: -------------------------------------------------------------------------------- 1 | # Server supporting bidirectional event streams 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A server that uses generated server stubs to show how to work with bidirectional event streams. 10 | 11 | The tool uses the [Hummingbird](https://github.com/hummingbird-project/hummingbird) server framework to handle HTTP requests, wrapped in the [Swift OpenAPI Hummingbird](https://github.com/swift-server/swift-openapi-hummingbird). 12 | 13 | The CLI starts the server on `http://localhost:8080` and can be invoked by running `bidirectional-event-streams-client-example`. 14 | 15 | ## Usage 16 | 17 | Build and run the server CLI using: 18 | 19 | ```console 20 | % swift run 21 | 2024-07-04T08:56:23+0200 info Hummingbird : [HummingbirdCore] Server started and listening on 127.0.0.1:8080 22 | ... 23 | ``` 24 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/bidirectional-event-streams-server-example/Sources/BidirectionalEventStreamsServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: EventStreamsService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greetings: 10 | post: 11 | operationId: getGreetingsStream 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | requestBody: 20 | description: A body with a greetings stream. 21 | required: true 22 | content: 23 | application/jsonl: {} 24 | responses: 25 | '200': 26 | description: A success response with a greetings stream. 27 | content: 28 | application/jsonl: {} 29 | components: 30 | schemas: 31 | Greeting: 32 | type: object 33 | description: A value with the greeting contents. 34 | properties: 35 | message: 36 | type: string 37 | description: The string representation of the greeting. 38 | required: 39 | - message 40 | -------------------------------------------------------------------------------- /Examples/command-line-client-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/command-line-client-example/README.md: -------------------------------------------------------------------------------- 1 | # Command-line client 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A command-line tool using Swift Argument Parser that shows using a generated client to make a request to the Greeting Service running on `http://localhost:8080`. 10 | 11 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 12 | 13 | The server can be started by running any of the Hello World server examples locally. 14 | 15 | ## Usage 16 | 17 | Build and run the client CLI using: 18 | 19 | ```console 20 | % swift run CommandLineClient greet --name CLI 21 | Hello, CLI! 22 | ``` 23 | -------------------------------------------------------------------------------- /Examples/command-line-client-example/Sources/CommandLineClient/CommandLineClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIURLSession 16 | import Foundation 17 | import ArgumentParser 18 | 19 | @main struct CommandLineClient: AsyncParsableCommand { 20 | static var configuration: CommandConfiguration = .init( 21 | commandName: "CommandLineClient", 22 | subcommands: [GreetCommand.self] 23 | ) 24 | } 25 | 26 | struct GreetCommand: AsyncParsableCommand { 27 | 28 | static var configuration: CommandConfiguration = .init(commandName: "greet") 29 | 30 | @Option var name: String 31 | 32 | func run() async throws { 33 | let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) 34 | let response = try await client.getGreeting(query: .init(name: name)) 35 | print(try response.ok.body.json.message) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Examples/command-line-client-example/Sources/CommandLineClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/command-line-client-example/Sources/CommandLineClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/curated-client-library-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/curated-client-library-example/README.md: -------------------------------------------------------------------------------- 1 | # Curated client library 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A library package that shows wrapping a generated client to make a request to the Greeting Service running on `http://localhost:8080`. 10 | 11 | The generated code is fully wrapped by the curated, hand-written Swift API. The library doesn't leak the fact that it uses Swift OpenAPI Generator under the hood. 12 | 13 | Under the hood, the tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 14 | 15 | The server can be started by running any of the Hello World server examples locally. 16 | 17 | ## Usage 18 | 19 | In another package, add this one as a package dependency. 20 | 21 | Then, use the provided client API: 22 | 23 | ```swift 24 | import CuratedLibraryClient 25 | 26 | let client = GreetingClient() 27 | let message = try await client.getGreeting(name: "Frank") 28 | print("Received the greeting message: \(message)") 29 | ``` 30 | 31 | ## Testing 32 | 33 | Run tests using: 34 | 35 | ```console 36 | % swift test 37 | ``` 38 | 39 | The testing strategy is to use a test implementation of the generated `APIProtocol` that allows simulating various conditions, including errors. 40 | 41 | Then, the `GreetingClient` can be exercised in unit tests without it making live network requests under the hood. 42 | -------------------------------------------------------------------------------- /Examples/curated-client-library-example/Sources/CuratedLibraryClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/curated-client-library-example/Sources/CuratedLibraryClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/event-streams-client-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/event-streams-client-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "event-streams-client-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 24 | ], 25 | targets: [ 26 | .executableTarget( 27 | name: "EventStreamsClient", 28 | dependencies: [ 29 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 30 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 31 | ], 32 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 33 | ) 34 | ] 35 | ) 36 | -------------------------------------------------------------------------------- /Examples/event-streams-client-example/README.md: -------------------------------------------------------------------------------- 1 | # Client handling event streams 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A command-line tool that uses a generated client to show how to work with event streams, such as JSON Lines, JSON Sequence, and Server-sent Events. 10 | 11 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 12 | 13 | The server can be started by running `event-streams-server-example` locally. 14 | 15 | ## Usage 16 | 17 | Build and run the client CLI using: 18 | 19 | ```console 20 | % swift run 21 | ``` 22 | -------------------------------------------------------------------------------- /Examples/event-streams-client-example/Sources/EventStreamsClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/event-streams-client-example/Sources/EventStreamsClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: EventStreamsService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greetings: 10 | get: 11 | operationId: getGreetingsStream 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | - name: count 20 | required: false 21 | in: query 22 | description: The number of greetings returned in the stream. 23 | schema: 24 | type: integer 25 | format: int32 26 | default: 10 27 | responses: 28 | '200': 29 | description: A success response with a greetings stream. 30 | content: 31 | application/jsonl: {} 32 | application/json-seq: {} 33 | text/event-stream: {} 34 | components: 35 | schemas: 36 | Greeting: 37 | type: object 38 | description: A value with the greeting contents. 39 | properties: 40 | message: 41 | type: string 42 | description: The string representation of the greeting. 43 | required: 44 | - message 45 | -------------------------------------------------------------------------------- /Examples/event-streams-server-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/event-streams-server-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "event-streams-server-example", 19 | platforms: [.macOS(.v10_15)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"), 24 | .package(url: "https://github.com/vapor/vapor", from: "4.89.0"), 25 | ], 26 | targets: [ 27 | .executableTarget( 28 | name: "EventStreamsServer", 29 | dependencies: [ 30 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 31 | .product(name: "OpenAPIVapor", package: "swift-openapi-vapor"), 32 | .product(name: "Vapor", package: "vapor"), 33 | ], 34 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 35 | ) 36 | ] 37 | ) 38 | -------------------------------------------------------------------------------- /Examples/event-streams-server-example/README.md: -------------------------------------------------------------------------------- 1 | # Server supporting event streams 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A server that uses generated server stubs to show how to work with event streams, such as JSON Lines, JSON Sequence, and Server-sent Events. 10 | 11 | The tool uses the [Vapor](https://github.com/vapor/vapor) server framework to handle HTTP requests, wrapped in the [Swift OpenAPI Vapor Transport](https://github.com/swift-server/swift-openapi-vapor). 12 | 13 | The CLI starts the server on `http://localhost:8080` and can be invoked by running `event-streams-client-example` or on the command line using: 14 | 15 | ```console 16 | % curl -N http://127.0.0.1:8080/api/greetings\?name\=CLI\&count\=3 17 | {"message":"Hey, CLI!"} 18 | {"message":"Hello, CLI!"} 19 | {"message":"Greetings, CLI!"} 20 | ``` 21 | 22 | ## Usage 23 | 24 | Build and run the server CLI using: 25 | 26 | ```console 27 | % swift run 28 | 2023-12-01T14:14:35+0100 notice codes.vapor.application : [Vapor] Server starting on http://127.0.0.1:8080 29 | ... 30 | ``` 31 | -------------------------------------------------------------------------------- /Examples/event-streams-server-example/Sources/EventStreamsServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/event-streams-server-example/Sources/EventStreamsServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: EventStreamsService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greetings: 10 | get: 11 | operationId: getGreetingsStream 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | - name: count 20 | required: false 21 | in: query 22 | description: The number of greetings returned in the stream. 23 | schema: 24 | type: integer 25 | format: int32 26 | default: 10 27 | responses: 28 | '200': 29 | description: A success response with a greetings stream. 30 | content: 31 | application/jsonl: {} 32 | application/json-seq: {} 33 | text/event-stream: {} 34 | components: 35 | schemas: 36 | Greeting: 37 | type: object 38 | description: A value with the greeting contents. 39 | properties: 40 | message: 41 | type: string 42 | description: The string representation of the greeting. 43 | required: 44 | - message 45 | -------------------------------------------------------------------------------- /Examples/hello-world-async-http-client-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/hello-world-async-http-client-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "hello-world-async-http-client-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/swift-server/swift-openapi-async-http-client", from: "1.0.0"), 24 | ], 25 | targets: [ 26 | .executableTarget( 27 | name: "HelloWorldAsyncHTTPClient", 28 | dependencies: [ 29 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 30 | .product(name: "OpenAPIAsyncHTTPClient", package: "swift-openapi-async-http-client"), 31 | ], 32 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 33 | ) 34 | ] 35 | ) 36 | -------------------------------------------------------------------------------- /Examples/hello-world-async-http-client-example/README.md: -------------------------------------------------------------------------------- 1 | # Hello World client using AsyncHTTPClient 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A "hello world" command-line tool that uses a generated client to make a request to the Greeting Service running on `http://localhost:8080`. 10 | 11 | The tool uses the [AsyncHTTPClient](https://github.com/swift-server/async-http-client) API to perform the HTTP call, wrapped in the [Swift OpenAPI AsyncHTTPClient Transport](https://github.com/swift-server/swift-openapi-async-http-client). 12 | 13 | The server can be started by running any of the Hello World server examples locally. 14 | 15 | ## Usage 16 | 17 | Build and run the client CLI using: 18 | 19 | ```console 20 | % swift run 21 | Hello, Stranger! 22 | ``` 23 | -------------------------------------------------------------------------------- /Examples/hello-world-async-http-client-example/Sources/HelloWorldAsyncHTTPClient/HelloWorldAsyncHTTPClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIAsyncHTTPClient 16 | import Foundation 17 | 18 | @main struct HelloWorldAsyncHTTPClient { 19 | static func main() async throws { 20 | let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: AsyncHTTPClientTransport()) 21 | let response = try await client.getGreeting() 22 | print(try response.ok.body.json.message) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Examples/hello-world-async-http-client-example/Sources/HelloWorldAsyncHTTPClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/hello-world-async-http-client-example/Sources/HelloWorldAsyncHTTPClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/hello-world-hummingbird-server-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/hello-world-hummingbird-server-example/Sources/HelloWorldHummingbirdServer/HelloWorldHummingbirdServer.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIHummingbird 16 | import Hummingbird 17 | import Foundation 18 | 19 | struct Handler: APIProtocol { 20 | func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output { 21 | let name = input.query.name ?? "Stranger" 22 | return .ok(.init(body: .json(.init(message: "Hello, \(name)!")))) 23 | } 24 | } 25 | 26 | @main struct HelloWorldHummingbirdServer { 27 | static func main() async throws { 28 | let router = Router() 29 | let handler = Handler() 30 | try handler.registerHandlers(on: router, serverURL: URL(string: "/api")!) 31 | let app = Application(router: router, configuration: .init()) 32 | try await app.run() 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Examples/hello-world-hummingbird-server-example/Sources/HelloWorldHummingbirdServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/hello-world-hummingbird-server-example/Sources/HelloWorldHummingbirdServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/hello-world-hummingbird-server-example/Tests/HelloWorldHummingbirdServerTests/HelloWorldHummingbirdServerTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import XCTest 15 | @testable import HelloWorldHummingbirdServer 16 | 17 | final class HelloWorldHummingbirdServerTests: XCTestCase { 18 | 19 | func testCustomName() async throws { 20 | let handler: APIProtocol = Handler() 21 | let response = try await handler.getGreeting(query: .init(name: "Jane")) 22 | XCTAssertEqual(response, .ok(.init(body: .json(.init(message: "Hello, Jane!"))))) 23 | } 24 | 25 | func testDefaultName() async throws { 26 | let handler: APIProtocol = Handler() 27 | let response = try await handler.getGreeting() 28 | XCTAssertEqual(response, .ok(.init(body: .json(.init(message: "Hello, Stranger!"))))) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Examples/hello-world-urlsession-client-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/hello-world-urlsession-client-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "hello-world-urlsession-client-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 24 | ], 25 | targets: [ 26 | .executableTarget( 27 | name: "HelloWorldURLSessionClient", 28 | dependencies: [ 29 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 30 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 31 | ], 32 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 33 | ) 34 | ] 35 | ) 36 | -------------------------------------------------------------------------------- /Examples/hello-world-urlsession-client-example/README.md: -------------------------------------------------------------------------------- 1 | # Hello World client using URLSession 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A "hello world" command-line tool that uses a generated client to make a request to the Greeting Service running on `http://localhost:8080`. 10 | 11 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 12 | 13 | The server can be started by running any of the Hello World server examples locally. 14 | 15 | ## Usage 16 | 17 | Build and run the client CLI using: 18 | 19 | ```console 20 | % swift run 21 | Hello, Stranger! 22 | ``` 23 | -------------------------------------------------------------------------------- /Examples/hello-world-urlsession-client-example/Sources/HelloWorldURLSessionClient/HelloWorldURLSessionClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIURLSession 16 | import Foundation 17 | 18 | @main struct HelloWorldURLSessionClient { 19 | static func main() async throws { 20 | let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) 21 | let response = try await client.getGreeting() 22 | print(try response.ok.body.json.message) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Examples/hello-world-urlsession-client-example/Sources/HelloWorldURLSessionClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/hello-world-urlsession-client-example/Sources/HelloWorldURLSessionClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/hello-world-vapor-server-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/hello-world-vapor-server-example/Sources/HelloWorldVaporServer/HelloWorldVaporServer.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIVapor 16 | import Vapor 17 | 18 | struct Handler: APIProtocol { 19 | func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output { 20 | let name = input.query.name ?? "Stranger" 21 | return .ok(.init(body: .json(.init(message: "Hello, \(name)!")))) 22 | } 23 | } 24 | 25 | @main struct HelloWorldVaporServer { 26 | static func main() async throws { 27 | let app = try await Vapor.Application.make() 28 | let transport = VaporTransport(routesBuilder: app) 29 | let handler = Handler() 30 | try handler.registerHandlers(on: transport, serverURL: URL(string: "/api")!) 31 | try await app.execute() 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Examples/hello-world-vapor-server-example/Sources/HelloWorldVaporServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/hello-world-vapor-server-example/Sources/HelloWorldVaporServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/hello-world-vapor-server-example/Tests/HelloWorldVaporServerTests/HelloWorldVaporServerTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import XCTest 15 | @testable import HelloWorldVaporServer 16 | 17 | final class HelloWorldVaporServerTests: XCTestCase { 18 | 19 | func testCustomName() async throws { 20 | let handler: APIProtocol = Handler() 21 | let response = try await handler.getGreeting(query: .init(name: "Jane")) 22 | XCTAssertEqual(response, .ok(.init(body: .json(.init(message: "Hello, Jane!"))))) 23 | } 24 | 25 | func testDefaultName() async throws { 26 | let handler: APIProtocol = Handler() 27 | let response = try await handler.getGreeting() 28 | XCTAssertEqual(response, .ok(.init(body: .json(.init(message: "Hello, Stranger!"))))) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Examples/logging-middleware-oslog-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/logging-middleware-oslog-example/Sources/HelloWorldURLSessionClient/HelloWorldURLSessionClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIURLSession 15 | import Foundation 16 | import LoggingMiddleware 17 | 18 | @main struct HelloWorldURLSessionClient { 19 | static func main() async throws { 20 | #if canImport(Darwin) 21 | let client = Client( 22 | serverURL: URL(string: "http://localhost:8080/api")!, 23 | transport: URLSessionTransport(), 24 | middlewares: [LoggingMiddleware(bodyLoggingConfiguration: .upTo(maxBytes: 1024))] 25 | ) 26 | let response = try await client.getGreeting() 27 | print(try response.ok.body.json.message) 28 | #else //canImport(Darwin) 29 | print("This example uses OSLog, so is only supported on Apple platforms") 30 | exit(EXIT_FAILURE) 31 | #endif //canImport(Darwin) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Examples/logging-middleware-oslog-example/Sources/HelloWorldURLSessionClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/logging-middleware-oslog-example/Sources/HelloWorldURLSessionClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/logging-middleware-swift-log-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/logging-middleware-swift-log-example/Sources/HelloWorldURLSessionClient/HelloWorldURLSessionClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIURLSession 15 | import Foundation 16 | import LoggingMiddleware 17 | import Logging 18 | 19 | @main struct HelloWorldURLSessionClient { 20 | static func main() async throws { 21 | var logger = Logger(label: "HelloWorldURLSessionClient") 22 | logger.logLevel = .debug 23 | let client = Client( 24 | serverURL: URL(string: "http://localhost:8080/api")!, 25 | transport: URLSessionTransport(), 26 | middlewares: [LoggingMiddleware(logger: logger, bodyLoggingConfiguration: .upTo(maxBytes: 1024))] 27 | ) 28 | let response = try await client.getGreeting() 29 | print(try response.ok.body.json.message) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Examples/logging-middleware-swift-log-example/Sources/HelloWorldURLSessionClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/logging-middleware-swift-log-example/Sources/HelloWorldURLSessionClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/logging-middleware-swift-log-example/Tests/LoggingMiddlewareTests/LoggingMiddlewareTests.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import XCTest 16 | 17 | @testable import LoggingMiddleware 18 | 19 | final class LoggingMiddlewareTests: XCTestCase { 20 | 21 | func testBodyLoggingPolicy() async throws { 22 | for (body, bodyLoggingPolicy, expectedMessage): (HTTPBody?, BodyLoggingPolicy, BodyLoggingPolicy.BodyLog) in [ 23 | (.none, .never, .none), (.none, .upTo(maxBytes: 5), .none), 24 | (HTTPBody("Hello", length: .unknown), .never, .redacted), 25 | (HTTPBody("Hello", length: .unknown), .upTo(maxBytes: 3), .unknownLength), 26 | (HTTPBody("Hello", length: .unknown), .upTo(maxBytes: 5), .unknownLength), 27 | (HTTPBody("Hello"), .never, .redacted), (HTTPBody("Hello"), .upTo(maxBytes: 3), .tooManyBytesToLog(5)), 28 | (HTTPBody("Hello"), .upTo(maxBytes: 5), .complete(Data("Hello".utf8))), 29 | ] { 30 | let (bodyToLog, _) = try await bodyLoggingPolicy.process(body) 31 | XCTAssertEqual(bodyToLog, expectedMessage) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Examples/manual-generation-generator-cli-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | .swift-openapi-generator/ 13 | -------------------------------------------------------------------------------- /Examples/manual-generation-generator-cli-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "manual-generation-generator-cli-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 23 | ], 24 | targets: [ 25 | .executableTarget( 26 | name: "ManualGeneratorInvocationClient", 27 | dependencies: [ 28 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 29 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 30 | ] 31 | ) 32 | ] 33 | ) 34 | -------------------------------------------------------------------------------- /Examples/manual-generation-generator-cli-example/README.md: -------------------------------------------------------------------------------- 1 | # Manual code generation using the `swift-openapi-generator` CLI 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A command-line tool that uses a generated client to make a request to the Greeting Service running on `http://localhost:8080`. 10 | 11 | It shows using the Swift OpenAPI Generator command-line tool manually instead of using either the build or command package plugin. 12 | 13 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 14 | 15 | The server can be started by running any of the Hello World server examples locally. 16 | 17 | ## Regenerate code 18 | 19 | Whenever the `openapi.yaml` document changes, rerun the code generation: 20 | 21 | ```console 22 | % make generate 23 | ``` 24 | 25 | And then check in the changed files in `./Sources/ManualGeneratorInvocationClient/Generated/*`. 26 | 27 | ## Usage 28 | 29 | Build and run the client CLI using: 30 | 31 | ```console 32 | % swift run 33 | Hello, Stranger! 34 | ``` 35 | -------------------------------------------------------------------------------- /Examples/manual-generation-generator-cli-example/Sources/ManualGeneratorInvocationClient/ManualGeneratorInvocationClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIURLSession 16 | import Foundation 17 | 18 | @main struct ManualGeneratorInvocationClient { 19 | static func main() async throws { 20 | let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) 21 | let response = try await client.getGreeting() 22 | print(try response.ok.body.json.message) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Examples/manual-generation-generator-cli-example/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/manual-generation-generator-cli-example/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/manual-generation-package-plugin-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/manual-generation-package-plugin-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "manual-generation-package-plugin-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 24 | ], 25 | targets: [ 26 | .executableTarget( 27 | name: "CommandPluginInvocationClient", 28 | dependencies: [ 29 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 30 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 31 | ] 32 | ) 33 | ] 34 | ) 35 | -------------------------------------------------------------------------------- /Examples/manual-generation-package-plugin-example/Sources/CommandPluginInvocationClient/CommandPluginInvocationClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIURLSession 16 | import Foundation 17 | 18 | @main struct CommandPluginInvocationClient { 19 | static func main() async throws { 20 | let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) 21 | let response = try await client.getGreeting() 22 | print(try response.ok.body.json.message) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Examples/manual-generation-package-plugin-example/Sources/CommandPluginInvocationClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/manual-generation-package-plugin-example/Sources/CommandPluginInvocationClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/.dockerignore: -------------------------------------------------------------------------------- 1 | .gitignore -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/Sources/HelloWorldVaporServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/Sources/HelloWorldVaporServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM swift:5.9 AS builder 2 | COPY ../Sources/ /code/Sources/ 3 | COPY ../Package.swift /code/Package.swift 4 | WORKDIR /code 5 | RUN swift build -c release 6 | 7 | FROM swift:5.9-slim AS runtime 8 | COPY --from=builder /code/.build/release/HelloWorldVaporServer /HelloWorldVaporServer 9 | ENV HOST=0.0.0.0 10 | ENV PORT=8080 11 | ENTRYPOINT [ "/HelloWorldVaporServer" ] 12 | -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/docker/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: "3.5" 2 | services: 3 | api: 4 | build: 5 | context: .. 6 | dockerfile: docker/Dockerfile 7 | ports: 8 | - 127.0.0.1:8080:8080 9 | prometheus: 10 | image: prom/prometheus 11 | volumes: 12 | - ./prometheus.yml:/etc/prometheus/prometheus.yml 13 | ports: 14 | - 127.0.0.1:9090:9090 15 | -------------------------------------------------------------------------------- /Examples/metrics-middleware-example/docker/prometheus.yml: -------------------------------------------------------------------------------- 1 | scrape_configs: 2 | - job_name: "greeting.server" 3 | scrape_interval: 5s 4 | static_configs: 5 | - targets: ["api:8080"] 6 | -------------------------------------------------------------------------------- /Examples/postgres-database-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/postgres-database-example/Sources/PostgresDatabaseServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/postgres-database-example/Sources/PostgresDatabaseServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | /count: 27 | get: 28 | operationId: getCount 29 | responses: 30 | '200': 31 | description: The previous greetings issued. 32 | content: 33 | application/json: 34 | schema: 35 | type: object 36 | properties: 37 | count: 38 | type: integer 39 | required: 40 | - count 41 | /reset: 42 | post: 43 | operationId: reset 44 | responses: 45 | '204': 46 | description: Reset the database. 47 | content: 48 | application/json: 49 | schema: 50 | type: object 51 | components: 52 | schemas: 53 | Greeting: 54 | type: object 55 | properties: 56 | message: 57 | type: string 58 | required: 59 | - message 60 | -------------------------------------------------------------------------------- /Examples/postgres-database-example/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | version: '3.7' 3 | 4 | services: 5 | postgres: 6 | image: postgres:15 7 | environment: 8 | POSTGRES_HOST_AUTH_METHOD: "${POSTGRES_HOST_AUTH_METHOD:-scram-sha-256}" 9 | POSTGRES_USER: test_username 10 | POSTGRES_DB: test_database 11 | POSTGRES_PASSWORD: test_password 12 | ports: 13 | - 5432:5432 14 | -------------------------------------------------------------------------------- /Examples/retrying-middleware-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/retrying-middleware-example/README.md: -------------------------------------------------------------------------------- 1 | # Retrying client middleware 2 | 3 | In this example we'll implement a `ClientMiddleware` that retries certain failed responses again. 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | This example extends the [hello-world-urlsession-client-example](../hello-world-urlsession-client-example) 10 | with a new target, `RetryingClientMiddleware`, which is then used when creating 11 | the `Client`. 12 | 13 | Requests with a body are only retried if the request body has `iterationPolicy` of `multiple`, as otherwise 14 | the request body cannot be iterated again. 15 | 16 | NOTE: This example shows just one way of retrying HTTP failures in a middleware 17 | and is purely for illustrative purposes. 18 | 19 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 20 | 21 | The server can be started by running any of the Hello World server examples locally. 22 | 23 | ## Usage 24 | 25 | Build and run the client CLI using: 26 | 27 | ``` 28 | % swift run 29 | Attempt 1 30 | Retrying with code 500 31 | Attempt 2 32 | Returning the received response, either because of success or ran out of attempts. 33 | Hello, Stranger! 34 | ``` 35 | -------------------------------------------------------------------------------- /Examples/retrying-middleware-example/Sources/HelloWorldURLSessionClient/HelloWorldURLSessionClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIURLSession 15 | import Foundation 16 | import RetryingClientMiddleware 17 | 18 | @main struct HelloWorldURLSessionClient { 19 | static func main() async throws { 20 | let client = Client( 21 | serverURL: URL(string: "http://localhost:8080/api")!, 22 | transport: URLSessionTransport(), 23 | middlewares: [ 24 | RetryingMiddleware( 25 | signals: [.code(429), .range(500..<600), .errorThrown], 26 | policy: .upToAttempts(count: 3), 27 | delay: .constant(seconds: 1) 28 | ) 29 | ] 30 | ) 31 | let response = try await client.getGreeting() 32 | print(try response.ok.body.json.message) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Examples/retrying-middleware-example/Sources/HelloWorldURLSessionClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/retrying-middleware-example/Sources/HelloWorldURLSessionClient/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/README.md: -------------------------------------------------------------------------------- 1 | # Common types between client and server modules 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | This example shows how you can structure a Swift package to share the types 10 | from an OpenAPI document between a client and server module by having a common 11 | target that runs the generator in `types` mode only. 12 | 13 | This allows you to write extensions or other helper functions that use these 14 | types and use them in both the client and server code. 15 | 16 | ## Usage 17 | 18 | Build and run the server using: 19 | 20 | ```console 21 | % swift run hello-world-server 22 | Build complete! 23 | ... 24 | info HummingBird : [HummingbirdCore] Server started and listening on 127.0.0.1:8080 25 | ``` 26 | 27 | Then, in another terminal window, run the client: 28 | 29 | ```console 30 | % swift run hello-world-client 31 | Build complete! 32 | +––––––––––––––––––+ 33 | |+––––––––––––––––+| 34 | ||Hello, Stranger!|| 35 | |+––––––––––––––––+| 36 | +––––––––––––––––––+ 37 | ``` 38 | 39 | Note how the message is boxed twice: once by the server and once by the client, 40 | both using an extension on a shared type, defined in the `Types` module. 41 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Client/HelloWorldURLSessionClient.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIURLSession 16 | import Foundation 17 | 18 | @main struct HelloWorldURLSessionClient { 19 | static func main() async throws { 20 | let client = Client(serverURL: URL(string: "http://localhost:8080/api")!, transport: URLSessionTransport()) 21 | let response = try await client.getGreeting() 22 | print(try response.ok.body.json.boxed().message) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Client/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - client 3 | accessModifier: internal 4 | namingStrategy: idiomatic 5 | additionalImports: 6 | - Types 7 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Client/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../openapi.yaml -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Server/HelloWorldHummingbirdServer.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIHummingbird 16 | import Hummingbird 17 | import Foundation 18 | import Types 19 | 20 | struct Handler: APIProtocol { 21 | func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output { 22 | let name = input.query.name ?? "Stranger" 23 | let message = Components.Schemas.Greeting(message: "Hello, \(name)!") 24 | return .ok(.init(body: .json(message.boxed()))) 25 | } 26 | } 27 | 28 | @main struct HelloWorldHummingbirdServer { 29 | static func main() async throws { 30 | let router = Router() 31 | let handler = Handler() 32 | try handler.registerHandlers(on: router, serverURL: URL(string: "/api")!) 33 | let app = Application(router: router, configuration: .init()) 34 | try await app.run() 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Server/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - server 3 | accessModifier: internal 4 | namingStrategy: idiomatic 5 | additionalImports: 6 | - Types 7 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Server/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../openapi.yaml -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Types/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | accessModifier: package 4 | namingStrategy: idiomatic 5 | -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/Types/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../openapi.yaml -------------------------------------------------------------------------------- /Examples/shared-types-client-server-example/Sources/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Swift", 3 | "image": "swift:6.1", 4 | "features": { 5 | "ghcr.io/devcontainers/features/common-utils:2": {}, 6 | "ghcr.io/devcontainers/features/git:1": {} 7 | }, 8 | "runArgs": [ 9 | "--cap-add=SYS_PTRACE", 10 | "--security-opt", 11 | "seccomp=unconfined" 12 | ], 13 | "customizations": { 14 | "vscode": { 15 | "settings": { 16 | "lldb.library": "/usr/lib/liblldb.so" 17 | }, 18 | "extensions": [ 19 | "swiftlang.swift-vscode", 20 | "42Crunch.vscode-openapi" 21 | ] 22 | } 23 | }, 24 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 25 | // "forwardPorts": [], 26 | 27 | // Set `remoteUser` to `root` to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 28 | "remoteUser": "root" 29 | } 30 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/.env.example: -------------------------------------------------------------------------------- 1 | # Rename this file to just .env and paste your token below. 2 | OPENAI_TOKEN=PASTE_YOUR_TOKEN_HERE 3 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build/ 3 | .build-linux/ 4 | .env 5 | /Packages 6 | /*.xcodeproj 7 | xcuserdata/ 8 | DerivedData/ 9 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 10 | .vscode 11 | /Package.resolved 12 | .ci/ 13 | .docc-build/ 14 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.exclude": { 3 | "**/.build": true, 4 | "**/.build-linux": true, 5 | "**/.gitignore": true, 6 | "**/.swiftpm": true, 7 | "**/.vscode": true, 8 | ".env": true, 9 | "*.o": true, 10 | "*.d": true, 11 | "*.swiftdeps": true, 12 | "*.swiftdeps~": true, 13 | }, 14 | "lldb.library": "/usr/lib/liblldb.so", 15 | "lldb.launch.expressions": "native", 16 | "swift.diagnosticsStyle": "default", 17 | "swift.disableAutoResolve": true, 18 | "swift.buildPath": ".build-linux", 19 | "workbench.startupEditor": "none" 20 | } 21 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ChatGPT/Middleware.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2025 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import Foundation 15 | import HTTPTypes 16 | import OpenAPIRuntime 17 | 18 | package struct HeaderFieldMiddleware: ClientMiddleware { 19 | var name: HTTPField.Name 20 | var value: String 21 | 22 | package static func authTokenFromEnvironment(_ environmentVariable: String) -> Self { 23 | guard let token = ProcessInfo.processInfo.environment[environmentVariable] else { 24 | fatalError("Please set \(environmentVariable) environment variable.") 25 | } 26 | return Self(name: .authorization, value: "Bearer \(token)") 27 | } 28 | 29 | package func intercept( 30 | _ request: HTTPRequest, 31 | body: HTTPBody?, 32 | baseURL: URL, 33 | operationID: String, 34 | next: @Sendable (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?) 35 | ) async throws -> (HTTPResponse, HTTPBody?) { 36 | var request = request 37 | request.headerFields[name] = value 38 | return try await next(request, body, baseURL) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ChatGPT/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: [types, client] 2 | accessModifier: package 3 | filter: 4 | paths: 5 | - /chat/completions 6 | namingStrategy: idiomatic -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ClientCLI/main.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2025 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIRuntime 15 | import OpenAPIURLSession 16 | import Foundation 17 | 18 | // Create a client. 19 | let client = Client( 20 | serverURL: URL(string: "http://localhost:8080")!, 21 | transport: URLSessionTransport() 22 | ) 23 | 24 | let userInput = CommandLine.arguments.dropFirst().first ?? "Any team!" 25 | 26 | // Print some placeholder to the console. 27 | setbuf(stdout, nil) // Don't buffer stdout. 28 | print("🧑‍💼: \(userInput)") 29 | print("---") 30 | print("🤖: ", terminator: "") 31 | 32 | // Make the request. 33 | let response = try await client.createChant( 34 | body: .json(.init(userInput: userInput)) 35 | ) 36 | 37 | // Decode JSON Lines into an async sequence of typed values. 38 | let messages = try response.ok.body.applicationJsonl 39 | .asDecodedJSONLines(of: Components.Schemas.ChantMessage.self) 40 | 41 | for try await message in messages { 42 | print(message.delta, terminator: "") 43 | } 44 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ClientCLI/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: [types, client] 2 | namingStrategy: idiomatic 3 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ClientCLI/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.0.3' 2 | info: 3 | title: ChantGPT 4 | version: 0.1.0 5 | paths: 6 | #---------------------------------------------------------- 7 | /chant: 8 | post: 9 | operationId: createChant 10 | summary: Generate a new chant for your favorite NBA team! 11 | tags: 12 | - Chants 13 | requestBody: 14 | required: true 15 | content: 16 | application/json: 17 | schema: 18 | $ref: "#/components/schemas/ChantRequest" 19 | responses: 20 | "200": 21 | description: A successful response with the generated chant. 22 | content: 23 | application/jsonl: 24 | schema: 25 | $ref: "#/components/schemas/ChantMessage" 26 | #---------------------------------------------------------- 27 | components: 28 | schemas: 29 | ChantRequest: 30 | type: object 31 | required: [userInput] 32 | properties: 33 | userInput: 34 | type: string 35 | example: 36 | userInput: The one with the bull logo. 37 | ChantMessage: 38 | type: object 39 | required: [delta] 40 | properties: 41 | delta: 42 | type: string 43 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ProxyServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: [types, server] 2 | namingStrategy: idiomatic 3 | -------------------------------------------------------------------------------- /Examples/streaming-chatgpt-proxy/Sources/ProxyServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.0.3' 2 | info: 3 | title: ChantGPT 4 | version: 0.1.0 5 | paths: 6 | #---------------------------------------------------------- 7 | /chant: 8 | post: 9 | operationId: createChant 10 | summary: Generate a new chant for your favorite NBA team! 11 | tags: 12 | - Chants 13 | requestBody: 14 | required: true 15 | content: 16 | application/json: 17 | schema: 18 | $ref: "#/components/schemas/ChantRequest" 19 | responses: 20 | "200": 21 | description: A successful response with the generated chant. 22 | content: 23 | application/jsonl: 24 | schema: 25 | $ref: "#/components/schemas/ChantMessage" 26 | #---------------------------------------------------------- 27 | components: 28 | schemas: 29 | ChantRequest: 30 | type: object 31 | required: [userInput] 32 | properties: 33 | userInput: 34 | type: string 35 | example: 36 | userInput: The one with the bull logo. 37 | ChantMessage: 38 | type: object 39 | required: [delta] 40 | properties: 41 | delta: 42 | type: string 43 | -------------------------------------------------------------------------------- /Examples/swagger-ui-endpoint-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/swagger-ui-endpoint-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "swagger-ui-endpoint-example", 19 | platforms: [.macOS(.v10_15)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"), 24 | .package(url: "https://github.com/vapor/vapor", from: "4.108.0"), 25 | ], 26 | targets: [ 27 | .executableTarget( 28 | name: "SwaggerUIEndpointsServer", 29 | dependencies: [ 30 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 31 | .product(name: "OpenAPIVapor", package: "swift-openapi-vapor"), 32 | .product(name: "Vapor", package: "vapor"), 33 | ], 34 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 35 | ) 36 | ] 37 | ) 38 | -------------------------------------------------------------------------------- /Examples/swagger-ui-endpoint-example/Public/openapi.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Swift OpenAPI Sample API 8 | 9 |
10 | 11 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Examples/swagger-ui-endpoint-example/Public/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: /api 7 | description: This server. 8 | - url: https://example.com/api 9 | description: Example service deployment. 10 | paths: 11 | /greet: 12 | get: 13 | operationId: getGreeting 14 | parameters: 15 | - name: name 16 | required: false 17 | in: query 18 | description: The name used in the returned greeting. 19 | schema: 20 | type: string 21 | responses: 22 | '200': 23 | description: A success response with a greeting. 24 | content: 25 | application/json: 26 | schema: 27 | $ref: '#/components/schemas/Greeting' 28 | components: 29 | schemas: 30 | Greeting: 31 | type: object 32 | description: A value with the greeting contents. 33 | properties: 34 | message: 35 | type: string 36 | description: The string representation of the greeting. 37 | required: 38 | - message 39 | -------------------------------------------------------------------------------- /Examples/swagger-ui-endpoint-example/Sources/SwaggerUIEndpointsServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/swagger-ui-endpoint-example/Sources/SwaggerUIEndpointsServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../../Public/openapi.yaml -------------------------------------------------------------------------------- /Examples/tracing-middleware-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/tracing-middleware-example/Sources/HelloWorldVaporServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/tracing-middleware-example/Sources/HelloWorldVaporServer/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | description: A value with the greeting contents. 31 | properties: 32 | message: 33 | type: string 34 | description: The string representation of the greeting. 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Examples/tracing-middleware-example/docker/collector-config.yaml: -------------------------------------------------------------------------------- 1 | receivers: 2 | otlp: 3 | protocols: 4 | grpc: 5 | endpoint: otel-collector:4317 6 | 7 | exporters: 8 | logging: 9 | verbosity: detailed 10 | jaeger: 11 | endpoint: "jaeger:14250" 12 | tls: 13 | insecure: true 14 | 15 | zipkin: 16 | endpoint: "http://zipkin:9411/api/v2/spans" 17 | 18 | service: 19 | pipelines: 20 | traces: 21 | receivers: otlp 22 | exporters: [logging, jaeger, zipkin] 23 | -------------------------------------------------------------------------------- /Examples/tracing-middleware-example/docker/docker-compose.yaml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | otel-collector: 4 | image: otel/opentelemetry-collector-contrib:latest 5 | command: ["--config=/etc/config.yaml"] 6 | volumes: 7 | - ./collector-config.yaml:/etc/config.yaml 8 | ports: 9 | - "4317:4317" 10 | networks: [exporter] 11 | depends_on: [zipkin, jaeger] 12 | 13 | jaeger: 14 | image: jaegertracing/all-in-one 15 | ports: 16 | - "16686:16686" 17 | networks: [exporter] 18 | 19 | zipkin: 20 | image: openzipkin/zipkin:latest 21 | ports: 22 | - "9411:9411" 23 | networks: [exporter] 24 | 25 | networks: 26 | exporter: 27 | -------------------------------------------------------------------------------- /Examples/various-content-types-client-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/various-content-types-client-example/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.10 2 | //===----------------------------------------------------------------------===// 3 | // 4 | // This source file is part of the SwiftOpenAPIGenerator open source project 5 | // 6 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 7 | // Licensed under Apache License v2.0 8 | // 9 | // See LICENSE.txt for license information 10 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 11 | // 12 | // SPDX-License-Identifier: Apache-2.0 13 | // 14 | //===----------------------------------------------------------------------===// 15 | import PackageDescription 16 | 17 | let package = Package( 18 | name: "various-content-types-client-example", 19 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 20 | dependencies: [ 21 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 22 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 23 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 24 | ], 25 | targets: [ 26 | .executableTarget( 27 | name: "ContentTypesClient", 28 | dependencies: [ 29 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 30 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 31 | ], 32 | plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] 33 | ) 34 | ] 35 | ) 36 | -------------------------------------------------------------------------------- /Examples/various-content-types-client-example/README.md: -------------------------------------------------------------------------------- 1 | # Client handling various content types 2 | 3 | An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). 4 | 5 | > **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. 6 | 7 | ## Overview 8 | 9 | A command-line tool that uses a generated client to show how to work with various HTTP content types. 10 | 11 | The tool uses the [URLSession](https://developer.apple.com/documentation/foundation/urlsession) API to perform the HTTP call, wrapped in the [Swift OpenAPI URLSession Transport](https://github.com/apple/swift-openapi-urlsession). 12 | 13 | The server can be started by running the `various-content-types-server-example` example locally. 14 | 15 | ## Usage 16 | 17 | Build and run the client CLI using: 18 | 19 | ```console 20 | % swift run 21 | ``` 22 | -------------------------------------------------------------------------------- /Examples/various-content-types-client-example/Sources/ContentTypesClient/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /Examples/various-content-types-server-example/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .vscode 9 | /Package.resolved 10 | .ci/ 11 | .docc-build/ 12 | -------------------------------------------------------------------------------- /Examples/various-content-types-server-example/Sources/ContentTypesServer/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | namingStrategy: idiomatic 6 | -------------------------------------------------------------------------------- /IntegrationTest/.gitignore: -------------------------------------------------------------------------------- 1 | # From running `swift package init`: 2 | .DS_Store 3 | /.build 4 | /Packages 5 | /*.xcodeproj 6 | xcuserdata/ 7 | DerivedData/ 8 | .swiftpm/config/registries.json 9 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 10 | .netrc 11 | 12 | # We explicitly _don't_ want to pin the versions of the dependencies 13 | /Package.resolved 14 | -------------------------------------------------------------------------------- /IntegrationTest/README.md: -------------------------------------------------------------------------------- 1 | # Integration Test 2 | 3 | A Swift package used as an integration test in the Swift OpenAPI Generator ecosystem. 4 | 5 | ## Usage 6 | 7 | This package can be used when testing changes in another project. 8 | 9 | For example, from the pull request pipeline for that project, you can do the following: 10 | 11 | ```console 12 | % git clone https://github.com/apple/swift-openapi-generator 13 | % cd swift-openapi-generator/IntegrationTests 14 | % swift package edit swift-openapi-runtime path/to/checkout/of/swift-openapi-runtime 15 | % swift build 16 | ``` 17 | 18 | If you're working manually on this, you may wish to reset any overrides, you 19 | can do this using the following command: 20 | 21 | ```console 22 | % swift package reset 23 | ``` 24 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Client/Empty.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Client/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - client 3 | additionalImports: 4 | - Types 5 | accessModifier: package 6 | namingStrategy: idiomatic 7 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Client/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../openapi.yaml -------------------------------------------------------------------------------- /IntegrationTest/Sources/MockTransportClient/Client.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import Types 15 | import Client 16 | import OpenAPIRuntime 17 | import Foundation 18 | import HTTPTypes 19 | 20 | struct MockClientTransport: ClientTransport { 21 | func send(_ request: HTTPTypes.HTTPRequest, body: OpenAPIRuntime.HTTPBody?, baseURL: URL, operationID: String) 22 | async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) 23 | { (HTTPTypes.HTTPResponse(status: 200), nil) } 24 | } 25 | 26 | func run() async throws { 27 | let client = Client(serverURL: try Servers.Server1.url(), transport: MockClientTransport()) 28 | _ = try await client.getGreeting(.init()) 29 | } 30 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/MockTransportServer/Server.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import Types 15 | import Server 16 | import OpenAPIRuntime 17 | import HTTPTypes 18 | 19 | actor SimpleAPIImpl: APIProtocol { 20 | func getGreeting(_ input: Operations.GetGreeting.Input) async throws -> Operations.GetGreeting.Output { 21 | let message = "Hello, \(input.query.name ?? "Stranger")!" 22 | return .ok(.init(body: .json(.init(message: message)))) 23 | } 24 | } 25 | 26 | class MockServerTransport: ServerTransport { 27 | typealias Handler = @Sendable ( 28 | HTTPTypes.HTTPRequest, OpenAPIRuntime.HTTPBody?, OpenAPIRuntime.ServerRequestMetadata 29 | ) async throws -> (HTTPTypes.HTTPResponse, OpenAPIRuntime.HTTPBody?) 30 | 31 | func register(_ handler: @escaping Handler, method: HTTPTypes.HTTPRequest.Method, path: String) throws { 32 | // noop. 33 | } 34 | } 35 | 36 | func initializeServer() throws { 37 | let handler = SimpleAPIImpl() 38 | let transport = MockServerTransport() 39 | try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url()) 40 | } 41 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Server/Empty.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Server/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - server 3 | additionalImports: 4 | - Types 5 | accessModifier: package 6 | namingStrategy: idiomatic 7 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Server/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../openapi.yaml -------------------------------------------------------------------------------- /IntegrationTest/Sources/Types/Empty.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Types/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | accessModifier: package 4 | namingStrategy: idiomatic 5 | -------------------------------------------------------------------------------- /IntegrationTest/Sources/Types/openapi.yaml: -------------------------------------------------------------------------------- 1 | ../openapi.yaml -------------------------------------------------------------------------------- /IntegrationTest/Sources/openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: "3.1.0" 2 | info: 3 | title: "GreetingService" 4 | version: "1.0.0" 5 | servers: 6 | - url: "https://example.com/api" 7 | description: "Example" 8 | paths: 9 | /greet: 10 | get: 11 | tags: ["Greetings"] 12 | operationId: "getGreeting" 13 | parameters: 14 | - name: "name" 15 | required: false 16 | in: "query" 17 | description: "name" 18 | schema: 19 | type: "string" 20 | responses: 21 | "200": 22 | description: "Returns a greeting" 23 | content: 24 | application/json: 25 | schema: 26 | $ref: "#/components/schemas/Greeting" 27 | components: 28 | schemas: 29 | Greeting: 30 | type: "object" 31 | properties: 32 | message: 33 | type: string 34 | required: 35 | - message 36 | -------------------------------------------------------------------------------- /Plugins/OpenAPIGenerator/PluginsShared: -------------------------------------------------------------------------------- 1 | ../../Plugins/PluginsShared -------------------------------------------------------------------------------- /Plugins/OpenAPIGeneratorCommand/PluginsShared: -------------------------------------------------------------------------------- 1 | ../../Plugins/PluginsShared -------------------------------------------------------------------------------- /Plugins/PluginsShared/GeneratorMode.swift: -------------------------------------------------------------------------------- 1 | ../../Sources/_OpenAPIGeneratorCore/GeneratorMode.swift -------------------------------------------------------------------------------- /Plugins/PluginsShared/PluginSource.swift: -------------------------------------------------------------------------------- 1 | ../../Sources/swift-openapi-generator/PluginSource.swift -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Documentation.docc/Documentation.md: -------------------------------------------------------------------------------- 1 | # ``_OpenAPIGeneratorCore`` 2 | 3 | Invoke the core logic of the Swift OpenAPI Generator tool and plugin. 4 | 5 | ## Overview 6 | 7 | This library contains the core logic powering the Swift OpenAPI Generator command-line tool and SwiftPM plugin. 8 | 9 | It is an implementation detail of the CLI and the plugin, and should *not* be used directly by users. 10 | 11 | > Important: Since this library is not meant to be use directly by users, API stability is not guaranteed. 12 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Errors.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import Foundation 15 | 16 | /// An error representing a fatal issue encountered by the generator. 17 | /// 18 | /// Use sparingly, as recoverable issues should instead be emitted as 19 | /// a ``Diagnostic`` into a ``DiagnosticCollector``. 20 | struct GenericError: Error { 21 | 22 | /// The message describing the issue. 23 | var message: String 24 | } 25 | 26 | extension GenericError: CustomStringConvertible { var description: String { message } } 27 | 28 | extension GenericError: LocalizedError { var errorDescription: String? { description } } 29 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Extensions/Foundation.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import Foundation 15 | 16 | #if $RetroactiveAttribute 17 | extension FileHandle: @retroactive TextOutputStream {} 18 | #else 19 | extension FileHandle: TextOutputStream {} 20 | #endif 21 | 22 | extension InMemoryInputFile { 23 | /// Creates a new in-memory file by reading the contents at the specified path. 24 | /// - Parameter url: The path to the file to read. 25 | /// - Throws: An error if there's an issue reading the file or initializing the in-memory file. 26 | init(fromFileAt url: URL) throws { try self.init(absolutePath: url, contents: Data(contentsOf: url)) } 27 | } 28 | 29 | /// File handle to stderr. 30 | let stdErrHandle = FileHandle.standardError 31 | 32 | extension FileHandle { 33 | /// Writes the given string to the file handle. 34 | /// 35 | /// This method writes the provided string to the file handle using its UTF-8 36 | /// representation. 37 | /// 38 | /// - Parameter string: The string to be written to the file handle. 39 | public func write(_ string: String) { write(Data(string.utf8)) } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/FeatureFlags.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | /// A feature that can be explicitly enabled before being released. 16 | /// 17 | /// Commonly used to get early feedback on breaking changes, before 18 | /// they are enabled by default, which can only be done in a major version. 19 | /// 20 | /// Once a feature is enabled unconditionally in the next major version, 21 | /// the corresponding feature flag should be removed at the same time. 22 | /// 23 | /// For example: a breaking feature is being built while version 0.1 is out, 24 | /// and is hidden behind a feature flag. Once ready, the feature is 25 | /// enabled unconditionally on main and the feature flag removed, and version 26 | /// 0.2 is tagged. (This is for pre-1.0 versioning, would be 1.0 and 2.0 after 27 | /// 1.0 is released.) 28 | public enum FeatureFlag: String, Hashable, Codable, CaseIterable, Sendable { 29 | // needs to be here for the enum to compile 30 | case empty 31 | } 32 | 33 | /// A set of enabled feature flags. 34 | public typealias FeatureFlags = Set 35 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Layers/ParsedOpenAPIRepresentation.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIKit 15 | 16 | /// An OpenAPIKit document that contains the operations and types for which the generator emits Swift types. 17 | typealias ParsedOpenAPIRepresentation = OpenAPI.Document 18 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Parser/ParserProtocol.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | /// An object that parses a provided raw file as an OpenAPI document 16 | /// and validates the contents of the document in the process. 17 | /// 18 | /// Parsing is the first phase in the generator pipeline. 19 | protocol ParserProtocol { 20 | 21 | /// Parses the specified raw file as an OpenAPI document. 22 | /// - Parameters: 23 | /// - input: The raw contents of the document. 24 | /// - config: The configuration of the generator. 25 | /// - diagnostics: The collector to which to emit diagnostics. 26 | /// - Returns: A parsed and validated OpenAPI document. 27 | /// - Throws: An error if an issue occurs during parsing or validation. 28 | func parseOpenAPI(_ input: InMemoryInputFile, config: Config, diagnostics: any DiagnosticCollector) throws 29 | -> ParsedOpenAPIRepresentation 30 | } 31 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/PlatformChecks.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | // Emit a compiler error if this library is linked with a target in an adopter 16 | // project. 17 | // This is only done for platforms where the linkage was most likely added 18 | // erroneously (for platforms which can't be used as development hosts). 19 | #if (os(iOS) && !targetEnvironment(macCatalyst)) || os(tvOS) || os(watchOS) || os(visionOS) 20 | #error( 21 | "_OpenAPIGeneratorCore is only to be used by swift-openapi-generator itself—your target should not link this library or the command line tool directly." 22 | ) 23 | #endif 24 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Renderer/RendererProtocol.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | /// An object that renders structured Swift representations 16 | /// into Swift files. 17 | /// 18 | /// Rendering is the last phase of the generator pipeline. 19 | protocol RendererProtocol { 20 | 21 | /// Renders the specified structured code into a raw Swift file. 22 | /// - Parameters: 23 | /// - code: A structured representation of the Swift code. 24 | /// - config: The configuration of the generator. 25 | /// - diagnostics: The collector to which to emit diagnostics. 26 | /// - Returns: A raw file with Swift contents. 27 | /// - Throws: An error if an issue occurs during rendering. 28 | func render(structured code: StructuredSwiftRepresentation, config: Config, diagnostics: any DiagnosticCollector) 29 | throws -> InMemoryOutputFile 30 | } 31 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Translator/CommonTypes/SchemaOverrides.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIKit 15 | 16 | /// A container of properties that can be defined at multiple levels in 17 | /// the OpenAPI document. If a property is filled in, the value is used instead 18 | /// of inspecting a matching property one level deeper. 19 | /// 20 | /// One example is an OpenAPI parameter, which wraps a JSON schema. Both the 21 | /// parameter and the schema can have a user-specified description. However if 22 | /// both are specified, the parameter description is used, as it better matches 23 | /// the role of the value, rather than the type of the value. However, if no 24 | /// property description is specified, the generator uses the schema value. 25 | struct SchemaOverrides { 26 | 27 | /// A Boolean value indicating whether the object is optional. 28 | var isOptional: Bool? 29 | 30 | /// A user-specified description from the OpenAPI document. 31 | var userDescription: String? 32 | 33 | /// Returns an empty overrides container. 34 | static var none: Self { .init(isOptional: nil, userDescription: nil) } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Translator/Content/CodingStrategy.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | /// Describes the underlying coding strategy. 16 | enum CodingStrategy: String, Hashable, Sendable { 17 | 18 | /// A strategy using JSONEncoder/JSONDecoder. 19 | case json 20 | 21 | /// A strategy using URIEncoder/URIDecoder. 22 | case uri 23 | 24 | /// A strategy that passes through the data unmodified. 25 | case binary 26 | 27 | /// A strategy using x-www-form-urlencoded. 28 | case urlEncodedForm 29 | 30 | /// A strategy using multipart/form-data. 31 | case multipart 32 | 33 | /// The name of the coding strategy in the runtime library. 34 | var runtimeName: String { 35 | switch self { 36 | case .json: return Constants.CodingStrategy.json 37 | case .uri: return Constants.CodingStrategy.uri 38 | case .binary: return Constants.CodingStrategy.binary 39 | case .urlEncodedForm: return Constants.CodingStrategy.urlEncodedForm 40 | case .multipart: return Constants.CodingStrategy.multipart 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Translator/FileTranslator+FeatureFlags.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIKit 15 | 16 | extension FileTranslator { 17 | // Add helpers for reading feature flags below. 18 | } 19 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Translator/Responses/acceptHeaderContentTypes.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIKit 15 | import Algorithms 16 | 17 | extension FileTranslator { 18 | 19 | /// Returns a list of candidate content-types from all responses in 20 | /// the specified operation. 21 | /// 22 | /// - Parameter description: The OpenAPI operation. 23 | /// - Returns: A list of content types. Might be empty, in which case no 24 | /// Accept header should be sent in the request. 25 | /// - Throws: Any errors that occur during the process of analyzing the responses. 26 | func acceptHeaderContentTypes(for description: OperationDescription) throws -> [ContentType] { 27 | let contentTypes = try description.responseOutcomes 28 | .flatMap { outcome in 29 | let response = try outcome.response.resolve(in: components) 30 | return try supportedContents(response.content, isRequired: true, foundIn: description.operationID) 31 | } 32 | .map { content in content.contentType } 33 | return Array(contentTypes.uniqued()) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Translator/TranslatorProtocol.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIKit 15 | 16 | /// An object that turns an OpenAPI document into the structured Swift 17 | /// representation of the request generated file: types, client, or server. 18 | protocol TranslatorProtocol { 19 | 20 | /// Translates the provided OpenAPI document into Swift code. 21 | /// - Parameters: 22 | /// - parsedOpenAPI: The input OpenAPI document. 23 | /// - config: A set of configuration values for the generator, such as 24 | /// which file to generate code for. 25 | /// - diagnostics: The collector to which the translator emits 26 | /// diagnostics. 27 | /// - Returns: A structured Swift representation of the generated code. 28 | /// - Throws: An error if there are issues parsing or translating the request body. 29 | func translate(parsedOpenAPI: ParsedOpenAPIRepresentation, config: Config, diagnostics: any DiagnosticCollector) 30 | throws -> StructuredSwiftRepresentation 31 | } 32 | -------------------------------------------------------------------------------- /Sources/_OpenAPIGeneratorCore/Translator/TypeAssignment/TypeLocation.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import OpenAPIKit 15 | 16 | /// Describes the location of a name type in the OpenAPI document. 17 | enum TypeLocation { 18 | 19 | /// A schema type. 20 | case schemas 21 | 22 | /// A parameter type. 23 | case parameters 24 | } 25 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Development/Documentation-for-maintainers.md: -------------------------------------------------------------------------------- 1 | # Documentation for maintainers 2 | 3 | Learn about the internals of Swift OpenAPI Generator. 4 | 5 | ## Overview 6 | 7 | Swift OpenAPI Generator contains multiple moving pieces, from the runtime library, to the generator CLI, plugin, to extension packages using the transport and middleware APIs. 8 | 9 | Use the resources below if you'd like to learn more about how the generator works under the hood, for example as part of contributing a new feature to it. 10 | 11 | ## Topics 12 | 13 | - 14 | - 15 | - 16 | - 17 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CDDefaultCodeListingLanguage 6 | swift 7 | CFBundleName 8 | swift-openapi-generator 9 | CFBundleDisplayName 10 | swift-openapi-generator 11 | CFBundleIdentifier 12 | com.apple.OpenAPIGenerator 13 | CFBundlePackageType 14 | DOCS 15 | CDDefaultModuleKind 16 | Tool 17 | 18 | 19 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.Package.0.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingServiceClient", 6 | targets: [ 7 | .executableTarget( 8 | name: "GreetingServiceClient" 9 | ) 10 | ] 11 | ) 12 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.Package.1.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingServiceClient", 6 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 7 | targets: [ 8 | .executableTarget( 9 | name: "GreetingServiceClient" 10 | ) 11 | ] 12 | ) 13 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.Package.2.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingServiceClient", 6 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 7 | dependencies: [ 8 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 9 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 10 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 11 | ], 12 | targets: [ 13 | .executableTarget( 14 | name: "GreetingServiceClient" 15 | ) 16 | ] 17 | ) 18 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.Package.3.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingServiceClient", 6 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 7 | dependencies: [ 8 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 9 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 10 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 11 | ], 12 | targets: [ 13 | .executableTarget( 14 | name: "GreetingServiceClient", 15 | plugins: [ 16 | .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"), 17 | ] 18 | ) 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.Package.4.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingServiceClient", 6 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 7 | dependencies: [ 8 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 9 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 10 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 11 | ], 12 | targets: [ 13 | .executableTarget( 14 | name: "GreetingServiceClient", 15 | dependencies: [ 16 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 17 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 18 | ], 19 | plugins: [ 20 | .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"), 21 | ] 22 | ) 23 | ] 24 | ) 25 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.Package.5.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingServiceClient", 6 | platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .visionOS(.v1)], 7 | dependencies: [ 8 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 9 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 10 | .package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.0"), 11 | ], 12 | targets: [ 13 | .executableTarget( 14 | name: "GreetingServiceClient", 15 | dependencies: [ 16 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 17 | .product(name: "OpenAPIURLSession", package: "swift-openapi-urlsession"), 18 | ], 19 | plugins: [ 20 | .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"), 21 | ] 22 | ) 23 | ] 24 | ) 25 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.1.0.txt: -------------------------------------------------------------------------------- 1 | % git clone https://github.com/apple/swift-openapi-generator 2 | % cd swift-openapi-generator/Examples/hello-world-vapor-server-example 3 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.1.1.txt: -------------------------------------------------------------------------------- 1 | % git clone https://github.com/apple/swift-openapi-generator 2 | % cd swift-openapi-generator/Examples/hello-world-vapor-server-example 3 | 4 | % swift run HelloWorldVaporServer 5 | .. 6 | Build complete! (37.91s) 7 | 2023-12-12T09:06:32+0100 notice codes.vapor.application : [Vapor] Server starting on http://127.0.0.1:8080 8 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.1.2.txt: -------------------------------------------------------------------------------- 1 | % git clone https://github.com/apple/swift-openapi-generator 2 | % cd swift-openapi-generator/Examples/hello-world-vapor-server-example 3 | 4 | % swift run HelloWorldVaporServer 5 | .. 6 | Build complete! (37.91s) 7 | 2023-12-12T09:06:32+0100 notice codes.vapor.application : [Vapor] Server starting on http://127.0.0.1:8080 8 | 9 | % curl 'localhost:8080/api/greet?name=Jane' 10 | { 11 | "message" : "Hello, Jane" 12 | } 13 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.2.0.txt: -------------------------------------------------------------------------------- 1 | % mkdir GreetingServiceClient 2 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.2.1.txt: -------------------------------------------------------------------------------- 1 | % mkdir GreetingServiceClient 2 | 3 | % swift package --package-path GreetingServiceClient init --type executable 4 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.2.2.txt: -------------------------------------------------------------------------------- 1 | % mkdir GreetingServiceClient 2 | 3 | % swift package --package-path GreetingServiceClient init --type executable 4 | 5 | % open GreetingServiceClient/Package.swift 6 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.3.0.txt: -------------------------------------------------------------------------------- 1 | % swift run --package-path GreetingServiceClient 2 | 3 | ok(GreetingServiceClient.Operations.GetGreeting.Output.Ok( 4 | headers: GreetingServiceClient.Operations.GetGreeting.Output.Ok.Headers(), 5 | body: GreetingServiceClient.Operations.GetGreeting.Output.Ok.Body.json( 6 | GreetingServiceClient.Components.Schemas.Greeting(message: "Hello, CLI")))) 7 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.console.4.0.txt: -------------------------------------------------------------------------------- 1 | % swift run --package-path GreetingServiceClient 2 | Hello, CLI! 3 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.0.swift: -------------------------------------------------------------------------------- 1 | // The Swift Programming Language 2 | // https://docs.swift.org/swift-book 3 | 4 | print("Hello, world!") 5 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.1.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.2.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | 4 | let client = Client( 5 | serverURL: try Servers.Server2.url(), 6 | transport: URLSessionTransport() 7 | ) 8 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.3.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | 4 | let client = Client( 5 | serverURL: try Servers.Server2.url(), 6 | transport: URLSessionTransport() 7 | ) 8 | 9 | let response = try await client.getGreeting(query: .init(name: "CLI")) 10 | print(response) 11 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.4.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | 4 | let client = Client( 5 | serverURL: try Servers.Server2.url(), 6 | transport: URLSessionTransport() 7 | ) 8 | 9 | let response = try await client.getGreeting(query: .init(name: "CLI")) 10 | switch response { 11 | case .ok(let okResponse): 12 | print(okResponse) 13 | } 14 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.5.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | 4 | let client = Client( 5 | serverURL: try Servers.Server2.url(), 6 | transport: URLSessionTransport() 7 | ) 8 | 9 | let response = try await client.getGreeting(query: .init(name: "CLI")) 10 | switch response { 11 | case .ok(let okResponse): 12 | print(okResponse) 13 | case .undocumented(statusCode: let statusCode, _): 14 | print("🥺 undocumented response: \(statusCode)") 15 | } 16 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.6.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | 4 | let client = Client( 5 | serverURL: try Servers.Server2.url(), 6 | transport: URLSessionTransport() 7 | ) 8 | 9 | let response = try await client.getGreeting(query: .init(name: "CLI")) 10 | switch response { 11 | case .ok(let okResponse): 12 | switch okResponse.body { 13 | case .json(let greeting): 14 | print(greeting.message) 15 | } 16 | case .undocumented(statusCode: let statusCode, _): 17 | print("🥺 undocumented response: \(statusCode)") 18 | } 19 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.main.7.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIRuntime 2 | import OpenAPIURLSession 3 | 4 | let client = Client( 5 | serverURL: try Servers.Server2.url(), 6 | transport: URLSessionTransport() 7 | ) 8 | 9 | let response = try await client.getGreeting(query: .init(name: "CLI")) 10 | print(try response.ok.body.json.message) 11 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - client 4 | namingStrategy: idiomatic 5 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.openapi.2.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | - url: http://127.0.0.1:8080/api 9 | description: Localhost deployment. 10 | paths: 11 | /greet: 12 | get: 13 | operationId: getGreeting 14 | parameters: 15 | - name: name 16 | required: false 17 | in: query 18 | description: The name used in the returned greeting. 19 | schema: 20 | type: string 21 | responses: 22 | '200': 23 | description: A success response with a greeting. 24 | content: 25 | application/json: 26 | schema: 27 | $ref: '#/components/schemas/Greeting' 28 | components: 29 | schemas: 30 | Greeting: 31 | type: object 32 | properties: 33 | message: 34 | type: string 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.openapi.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | properties: 31 | message: 32 | type: string 33 | required: 34 | - message 35 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.0.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.1.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.2.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | let client = Client( 9 | serverURL: try Servers.Server2.url(), 10 | transport: URLSessionTransport() 11 | ) 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.3.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | let client = Client( 9 | serverURL: try Servers.Server2.url(), 10 | transport: URLSessionTransport() 11 | ) 12 | let response = try await client.getGreeting(query: .init(name: name)) 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.4.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | let client = Client( 9 | serverURL: try Servers.Server2.url(), 10 | transport: URLSessionTransport() 11 | ) 12 | let response = try await client.getGreeting(query: .init(name: name)) 13 | switch response { 14 | case .ok(let okResponse): 15 | print(okResponse) 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.5.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | let client = Client( 9 | serverURL: try Servers.Server2.url(), 10 | transport: URLSessionTransport() 11 | ) 12 | let response = try await client.getGreeting(query: .init(name: name)) 13 | switch response { 14 | case .ok(let okResponse): 15 | print(okResponse) 16 | case .undocumented(statusCode: let statusCode, _): 17 | return "🙉 \(statusCode)" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.6.2.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | let client = Client( 9 | serverURL: try Servers.Server2.url(), 10 | transport: URLSessionTransport() 11 | ) 12 | let response = try await client.getGreeting(query: .init(name: name)) 13 | return try response.ok.body.json.message 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.6.swift: -------------------------------------------------------------------------------- 1 | import OpenAPIURLSession 2 | 3 | public struct GreetingClient { 4 | 5 | public init() {} 6 | 7 | public func getGreeting(name: String?) async throws -> String { 8 | let client = Client( 9 | serverURL: try Servers.Server2.url(), 10 | transport: URLSessionTransport() 11 | ) 12 | let response = try await client.getGreeting(query: .init(name: name)) 13 | switch response { 14 | case .ok(let okResponse): 15 | switch okResponse.body { 16 | case .json(let greeting): 17 | return greeting.message 18 | } 19 | case .undocumented(statusCode: let statusCode, _): 20 | return "🙉 \(statusCode)" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/client.xcode.7.swift: -------------------------------------------------------------------------------- 1 | let greeting = try await GreetingClient().getGreeting(name: "App") 2 | // Display the greeting text in the UI. 3 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.0.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.1.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.2.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.3.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.4.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.5.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.6.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | properties: 31 | message: 32 | type: string 33 | required: 34 | - message 35 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/exploring-openapi.openapi.7.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | properties: 31 | message: 32 | type: string 33 | required: 34 | - message 35 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple/swift-openapi-generator/09ed6c489f88d06228a404a0313adeea743e6cad/Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/image.png -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.console.0.txt: -------------------------------------------------------------------------------- 1 | % mkdir Public 2 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.console.1.txt: -------------------------------------------------------------------------------- 1 | % mkdir Public 2 | 3 | % mv Sources/openapi.yaml Public/ 4 | 5 | % ln -s ../Public/openapi.yaml Sources/openapi.yaml 6 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.console.2.txt: -------------------------------------------------------------------------------- 1 | % mkdir Public 2 | 3 | % mv Sources/openapi.yaml Public/ 4 | 5 | % ln -s ../Public/openapi.yaml Sources/openapi.yaml 6 | 7 | % curl "localhost:8080/openapi.yaml" 8 | openapi: '3.1.0' 9 | info: 10 | title: GreetingService 11 | version: 1.0.0 12 | servers: 13 | - url: https://example.com/api 14 | description: Example service deployment. 15 | paths: 16 | /greet: 17 | get: 18 | operationId: getGreeting 19 | parameters: 20 | - name: name 21 | ---[SNIP]--- 22 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.main.0.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Vapor 3 | import OpenAPIRuntime 4 | import OpenAPIVapor 5 | 6 | // Define a type that conforms to the generated protocol. 7 | struct GreetingServiceAPIImpl: APIProtocol { 8 | func getGreeting( 9 | _ input: Operations.GetGreeting.Input 10 | ) async throws -> Operations.GetGreeting.Output { 11 | let name = input.query.name ?? "Stranger" 12 | let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!") 13 | return .ok(.init(body: .json(greeting))) 14 | } 15 | } 16 | 17 | // Create your Vapor application. 18 | let app = try await Vapor.Application.make() 19 | 20 | // Create a VaporTransport using your application. 21 | let transport = VaporTransport(routesBuilder: app) 22 | 23 | // Create an instance of your handler type that conforms the generated protocol 24 | // defining your service API. 25 | let handler = GreetingServiceAPIImpl() 26 | 27 | // Call the generated function on your implementation to add its request 28 | // handlers to the app. 29 | try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url()) 30 | 31 | // Start the app as you would normally. 32 | try await app.execute() 33 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.main.1.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Vapor 3 | import OpenAPIRuntime 4 | import OpenAPIVapor 5 | 6 | // Define a type that conforms to the generated protocol. 7 | struct GreetingServiceAPIImpl: APIProtocol { 8 | func getGreeting( 9 | _ input: Operations.GetGreeting.Input 10 | ) async throws -> Operations.GetGreeting.Output { 11 | let name = input.query.name ?? "Stranger" 12 | let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!") 13 | return .ok(.init(body: .json(greeting))) 14 | } 15 | } 16 | 17 | // Create your Vapor application. 18 | let app = try await Vapor.Application.make() 19 | 20 | // Create a VaporTransport using your application. 21 | let transport = VaporTransport(routesBuilder: app) 22 | 23 | // Create an instance of your handler type that conforms the generated protocol 24 | // defining your service API. 25 | let handler = GreetingServiceAPIImpl() 26 | 27 | // Call the generated function on your implementation to add its request 28 | // handlers to the app. 29 | try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url()) 30 | 31 | // Add Vapor middleware to serve the contents of the Public/ directory. 32 | app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) 33 | 34 | // Start the app as you would normally. 35 | try await app.execute() 36 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.main.2.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Vapor 3 | import OpenAPIRuntime 4 | import OpenAPIVapor 5 | 6 | // Define a type that conforms to the generated protocol. 7 | struct GreetingServiceAPIImpl: APIProtocol { 8 | func getGreeting( 9 | _ input: Operations.GetGreeting.Input 10 | ) async throws -> Operations.GetGreeting.Output { 11 | let name = input.query.name ?? "Stranger" 12 | let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!") 13 | return .ok(.init(body: .json(greeting))) 14 | } 15 | } 16 | 17 | // Create your Vapor application. 18 | let app = try await Vapor.Application.make() 19 | 20 | // Create a VaporTransport using your application. 21 | let transport = VaporTransport(routesBuilder: app) 22 | 23 | // Create an instance of your handler type that conforms the generated protocol 24 | // defining your service API. 25 | let handler = GreetingServiceAPIImpl() 26 | 27 | // Call the generated function on your implementation to add its request 28 | // handlers to the app. 29 | try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url()) 30 | 31 | // Add Vapor middleware to serve the contents of the Public/ directory. 32 | app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) 33 | 34 | // Redirect /openapi to openapi.html, which serves the rendered documentation. 35 | app.get("openapi") { $0.redirect(to: "/openapi.html", redirectType: .permanent) } 36 | 37 | // Start the app as you would normally. 38 | try await app.execute() 39 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.openapi.0.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | properties: 31 | message: 32 | type: string 33 | required: 34 | - message 35 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.openapi.1.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | - url: /api 9 | description: The server hosting this document. 10 | paths: 11 | /greet: 12 | get: 13 | operationId: getGreeting 14 | parameters: 15 | - name: name 16 | required: false 17 | in: query 18 | description: The name used in the returned greeting. 19 | schema: 20 | type: string 21 | responses: 22 | '200': 23 | description: A success response with a greeting. 24 | content: 25 | application/json: 26 | schema: 27 | $ref: '#/components/schemas/Greeting' 28 | components: 29 | schemas: 30 | Greeting: 31 | type: object 32 | properties: 33 | message: 34 | type: string 35 | required: 36 | - message 37 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server-openapi-endpoints.openapi.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Swift OpenAPI Sample API 8 | 9 |
10 | 11 | 21 | 22 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.Package.0.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingService", 6 | targets: [ 7 | .executableTarget( 8 | name: "GreetingService" 9 | ) 10 | ] 11 | ) 12 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.Package.1.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingService", 6 | platforms: [ 7 | .macOS(.v10_15) 8 | ], 9 | targets: [ 10 | .executableTarget( 11 | name: "GreetingService" 12 | ) 13 | ] 14 | ) 15 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.Package.2.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingService", 6 | platforms: [ 7 | .macOS(.v10_15) 8 | ], 9 | dependencies: [ 10 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 11 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 12 | .package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"), 13 | .package(url: "https://github.com/vapor/vapor", from: "4.89.0"), 14 | ], 15 | targets: [ 16 | .executableTarget( 17 | name: "GreetingService" 18 | ) 19 | ] 20 | ) 21 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.Package.3.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingService", 6 | platforms: [ 7 | .macOS(.v10_15) 8 | ], 9 | dependencies: [ 10 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 11 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 12 | .package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"), 13 | .package(url: "https://github.com/vapor/vapor", from: "4.89.0"), 14 | ], 15 | targets: [ 16 | .executableTarget( 17 | name: "GreetingService", 18 | plugins: [ 19 | .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"), 20 | ] 21 | ) 22 | ] 23 | ) 24 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.Package.4.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingService", 6 | platforms: [ 7 | .macOS(.v10_15) 8 | ], 9 | dependencies: [ 10 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 11 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 12 | .package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"), 13 | .package(url: "https://github.com/vapor/vapor", from: "4.89.0"), 14 | ], 15 | targets: [ 16 | .executableTarget( 17 | name: "GreetingService", 18 | dependencies: [ 19 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 20 | .product(name: "OpenAPIVapor", package: "swift-openapi-vapor"), 21 | .product(name: "Vapor", package: "vapor"), 22 | ], 23 | plugins: [ 24 | .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"), 25 | ] 26 | ) 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.Package.5.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "GreetingService", 6 | platforms: [ 7 | .macOS(.v10_15) 8 | ], 9 | dependencies: [ 10 | .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), 11 | .package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), 12 | .package(url: "https://github.com/swift-server/swift-openapi-vapor", from: "1.0.0"), 13 | .package(url: "https://github.com/vapor/vapor", from: "4.89.0"), 14 | ], 15 | targets: [ 16 | .executableTarget( 17 | name: "GreetingService", 18 | dependencies: [ 19 | .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"), 20 | .product(name: "OpenAPIVapor", package: "swift-openapi-vapor"), 21 | .product(name: "Vapor", package: "vapor"), 22 | ], 23 | plugins: [ 24 | .plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator"), 25 | ] 26 | ) 27 | ] 28 | ) 29 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.console.1.0.txt: -------------------------------------------------------------------------------- 1 | % mkdir GreetingService 2 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.console.1.1.txt: -------------------------------------------------------------------------------- 1 | % mkdir GreetingService 2 | 3 | % swift package --package-path GreetingService init --type executable 4 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.console.1.2.txt: -------------------------------------------------------------------------------- 1 | % mkdir GreetingService 2 | 3 | % swift package --package-path GreetingService init --type executable 4 | 5 | % open GreetingService/Package.swift 6 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.console.2.txt: -------------------------------------------------------------------------------- 1 | % curl 'http://localhost:8080/api/greet?name=Jane' 2 | { 3 | "message" : "Hello, Jane!" 4 | } 5 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.console.3.txt: -------------------------------------------------------------------------------- 1 | % curl http://localhost:8080/api/emoji 2 | "👋" 3 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.main.0.swift: -------------------------------------------------------------------------------- 1 | // The Swift Programming Language 2 | // https://docs.swift.org/swift-book 3 | 4 | print("Hello, world!") 5 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.main.1.1.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Vapor 3 | import OpenAPIRuntime 4 | import OpenAPIVapor 5 | 6 | // Define a type that conforms to the generated protocol. 7 | struct GreetingServiceAPIImpl: APIProtocol { 8 | func getGreeting( 9 | _ input: Operations.GetGreeting.Input 10 | ) async throws -> Operations.GetGreeting.Output { 11 | let name = input.query.name ?? "Stranger" 12 | let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!") 13 | return .ok(.init(body: .json(greeting))) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.main.1.2.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Vapor 3 | import OpenAPIRuntime 4 | import OpenAPIVapor 5 | 6 | // Define a type that conforms to the generated protocol. 7 | struct GreetingServiceAPIImpl: APIProtocol { 8 | func getGreeting( 9 | _ input: Operations.GetGreeting.Input 10 | ) async throws -> Operations.GetGreeting.Output { 11 | let name = input.query.name ?? "Stranger" 12 | let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!") 13 | return .ok(.init(body: .json(greeting))) 14 | } 15 | } 16 | 17 | // Create your Vapor application. 18 | let app = try await Vapor.Application.make() 19 | 20 | // Create a VaporTransport using your application. 21 | let transport = VaporTransport(routesBuilder: app) 22 | 23 | // Create an instance of your handler type that conforms the generated protocol 24 | // defining your service API. 25 | let handler = GreetingServiceAPIImpl() 26 | 27 | // Call the generated function on your implementation to add its request 28 | // handlers to the app. 29 | try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url()) 30 | 31 | // Start the app as you would normally. 32 | try await app.execute() 33 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.main.2.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import Vapor 3 | import OpenAPIRuntime 4 | import OpenAPIVapor 5 | 6 | // Define a type that conforms to the generated protocol. 7 | struct GreetingServiceAPIImpl: APIProtocol { 8 | func getGreeting( 9 | _ input: Operations.GetGreeting.Input 10 | ) async throws -> Operations.GetGreeting.Output { 11 | let name = input.query.name ?? "Stranger" 12 | let greeting = Components.Schemas.Greeting(message: "Hello, \(name)!") 13 | return .ok(.init(body: .json(greeting))) 14 | } 15 | 16 | func getEmoji( 17 | _ input: Operations.GetEmoji.Input 18 | ) async throws -> Operations.GetEmoji.Output { 19 | let emojis = "👋👍👏🙏🤙🤘" 20 | let emoji = String(emojis.randomElement()!) 21 | return .ok(.init(body: .plainText(.init(emoji)))) 22 | } 23 | } 24 | 25 | // Create your Vapor application. 26 | let app = try await Vapor.Application.make() 27 | 28 | // Create a VaporTransport using your application. 29 | let transport = VaporTransport(routesBuilder: app) 30 | 31 | // Create an instance of your handler type that conforms the generated protocol 32 | // defining your service API. 33 | let handler = GreetingServiceAPIImpl() 34 | 35 | // Call the generated function on your implementation to add its request 36 | // handlers to the app. 37 | try handler.registerHandlers(on: transport, serverURL: Servers.Server1.url()) 38 | 39 | // Start the app as you would normally. 40 | try await app.execute() 41 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | namingStrategy: idiomatic 5 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.openapi.0.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | components: 27 | schemas: 28 | Greeting: 29 | type: object 30 | properties: 31 | message: 32 | type: string 33 | required: 34 | - message 35 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Documentation.docc/Tutorials/_Resources/server.openapi.1.yaml: -------------------------------------------------------------------------------- 1 | openapi: '3.1.0' 2 | info: 3 | title: GreetingService 4 | version: 1.0.0 5 | servers: 6 | - url: https://example.com/api 7 | description: Example service deployment. 8 | paths: 9 | /greet: 10 | get: 11 | operationId: getGreeting 12 | parameters: 13 | - name: name 14 | required: false 15 | in: query 16 | description: The name used in the returned greeting. 17 | schema: 18 | type: string 19 | responses: 20 | '200': 21 | description: A success response with a greeting. 22 | content: 23 | application/json: 24 | schema: 25 | $ref: '#/components/schemas/Greeting' 26 | /emoji: 27 | get: 28 | operationId: getEmoji 29 | responses: 30 | '200': 31 | description: A success response with an emoji. 32 | content: 33 | text/plain: 34 | schema: 35 | type: string 36 | components: 37 | schemas: 38 | Greeting: 39 | type: object 40 | properties: 41 | message: 42 | type: string 43 | required: 44 | - message 45 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/PluginSource.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | 15 | /// The source of a plugin generator invocation. 16 | enum PluginSource: String, Codable { 17 | /// BuildTool plugin 18 | case build 19 | /// Command plugin. 20 | case command 21 | } 22 | -------------------------------------------------------------------------------- /Sources/swift-openapi-generator/Tool.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import ArgumentParser 15 | 16 | @main struct _Tool: AsyncParsableCommand { 17 | static let configuration: CommandConfiguration = .init( 18 | commandName: "swift-openapi-generator", 19 | abstract: "Generate Swift client and server code from an OpenAPI document", 20 | subcommands: [_FilterCommand.self, _GenerateCommand.self] 21 | ) 22 | } 23 | -------------------------------------------------------------------------------- /Tests/OpenAPIGeneratorCoreTests/Test_Config.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import XCTest 15 | import OpenAPIKit 16 | @testable import _OpenAPIGeneratorCore 17 | 18 | final class Test_Config: Test_Core { 19 | func testDefaultAccessModifier() { XCTAssertEqual(Config.defaultAccessModifier, .internal) } 20 | func testAdditionalFileComments() { 21 | let config = Config( 22 | mode: .types, 23 | access: .public, 24 | additionalFileComments: ["swift-format-ignore-file", "swiftlint:disable all"], 25 | namingStrategy: .defensive 26 | ) 27 | XCTAssertEqual(config.additionalFileComments, ["swift-format-ignore-file", "swiftlint:disable all"]) 28 | } 29 | func testEmptyAdditionalFileComments() { 30 | let config = Config(mode: .types, access: .public, namingStrategy: .defensive) 31 | XCTAssertEqual(config.additionalFileComments, []) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Tests/OpenAPIGeneratorCoreTests/Translator/Multipart/Test_MultipartAdditionalProperties.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import XCTest 15 | @preconcurrency import OpenAPIKit 16 | @testable import _OpenAPIGeneratorCore 17 | 18 | class Test_MultipartAdditionalProperties: XCTestCase { 19 | 20 | static let cases: [(Either?, MultipartAdditionalPropertiesStrategy)] = [ 21 | (nil, .allowed), (.a(true), .any), (.a(false), .disallowed), (.b(.string), .typed(.string)), 22 | ] 23 | func test() throws { 24 | for (additionalProperties, expectedStrategy) in Self.cases { 25 | XCTAssertEqual(MultipartAdditionalPropertiesStrategy(additionalProperties), expectedStrategy) 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Tests/OpenAPIGeneratorReferenceTests/XCTestDiagnosticCollector.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import _OpenAPIGeneratorCore 15 | import XCTest 16 | 17 | // A diagnostic collector that fails the running test on a warning or error. 18 | struct XCTestDiagnosticCollector: DiagnosticCollector { 19 | var test: XCTestCase 20 | var ignoredDiagnosticMessages: Set = [] 21 | 22 | func emit(_ diagnostic: Diagnostic) { 23 | guard !ignoredDiagnosticMessages.contains(diagnostic.message) else { return } 24 | print("Test emitted diagnostic: \(diagnostic.description)") 25 | switch diagnostic.severity { 26 | case .note: 27 | // no need to fail, just print 28 | break 29 | case .warning, .error: XCTFail("Failing with a diagnostic: \(diagnostic.description)") 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Tests/OpenAPIGeneratorTests/Resources/Docs/malformed-openapi.yaml: -------------------------------------------------------------------------------- 1 | # openapi: '3.1.0' -> Missing openapi version 2 | info: 3 | title: MalformedDocument 4 | version: 1.0.0 5 | -------------------------------------------------------------------------------- /Tests/OpenAPIGeneratorTests/Resources/Docs/openapi-generator-config.yaml: -------------------------------------------------------------------------------- 1 | generate: 2 | - types 3 | - server 4 | accessModifier: internal 5 | -------------------------------------------------------------------------------- /Tests/PetstoreConsumerTests/Common.swift: -------------------------------------------------------------------------------- 1 | //===----------------------------------------------------------------------===// 2 | // 3 | // This source file is part of the SwiftOpenAPIGenerator open source project 4 | // 5 | // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors 6 | // Licensed under Apache License v2.0 7 | // 8 | // See LICENSE.txt for license information 9 | // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors 10 | // 11 | // SPDX-License-Identifier: Apache-2.0 12 | // 13 | //===----------------------------------------------------------------------===// 14 | import XCTest 15 | import HTTPTypes 16 | 17 | extension Operations.ListPets.Output { 18 | static var success: Self { .ok(.init(headers: .init(myResponseUUID: "abcd"), body: .json([]))) } 19 | } 20 | 21 | extension HTTPRequest { 22 | /// Initializes an HTTP request with the specified path, HTTP method, and header fields. 23 | /// 24 | /// - Parameters: 25 | /// - path: The path of the HTTP request. 26 | /// - method: The HTTP method (e.g., GET, POST, PUT, DELETE, etc.). 27 | /// - headerFields: HTTP header fields to include in the request. 28 | public init(soar_path path: String, method: Method, headerFields: HTTPFields = .init()) { 29 | self.init(method: method, scheme: nil, authority: nil, path: path, headerFields: headerFields) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Tests/PetstoreConsumerTests/Generated: -------------------------------------------------------------------------------- 1 | ../OpenAPIGeneratorReferenceTests/Resources/ReferenceSources/Petstore --------------------------------------------------------------------------------