Git Product home page Git Product logo

Comments (4)

mczachurski avatar mczachurski commented on May 28, 2024

You have to register all objects. In your case NestedObject and TestObject.

let nested1 = NestedObject(version: "1.0", value: "Nested Object 1")
let test = TestObject(nested:nested1, nestedObjects: [nested1], nestedValues: ["Str1", "Str2"])

let openAPIBuilder = OpenAPIBuilder(
    title: "Tasker server API",
    version: "1.0.0",
    description: "This is a sample server for task server application."
)
.add([
    APIObject(object: test),
    APIObject(object: nested1)     // <- add also nested
])

Nested objects in OpenAPI definition are added as a reference (name of the type, not real objects).

from swiftgger.

AndriyVasyk avatar AndriyVasyk commented on May 28, 2024

I tried to register NestedObject as you suggested. But it still does not contain NestedObject.
Here is the code I tried:

            let nested = NestedObject(version: "1.0", value: "Nested Object 1")
            let test = TestObject(nestedObjects: [nested])

            let openAPIBuilder = OpenAPIBuilder(
                title: "Tasker server API",
                version: "1.0.0",
                description: "This is a sample server for task server application."
            )
            .add([
                APIObject(object: test),
                APIObject(object: nested)
            ])
            .add(APIController(name: "Test",
                               description: "Controller where we can manage test",
                               actions: [
                                    APIAction(method: .get,
                                              route: "/test",
                                              summary: "Summary",
                                              description: "Description",
                                              responses: [
                                                APIResponse(code: "200",
                                                            description: "Ok",
                                                            type: .object(TestObject.self))
                                              ],
                                              authorization: false)
                               ])
            )

            let openAPIDocument = openAPIBuilder.built()

            let encoder = JSONEncoder()
            encoder.outputFormatting = .prettyPrinted

            let jsonData = try! encoder.encode(openAPIDocument)
            let jsonString = String(bytes: jsonData, encoding: .utf8)

JSON:
json.txt

Screenshot:
Screenshot 2021-06-24 at 09 22 14

from swiftgger.

mczachurski avatar mczachurski commented on May 28, 2024

I've copy&paste your code and I've different result.

image

Are you sure that you are using latest Swiftgger version (2.0.0-rc1)?

from swiftgger.

AndriyVasyk avatar AndriyVasyk commented on May 28, 2024

Yeah, I updated Swiftgger and now it works in most cases.
However i am seeing the same issue with array of nested optional objects ( [NestedObject?] ).
Could you please to take a look the issue with the following code?
And confirm whether this works. Or maybe I am doing something wrong again?

struct NestedObject: Content {
    var value: String
}
struct TestObject: Content {
    var nestedObjects: [NestedObject?] // --> This line is changed in comparison to previous examples
}
        let nested = NestedObject(value: "Nested Object 1")
        let test = TestObject(nestedObjects: [nested])

        let openAPIBuilder = OpenAPIBuilder(
            title: "Tasker server API",
            version: "1.0.0",
            description: "This is a sample server for task server application."
        )
        .add([
            APIObject(object: nested),
            APIObject(object: test)
        ])
        .add(APIController(name: "Test",
                           description: "Controller where we can manage test",
                           actions: [
                                APIAction(method: .get,
                                          route: "/test",
                                          summary: "Summary",
                                          description: "Description",
                                          responses: [
                                            APIResponse(code: "200",
                                                        description: "Ok",
                                                        type: .object(TestObject.self))
                                          ],
                                          authorization: false)
                           ])
        )

        let openAPIDocument = openAPIBuilder.built()
        let data = try JSONEncoder().encode(openAPIDocument)
        let serializedData = String(data: data, encoding: .utf8)!
        return serializedData

from swiftgger.

Related Issues (19)

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.