bow-swift / bow-openapi

šŸŒ Functional HTTP client generator from an OpenAPI/Swagger specification.
https://openapi.bow-swift.io
Other
51 stars 1 forks source link

Support oneOf #32

Open ferranpujolcamins opened 4 years ago

ferranpujolcamins commented 4 years ago

Given the following schema:

openapi: 3.0.2
info:
  version: 0.7.1
  title: aoide
  description: Music library management
  license:
    name: AGPL-3.0-or-later
    url: 'https://www.gnu.org/licenses/agpl-3.0.html'
paths:
  /date:
    get:
      summary: Get current date
      operationId: date
      parameters:
        - $ref: '#/components/schemas/WRAPPER'
      responses:
        '400':
          $ref: '#/components/schemas/YYYYMMDD'
components:
  schemas:
    WRAPPER:
      oneOf:
        - $ref: '#/components/schemas/YYYYMMDD'
    YYYYMMDD:
      type: object
      required:
        - value
      properties:
              value:
                 type: integer
                 format: int32

The YYYYMMDD struct is generated correctly. But then, bow-openapi generates two empty structs WRAPPER and OneOfWRAPPER.

I would expect WRAPPER to be an enum with a case with an associated value of YYYYMMDD.

truizlop commented 4 years ago

Thanks for reporting! We'll take a look, but it may be due to the underlying tool we are using.

miguelangel-dev commented 4 years ago

Ey @ferranpujolcamins ! You are putting on limit the library, nice!

Currently, we do not support OneOf, but you can use enum; I know you can not use it with associated complex types, but maybe you can find a workaround (think about it as DTO)... I will show you an example:

Given the next Apple SignIn service: https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens

You can look at the field grant_type, defined as enum in order to restrict possibilities for requests.

AppleSignInAuth:
      type: object
      required:
        - client_id
        - client_secret
        - grant_type
        - code
        - redirect_uri
      properties:
        client_id:
          type: string
          description: The application identifier for your app.
        client_secret:
          type: string
          description: A secret generated as a JSON Web Token that uses the secret key generated by the WWDR portal.
        grant_type:
          type: string
          description: The grant type that determines how the client interacts with the server. For authorization code validation, use authorization_code. For refresh token validation requests, use refresh_token.
          enum:
            - authorization_code
            - refresh_token
        code:
          type: string
          description: The authorization code received from your applicationā€™s user agent. The code is single use only and valid for five minutes.
        redirect_uri:
          type: string
          description: The destination URI the code was originally sent to. It must include a domain name, and canā€™t be an IP address or localhost.

Another example, in the response: https://developer.apple.com/documentation/sign_in_with_apple/errorresponse

We could define the error as

AppleSignInError:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: A string that describes the reason for the unsuccessful request. The string consists of a single allowed value.
          enum: 
            - invalid_request
            - invalid_client
            - invalid_grant
            - unauthorized_client
            - unsupported_grant_type
            - invalid_scope