-
Notifications
You must be signed in to change notification settings - Fork 132
Allow substituting types #764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+351
−3
Merged
Changes from 22 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
68ef99c
Allow substituting types
762fbdd
Support replacement also in inline defined schemas
cff5760
More tests
0227843
WIP Proposal
8fe84b7
Add Example Project
8646df8
Apply x-swift-open-api-replace-type extension
37b8544
Add additionalProperties to examples
dbf467d
Replace in additionalProperties
108386a
Update ProposalText
715b35d
Remove vendor-extensions implementation
4922fa7
Add example
af6c242
more tests
2130376
Update Proposal Document
f14f710
add type-overrides in nested config
5753bc1
Apply suggestions from code review
simonbility 33da4ee
fix failing test
b4acd4c
add doc comment
46b20fd
Update example code
11fa2c2
Remove Proposal added in separate PR
557e01a
Cleanup example project
7956564
emit warning when overriding non-existing schema
b2ea3fe
Switch to plugin in example
16f4cf0
Apply suggestions from code review
simonbility 3df13a7
move validation logic
34aa46a
simplify run description
384f427
Migrate to SnippedBasedReferenceTests
31d11d0
test validation logic
c57b645
undo whitespace change
60595b9
Add TypeOverrides in Tutorials
e43134f
add todo for example project
73cc059
Update Examples/type-overrides-example/Sources/openapi.yaml
czechboy0 d4f8103
Merge branch 'main' into type-substitutions
czechboy0 f863db1
cleanup
7a271e5
Add License Header
f9418f3
Format Code
88706b7
Format Code in Tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.DS_Store | ||
.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.vscode | ||
/Package.resolved | ||
.ci/ | ||
.docc-build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// swift-tools-version:5.10 | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "type-overrides-example", | ||
platforms: [.macOS(.v14)], | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
products: [ | ||
.library(name: "Types", targets: ["Types"]), | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), | ||
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.7.0"), | ||
], | ||
targets: [ | ||
.target( | ||
name: "Types", | ||
dependencies: [ | ||
"ExternalLibrary", | ||
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")], | ||
plugins: [.plugin(name: "OpenAPIGenerator", package: "swift-openapi-generator")] | ||
), | ||
.target( | ||
name: "ExternalLibrary" | ||
), | ||
] | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Overriding types | ||
|
||
An example project using [Swift OpenAPI Generator](https://github.com/apple/swift-openapi-generator). | ||
|
||
> **Disclaimer:** This example is deliberately simplified and is intended for illustrative purposes only. | ||
|
||
## Overview | ||
|
||
This example shows how to use the TypeOverrides feature of the Swift OpenAPI Generator. | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Usage | ||
|
||
Build: | ||
|
||
```console | ||
% swift build | ||
Build complete! | ||
``` |
38 changes: 38 additions & 0 deletions
38
Examples/type-overrides-example/Sources/ExternalLibrary/PrimeNumber.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
/// Example struct to be used instead of the default generated type. | ||
/// This illustrates how to introduce a type performing additional validation during Decoding that cannot be expressed with OpenAPI | ||
public struct PrimeNumber: Codable, Hashable, RawRepresentable, Sendable { | ||
public let rawValue: Int | ||
public init?(rawValue: Int) { | ||
if !rawValue.isPrime { return nil } | ||
self.rawValue = rawValue | ||
} | ||
|
||
public init(from decoder: any Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let number = try container.decode(Int.self) | ||
guard let value = PrimeNumber(rawValue: number) else { | ||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "The number is not prime.") | ||
} | ||
self = value | ||
} | ||
public func encode(to encoder: any Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(self.rawValue) | ||
} | ||
|
||
} | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
extension Int { | ||
fileprivate var isPrime: Bool { | ||
if self <= 1 { return false } | ||
if self <= 3 { return true } | ||
|
||
var i = 2 | ||
while i * i <= self { | ||
if self % i == 0 { return false } | ||
i += 1 | ||
} | ||
return true | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Examples/type-overrides-example/Sources/Types/openapi-generator-config.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
generate: | ||
- types | ||
accessModifier: package | ||
namingStrategy: idiomatic | ||
additionalImports: | ||
- Foundation | ||
- ExternalLibrary | ||
typeOverrides: | ||
schemas: | ||
UUID: Foundation.UUID | ||
PrimeNumber: ExternalLibrary.PrimeNumber |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../openapi.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
openapi: '3.1.0' | ||
info: | ||
title: GreetingService | ||
version: 1.0.0 | ||
servers: | ||
- url: https://example.com/api | ||
description: Example service deployment. | ||
paths: | ||
/user: | ||
get: | ||
operationId: getUser | ||
parameters: | ||
- name: name | ||
required: false | ||
in: query | ||
description: The name of the user | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: A success response with the user. | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/User' | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
components: | ||
schemas: | ||
UUID: # this will be replaced by with Foundation.UUID specified by typeOverrides in open-api-generator-config | ||
type: string | ||
format: uuid | ||
|
||
PrimeNumber: # this will be replaced by with ExternalLibrary.PrimeNumber specified by typeOverrides in open-api-generator-config | ||
czechboy0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: string | ||
format: uuid | ||
|
||
User: | ||
type: object | ||
properties: | ||
id: | ||
$ref: '#/components/schemas/UUID' | ||
name: | ||
type: string | ||
favorite_prime_number: | ||
$ref: '#/components/schemas/PrimeNumber' | ||
|
||
|
||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,7 +372,6 @@ enum Constants { | |
/// The substring used in method names for the multipart coding strategy. | ||
static let multipart: String = "Multipart" | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please undo this change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW i just ran the code-formatter on this file. |
||
/// Constants related to types used in many components. | ||
enum Global { | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftOpenAPIGenerator open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the SwiftOpenAPIGenerator project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Type containing overrides for schema types. | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public struct TypeOverrides: Sendable { | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// A dictionary of overrides for replacing the types generated from schemas with manually provided types | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public var schemas: [String: String] | ||
|
||
/// Creates a new instance of `TypeOverrides` | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// - Parameter schemas: A dictionary mapping schema names to their override types. | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public init(schemas: [String: String] = [:]) { self.schemas = schemas } | ||
|
||
/// A Boolean value indicating whether there are no overrides. | ||
public var isEmpty: Bool { schemas.isEmpty } | ||
simonbility marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.