Comments (5)
Hi @edonv,
yeah this is a large topic, so let me answer the individual points inline.
Regarding nullability, check out this maintainer doc (https://swiftpackageindex.com/apple/swift-openapi-generator/1.2.1/documentation/swift-openapi-generator/handling-nullable-schemas), which goes into detail of some of the decisions we made and hopefully answers your question.
At a higher level - we try to apply the smallest amount of post-processing to the OpenAPI doc, we don't flatten various schemas, because we want to stay faithful to the doc (rationale explained here: https://swiftpackageindex.com/apple/swift-openapi-generator/1.2.1/documentation/swift-openapi-generator/project-scope-and-goals).
That means that if you write something like:
Foo:
type: object
properties:
bar:
anyOf:
- type: null
- type: string
we will generate the complicated structure, even though you could have spelled the same thing as:
Foo:
type: object
properties:
bar:
type: string
The idea is that the OpenAPI document is the source of truth, and we follow that as closely as possible. If your OpenAPI document is written in an inefficient or verbose way, so will your generated Swift code.
Could oneOf be translated to an enum with associated values of the possible options?
That's already the case today. If you're not seeing that, please provide an example of the input OpenAPI snippet and generated code.
For anyOf/oneOf where the only options are "null" and some $ref, could it be translated to an optional instance of that type?
Not really, for the reasons explained earlier - we don't try to optimize your OpenAPI doc, that's up to the OpenAPI doc author. We just generate the most faithful Swift representation of the OpenAPI doc, without first pre-processing it.
If you're looking for ways to find and fix inefficiencies, there are many OSS tools that take an OpenAPI document as input and suggest improvements. But that functionality is out of scope of our Swift code generator, we try to be good OpenAPI community citizens and not needlessly include too many features that are already covered by other tools, as it both increases complexity of the generator and removes flexibility from our adopters.
from swift-openapi-generator.
Thanks for the detailed answer. I'm still relatively new to OpenAPI as it is, so it's likely just that the doc I'm working on isn't as efficient as it could be, leading the generated Swift code to be the same.
But in that vein, in OpenAPI, what would you say would be the best way to represent a property in an object that must be present but can be null
? It sounds like there should be a better way than what I example above.
from swift-openapi-generator.
And any suggestions regarding my last question?
Separately, is it possible to set the configuration so the generated types from components/schemas are public, but the initializers and underlying stuff is internal? Or would I just have to do 2 separate targets that generate the types and clients separately, then I combine them for public-facing stuff in a 3rd target?
from swift-openapi-generator.
And any suggestions regarding my last question?
Separately, is it possible to set the configuration so the generated types from components/schemas are public, but the initializers and underlying stuff is internal? Or would I just have to do 2 separate targets that generate the types and clients separately, then I combine them for public-facing stuff in a 3rd target?
That's currently the only supported way, yes. Our integration test package has an example of using the generator in multiple targets to separate the generation of types, client, and server, and sharing the generated types in the other targets: https://github.com/apple/swift-openapi-generator/tree/main/IntegrationTest.
from swift-openapi-generator.
But in that vein, in OpenAPI, what would you say would be the best way to represent a property in an object that must be present but can be null?
That's not easy to do for the reasons outlined in: #513 (comment)
I'd recommend to try to avoid needing to make a distinction between a present null value and an absent value, if at all possible.
from swift-openapi-generator.
Related Issues (20)
- Expose internal symbols HOT 4
- Swift tools version 5.9.2 but the installed version is 5.9.0 HOT 4
- Parsing ISO8601 date time from server fails due to milliseconds HOT 2
- How to send a file in multipart/form-data? HOT 3
- Convert the repsonse to an array of structs HOT 8
- Streamlining Use HOT 10
- How to throw errors that won't get transformed to ServerError? HOT 9
- Equatable conformance HOT 2
- Support `$dynamicAnchor` and `$dynamicRef` HOT 19
- Generated enums should conform to `CaseIterable` HOT 7
- Is it possible to use component refs in paths that accept multiple content types? HOT 8
- Multipart content not properly generated? HOT 11
- A way to group features or services to allow middleware to be applied to groups HOT 4
- Manual invocation of the generator and SPM warnings HOT 5
- Null property type with quotes not generated HOT 14
- Support for XML request body and response body HOT 7
- Support schemas.type: string, format: uri generates URL property not String HOT 3
- How to support OAuth2 Password flows? HOT 3
- allOf plus properties HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-openapi-generator.