Repository navigation
Closed
Description
spec:
# openapi.yaml
openapi: 3.0.3
info:
title: Test
description: Test
version: 1.0.0
paths:
/test/1:
get:
responses:
200:
description: OK
content:
application/json:
schema:
type: object
properties:
string:
type: string
number:
type: number
required:
- string
- number
/test/2:
get:
responses:
200:
$ref: '#/components/responses/test_response'
/test/3:
get:
responses:
200:
$ref: '#/components/responses/test_array_response'
/test/4:
get:
responses:
200:
$ref: '#/components/responses/test_array_response2'
components:
schemas:
test:
type: object
properties:
string:
type: string
number:
type: number
required:
- string
- number
test_array:
type: array
items:
$ref: '#/components/schemas/test'
responses:
test_response:
description: Test response
content:
application/json:
schema:
$ref: '#/components/schemas/test'
test_array_response:
description: Test array response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/test'
test_array_response2:
description: Test array response 2
content:
application/json:
schema:
$ref: '#/components/schemas/test_array'
generate:
oapi-codegen \
-generate types,client,server,strict-server,spec \
-package api \
-o api/test.gen.go \
openapi.yaml
run:
package main
import (
"context"
"github.com/labstack/echo/v4"
"log"
"test/api"
)
type impl struct{}
func (i impl) GetTest4(ctx context.Context, request api.GetTest4RequestObject) (api.GetTest4ResponseObject, error) {
return api.GetTest4200JSONResponse{TestArrayResponse2JSONResponse: []api.Test{
{String: "s", Number: 1},
{String: "str", Number: 2},
}}, nil
}
func (i impl) GetTest3(ctx context.Context, request api.GetTest3RequestObject) (api.GetTest3ResponseObject, error) {
return api.GetTest3200JSONResponse{TestArrayResponseJSONResponse: []api.Test{
{String: "s", Number: 1},
{String: "str", Number: 2},
}}, nil
}
func (i impl) GetTest1(ctx context.Context, request api.GetTest1RequestObject) (api.GetTest1ResponseObject, error) {
return api.GetTest1200JSONResponse{String: "string", Number: 1}, nil
}
func (i impl) GetTest2(ctx context.Context, request api.GetTest2RequestObject) (api.GetTest2ResponseObject, error) {
return api.GetTest2200JSONResponse{
TestResponseJSONResponse: api.TestResponseJSONResponse{String: "string", Number: 1},
}, nil
}
func main() {
e := echo.New()
srv := api.NewStrictHandler(impl{}, nil)
api.RegisterHandlers(e, srv)
log.Fatal(e.Start("localhost:8080"))
}
curl
curl localhost:8080/test/1
{"number":1,"string":"string"}
curl localhost:8080/test/2
{"number":1,"string":"string"}
curl localhost:8080/test/3
{"TestArrayResponseJSONResponse":[{"number":1,"string":"s"},{"number":2,"string":"str"}]}
curl localhost:8080/test/4
{"TestArrayResponse2JSONResponse":[{"number":1,"string":"s"},{"number":2,"string":"str"}]}
Last 2 responses are not as as expected
Reacted by Danko Petrovic and Matthias Preu
Metadata
Metadata
Assignees
Labels
No labels
Activity
Revert "fix #1147 (#1148)"
fix oapi-codegen#1147
slusarz commentedon Aug 11, 2023
Nikscorp commentedon Aug 17, 2023
I have the same problem for additionalProperties-only handlers, I guess it is because of this
json.Marshalbehaviour: https://go.dev/play/p/IXI8tyWLhc0spec to repro
config:
Command:
oapi-codegen --config api/config.yaml api/hello.yamlVersion: v1.13.4
Nikscorp commentedon Aug 17, 2023
And for such spec that has additionalProperties and strict properties either — additional properties won't be marshalled:
I guess in codegen it should be
type HelloWorldResponseJSONResponse HelloWorld->type HelloWorldResponseJSONResponse = HelloWorldforfunc (a HelloWorld) MarshalJSON() ([]byte, error)to handle marshalCommand:
oapi-codegen --config api/config.yaml api/hello.yamlVersion: v1.13.4
fix oapi-codegen#1147 (oapi-codegen#1148)
Revert "fix oapi-codegen#1147 (oapi-codegen#1148)"
fix oapi-codegen#1147 (oapi-codegen#1148)
fix oapi-codegen#1147 (oapi-codegen#1148)
Revert "fix oapi-codegen#1147 (oapi-codegen#1148)"
dastein1 commentedon Jul 11, 2025
The issue is quite old. I encountered the issue today while upgrading. Is there any resolution in sight?
fix: allOf/anyOf/oneOf generation bugs, with an aggregate test matrix (…