Giter Site home page Giter Site logo

Comments (5)

czechboy0 avatar czechboy0 commented on June 5, 2024 1

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.

edonv avatar edonv commented on June 5, 2024

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.

edonv avatar edonv commented on June 5, 2024

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.

simonjbeaumont avatar simonjbeaumont commented on June 5, 2024

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.

czechboy0 avatar czechboy0 commented on June 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.