Skip to content

Checking for the *[] vs []* #158

Closed

Description

@ahamansh

when we try to nest a struct
e.g. :
response{
total,
orderby,
[]data
}

total:
    type: integer
    format: int32  
data:
    type: array
    items:
      $ref: searchcomponent.yml

code genererated is

type SearchResponse struct {
	Data *[]struct {
		// Embedded struct due to allOf(#/components/schemas/CommonParent)
		someparentstruct
		// Embedded fields due to inline allOf schema
		Uri *string `json:"uri,omitempty"`
	} `json:"data,omitempty"`
	Total *int32 `json:"total,omitempty"`
}

above *[]struct should be []*struct

Activity

  1. pseudo-su commented on Mar 20, 2020

    @pseudo-su
    Contributor
    components:
      schemas:
        CommonParent:
          type: object
          required:
            - myval
          properties:
            myval:
              type: string
        SearchResponse:
          type: object
          properties:
            data:
              type: array
              items:
                allOf:
                  - $ref: "#/components/schemas/CommonParent"
                  - type: object
                    properties:
                      uri:
                        type: string
            total:
              type: integer
              format: int32

    This is the closest I got to replicating you use case.

    If you make the data field required it will remote the pointer from the Data field

    components:
      schemas:
        CommonParent:
          type: object
          required:
            - myval
          properties:
            myval:
              type: string
        SearchResponse:
          type: object
          required:
              - data
          properties:
            data:
              type: array
              items:
                allOf:
                  - $ref: "#/components/schemas/CommonParent"
                  - type: object
                    properties:
                      uri:
                        type: string
            total:
              type: integer
              format: int32

    I'm. not sure what the semantics are meant to be regarding type: array and if/how you can mark individual items as able to be null. possibly by having nullable: true somewhere, I'm not sure if/how this library handled this.

  2. pseudo-su commented on Mar 20, 2020

    @pseudo-su
    Contributor

    A quick test using this:

    components:
      schemas:
        CommonParent:
          type: object
          required:
            - myval
          properties:
            myval:
              type: string
        SearchResponse:
          type: object
          required:
              - data
          properties:
            data:
              type: array
              items:
                type: object
                nullable: true
                # allOf:
                #   - $ref: "#/components/schemas/CommonParent"
                #   - type: object
                #     properties:
                #       uri:
                #         type: string
            total:
              type: integer
              format: int32

    Generates:

    // SearchResponse defines model for SearchResponse.
    type SearchResponse struct {
    	Data  []map[string]interface{} `json:"data"`
    	Total *int32                   `json:"total,omitempty"`
    }

    And suggests that this library is not using the nullable: true field when determining if references to an object can be nil or not. That might be a feature or bugfix that could be added for correctness

  3. pseudo-su commented on Mar 20, 2020

    @pseudo-su
    Contributor

    Looks like there's some weirdness around the OpenAPI spec and nullable OAI/OpenAPI-Specification#1368 (comment)

    I might be wrong but it looks like the correct spec required in order to generate the code you would ([]*struct{}) would look something like this

    components:
      schemas:
        CommonParent:
          type: object
          required:
            - myval
          properties:
            myval:
              type: string
        SearchResponse:
          type: object
          required:
              - data
          properties:
            data:
              type: array
              items:
                type: object
                allOf:
                  - nullable: true
                  - $ref: "#/components/schemas/CommonParent"
                  - type: object
                    properties:
                      uri:
                        type: string
            total:
              type: integer
              format: int32

    However this currently generated a signature of []struct{} because nullable: true doesn't seem to be handled.

  4. dududko commented on Apr 20, 2020

    @dududko
    Contributor

    Have the same issue. I need to be able to generate the array of pointers to primitives of objets. OpenAPI v3 specification allows to do this by using nullable: true. However it is not implemented in this codegen and I also did not find the evidence of this feature in the source code.

    Is it possible to add this feature ?

  5. dududko commented on Apr 20, 2020

    @dududko
    Contributor

    Here is the example of api definition.

    components:
      schemas:
        Resp:
          properties:
            intValues:
              type: array
              items:
                type: integer
                format: int64
                nullable: true
    

    expected output

    // Resp defines model for Resp.
    type Resp struct {
            IntValues []*int64 `json:"intValues"`
    }
    
  6. wjase commented on Nov 26, 2021

    @wjase

    Hi Folks, I just submitted a pull request that should resolve at least some of these issues. I hit the same issue when trying to return a db row which was a column of nullable fields...
    #490

  7. wjase commented on Mar 12, 2022

    @wjase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions