Skip to content
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

Added Open API generated code. #2

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Code generation

1. Run below command to update the OpenAPI generated code:
```
openapi-generator generate -i https://api.swaggerhub.com/apis/passage/passage-auth-api/1 -g swift5 --additional-properties=responseAs=AsyncAwait -o Sources/Passage/generated
```

2. Run script to fix known generated code issues:
```
python3 Sources/Passage/fix_generated_code.py
```
75 changes: 75 additions & 0 deletions Sources/Passage/fix_generated_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os

openApiDirectory = './Sources/Passage/generated/OpenAPIClient/Classes/OpenAPIs'

# Define a list of tuples for replacements where each tuple is ('file_path', 'old_text', 'new_text')
replacements = [

# User model requires `webauthn_types` property, but webauthn start returns user WITHOUT it, so we must make it optional.
(
f'{openApiDirectory}/Models/User.swift',
'''public var webauthnTypes: [WebAuthnType]

''',
'''public var webauthnTypes: [WebAuthnType]?

'''
),

(
f'{openApiDirectory}/Models/CurrentUser.swift',
'''public var webauthnTypes: [WebAuthnType]

''',
'''public var webauthnTypes: [WebAuthnType]?

'''
),

# Some endpoints return a user with a status set to "". This is incompatible with the spec, so we have to add the statusUnavailable option.
(
f'{openApiDirectory}/Models/UserStatus.swift',
'''case pending = "pending"
}
''',
'''case pending = "pending"
case statusUnavailable = ""
}
'''
),

# Fix identifier encoding issue in UsersAPI:
(
f'{openApiDirectory}/APIs/UsersAPI.swift',
'return localVariableRequestBuilder.init(method: "GET", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, requiresAuthentication: false)',
'return localVariableRequestBuilder.init(method: "GET", URLString: (localVariableUrlComponents?.string?.removingPercentEncoding ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, requiresAuthentication: false)'
),

# Add more replacements here as needed
# ('file', 'oldText', 'newText'),
]

def replace_in_file(replacement):
file_path, old_text, new_text = replacement
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()

updated_content = content
# for old_text, new_text in replacements:
updated_content = updated_content.replace(old_text, new_text)

if content != updated_content:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"Updated {file_path}")

def remove_file(file_path):
if os.path.exists(file_path):
os.remove(file_path)
print(f"File '{file_path}' deleted successfully.")
else: print(f"File '{file_path}' not found.")

for replacement in replacements:
replace_in_file(replacement)

remove_file('Sources/Passage/generated/Package.swift')
100 changes: 100 additions & 0 deletions Sources/Passage/generated/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Created by https://www.toptal.com/developers/gitignore/api/xcode,swift
# Edit at https://www.toptal.com/developers/gitignore?templates=xcode,swift

### Swift ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## User settings
xcuserdata/

## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
*.xcscmblueprint
*.xccheckout

## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
build/
DerivedData/
*.moved-aside
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3

## Obj-C/Swift specific
*.hmap

## App packaging
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm

.build/

# CocoaPods
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
# Pods/
# Add this line if you want to avoid checking in source code from the Xcode workspace
# *.xcworkspace

# Carthage
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build/

# Accio dependency management
Dependencies/
.accio/

# fastlane
# It is recommended to not store the screenshots in the git repo.
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/#source-control

fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output

# Code Injection
# After new code Injection tools there's a generated folder /iOSInjectionProject
# https://github.com/johnno1962/injectionforxcode

iOSInjectionProject/

### Xcode ###

## Xcode 8 and earlier

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno
**/xcshareddata/WorkspaceSettings.xcsettings

# End of https://www.toptal.com/developers/gitignore/api/xcode,swift
23 changes: 23 additions & 0 deletions Sources/Passage/generated/.openapi-generator-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Loading