Skip to content

Commit

Permalink
OpenAPI spec preprocessing (#102)
Browse files Browse the repository at this point in the history
# OpenAPI spec preprocessing

## ♻️ Current situation & Problem
The OpenAI OpenAPI specification contains the following issues that
require preprocessing before code generation:

- **Incorrect `required` Property**: A non-existent property is
incorrectly marked as `required` (see
[`openai-openapi#421`](openai/openai-openapi#421)).
- **Unsupported `oneOf` Syntax**: The `swift-openapi-generator` does not
fully support `oneOf` with `required` properties (see
[`swift-openapi-generator#739`](apple/swift-openapi-generator#739)).
- **Deprecation Warnings**: `deprecated` markings in the OpenAPI spec
trigger warnings in the generated Swift code (see
[`swift-openapi-generator#106`](apple/swift-openapi-generator#106)
and
[`swift-openapi-generator#715`](apple/swift-openapi-generator#715)).

Without preprocessing, these issues result in unnecessary warnings
during the Swift code generation and in the resulting Swift client code.

## ⚙️ Release Notes 
- Add OpenAPI spec preprocessing script
- Preprocess OpenAPI OpenAI spec 


## 📚 Documentation
README for proper motivation for the need of preprocessing and usage
instructions.


## ✅ Testing
Local testing


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
  • Loading branch information
philippzagar authored Feb 24, 2025
1 parent 6119ae9 commit 61b664b
Show file tree
Hide file tree
Showing 10 changed files with 1,560 additions and 1,142 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ docs/
*.key
*.srl
*.csr

# npm dependencies
node_modules/
56 changes: 56 additions & 0 deletions Sources/GeneratedOpenAIClient/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--
This source file is part of the Stanford Spezi open source project
SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md)
SPDX-License-Identifier: MIT
-->

# Generated OpenAI client

This SPM target uses the [`swift-openapi-generator`](https://github.com/apple/swift-openapi-generator) to generate Swift client code from the [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) and provides the generated code to other SpeziLLM targets. The generator's configuration is defined in `openapi-generator-config.yaml`, while the specification is located in `openapi.yaml`.

### Why Preprocessing is Needed

The OpenAI OpenAPI specification contains the following issues that require preprocessing before code generation:

- **Incorrect `required` Property**: A non-existent property is incorrectly marked as `required` (see [`openai-openapi#421`](https://github.com/openai/openai-openapi/issues/421)).
- **Unsupported `oneOf` Syntax**: The `swift-openapi-generator` does not fully support `oneOf` with `required` properties (see [`swift-openapi-generator#739`](https://github.com/apple/swift-openapi-generator/issues/739)).
- **Deprecation Warnings**: `deprecated` markings in the OpenAPI spec trigger warnings in the generated Swift code (see [`swift-openapi-generator#106`](https://github.com/apple/swift-openapi-generator/issues/106) and [`swift-openapi-generator#715`](https://github.com/apple/swift-openapi-generator/issues/715)).

Without preprocessing, these issues result in unnecessary warnings during the Swift code generation and in the resulting Swift client code.

### Running the Preprocessing Script

After updating the used [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) in SpeziLLM, run the preprocessing script to prepare the spec for use in SpeziLLM.

#### Prerequisits:

Make sure Node.js and npm are installed on your system, either via `brew install node` on macOS or `sudo apt install nodejs npm` on Ubuntu / Linux.
Also, ensure that the OpenAI OpenAPI spec is located in the `openapi.yaml` file.

#### **Steps:**
1. Navigate to the generated client directory:

```sh
cd Sources/GeneratedOpenAIClient
```


2. Install dependencies (if not already installed):

```sh
npm install
```


3. Run the preprocessing script:

```sh
npm run preprocess
```


After running the script, the specification will be correctly formatted (including the Spezi copyright header) and ready for Swift client code generation.
Loading

0 comments on commit 61b664b

Please sign in to comment.