oapi-codegen
oapi-codegen copied to clipboard
Checking for the *[] vs []*
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
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.
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
Looks like there's some weirdness around the OpenAPI spec and nullable
https://github.com/OAI/OpenAPI-Specification/issues/1368#issuecomment-580290441
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.
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 ?
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"`
}
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... https://github.com/deepmap/oapi-codegen/pull/490
https://github.com/deepmap/oapi-codegen/pull/490