Git Product home page Git Product logo

Comments (4)

mgrider avatar mgrider commented on May 23, 2024 2

Hmmm. Don't know why github ate my uploads... but I'll see if I can find them or re-create them later today.

from createapi.

liamnichols avatar liamnichols commented on May 23, 2024

Hey @mgrider, sorry for missing this. The links to the examples seem to be broken, is it possible for you to share the output again?

You can use <details> to add the code in the comment, like the example below:

<details>
<summary>cleaned-openapi.yaml</summary>

<!-- ```yaml -->
<!-- Add the code here (without the html comments) -->
<!-- ``` -->

</details>

For example:

cleaned-openapi.yaml
# schema

from createapi.

mgrider avatar mgrider commented on May 23, 2024

I think this basically happens any time a path has a trailing slash in the yaml spec, for example:

paths:
  /api/lists/:

This will result in a generated Paths extension containing:

    public·static·var·lists:·Lists·{
        Lists(path"/api/lists")
    }

The command I'm using looks like this:

$ mint run CreateAPI create-api generate api.yaml --package APIPackage -c -s --output build

Here's a full-spec example that exhibits this behavior:

api.yaml
openapi: 3.0.2
info:
  title: Mobile API
  version: 1.0.0
  description: Private API for Mobile Apps
paths:
  /api/lists/:
    get:
      operationId: listLists
      description: ''
      parameters:
      - name: limit
        required: false
        in: query
        description: Number of results to return per page.
        schema:
          type: integer
      - name: offset
        required: false
        in: query
        description: The initial index from which to return the results.
        schema:
          type: integer
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object
                properties:
                  count:
                    type: integer
                    example: 123
                  next:
                    type: string
                    nullable: true
                    format: uri
                    example: http://api.example.org/accounts/?offset=400&limit=100
                  previous:
                    type: string
                    nullable: true
                    format: uri
                    example: http://api.example.org/accounts/?offset=200&limit=100
                  results:
                    type: array
                    items:
                      $ref: '#/components/schemas/List'
          description: ''
      tags:
      - api
  /api/lists/{url_locator}/:
    get:
      operationId: retrieveList
      description: ''
      parameters:
      - name: url_locator
        in: path
        required: true
        description: ''
        schema:
          type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ListDetail'
          description: ''
      tags:
      - api
  /api/token-auth/:
    post:
      operationId: createAuthToken
      description: ''
      parameters: []
      requestBody:
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/AuthToken'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/AuthToken'
          application/json:
            schema:
              $ref: '#/components/schemas/AuthToken'
      responses:
        '201':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthToken'
          description: ''
      tags:
      - api
components:
  schemas:
    List:
      type: object
      properties:
        url_locator:
          type: string
          maxLength: 100
        user:
          type: object
          properties:
            first_name:
              type: string
              maxLength: 150
            last_name:
              type: string
              maxLength: 150
            userprofile:
              type: object
              properties:
                url_locator:
                  type: string
                  maxLength: 100
                avatar:
                  type: object
                  properties:
                    url:
                      type: string
                      readOnly: true
                  readOnly: true
              required:
              - url_locator
              readOnly: true
          readOnly: true
        title:
          type: string
          description: Name your list
          maxLength: 255
        description:
          type: string
          nullable: true
          description: Why are you making this list? Who should look at it? (optional)
        modified:
          type: string
          format: date-time
          readOnly: true
        modified_date:
          type: string
          readOnly: true
        modified_date_short:
          type: string
          readOnly: true
        modified_humanized_delta:
          type: string
          readOnly: true
        created:
          type: string
          format: date-time
          readOnly: true
        privacy_level:
          enum:
          - 0
          - 1
          - 2
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        privacy_level_display:
          type: string
          readOnly: true
        search_visibility:
          enum:
          - 2
          - 1
          - 0
          type: integer
          minimum: -2147483648
          maximum: 2147483647
      required:
      - url_locator
      - title
    ListDetail:
      type: object
      properties:
        url_locator:
          type: string
          maxLength: 100
        user:
          type: object
          properties:
            first_name:
              type: string
              maxLength: 150
            last_name:
              type: string
              maxLength: 150
            userprofile:
              type: object
              properties:
                url_locator:
                  type: string
                  maxLength: 100
                avatar:
                  type: object
                  properties:
                    url:
                      type: string
                      readOnly: true
                  readOnly: true
              required:
              - url_locator
              readOnly: true
          readOnly: true
        title:
          type: string
          description: Name your list
          maxLength: 255
        description:
          type: string
          nullable: true
          description: Why are you making this list? Who should look at it? (optional)
        modified:
          type: string
          format: date-time
          readOnly: true
        modified_date:
          type: string
          readOnly: true
        modified_date_short:
          type: string
          readOnly: true
        modified_humanized_delta:
          type: string
          readOnly: true
        created:
          type: string
          format: date-time
          readOnly: true
        privacy_level:
          enum:
          - 0
          - 1
          - 2
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        privacy_level_display:
          type: string
          readOnly: true
        search_visibility:
          enum:
          - 2
          - 1
          - 0
          type: integer
          minimum: -2147483648
          maximum: 2147483647
        is_archive_list:
          type: string
          readOnly: true
        is_child_list:
          type: string
          readOnly: true
        is_shared:
          type: string
          readOnly: true
        is_private:
          type: string
          readOnly: true
        is_public:
          type: string
          readOnly: true
      required:
      - url_locator
      - title
    AuthToken:
      type: object
      properties:
        username:
          type: string
          writeOnly: true
        password:
          type: string
          writeOnly: true
        token:
          type: string
          readOnly: true
      required:
      - username
      - password

...and one of the resulting swift files:

build/APIPackage/Sources/Paths/PathsAPILists.swift
// Generated by Create API
// https://github.com/CreateAPI/CreateAPI
//
// swiftlint:disable all

import Foundation
import Get
import URLQueryEncoder

extension Paths {
    public static var lists: Lists {
        Lists(path: "/api/lists")
    }

    public struct Lists {
        /// Path: `/api/lists`
        public let path: String

        public func get(limit: Int? = nil, offset: Int? = nil) -> Request<GetResponse> {
            .get(path, query: makeGetQuery(limit, offset))
        }

        public struct GetResponse: Decodable {
            /// Example: 123
            public var count: Int?
            /// Example: "http://api.example.org/accounts/?offset=400&limit=100"
            public var next: URL?
            /// Example: "http://api.example.org/accounts/?offset=200&limit=100"
            public var previous: URL?
            public var results: [APIPackage.List]?

            public init(count: Int? = nil, next: URL? = nil, previous: URL? = nil, results: [APIPackage.List]? = nil) {
                self.count = count
                self.next = next
                self.previous = previous
                self.results = results
            }
        }

        private func makeGetQuery(_ limit: Int?, _ offset: Int?) -> [(String, String?)] {
            let encoder = URLQueryEncoder()
            encoder.encode(limit, forKey: "limit")
            encoder.encode(offset, forKey: "offset")
            return encoder.items
        }
    }
}

Worth noting that after some discussion, we decided to remove the trailing slashes on the server-side for my current project, and this is no longer an issue. But for a few iterations, I was manually editing all the resulting files to re-add the trailing slashes to the URLs.

from createapi.

liamnichols avatar liamnichols commented on May 23, 2024

Ahh thanks for that, I see what you are saying! In fact, I can see it in our existing test fixtures:

public static var collections: Collections {
Collections(path: "/api/collections")
}

I dug into it very quickly, and it seems like the root of this issue lies with OpenAPIKit:

https://github.com/mattpolzin/OpenAPIKit/blob/84193ea4c47d08b986f6a60ff85137002d36a940/Sources/OpenAPIKit/Path%20Item/PathItem.swift#L10-L33

OpenAPI.Path holds its data as an array of components that seems to discard knowledge of the leading and trailing /'s. I think if that is patched upstream, it should just work in CreateAPI though 🤞

This is useful to know but I am going to close this. I guess its not that important now, but if you do need this resolving then please open a new ticket in OpenAPIKit.

Thanks for taking the time to find the example again!

from createapi.

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.