Skip to content

strict server returns wrong JSON for array responses #1147

Closed

Description

@const-tmp

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

Activity

  1. ghost closed this as completedin e393d91on Jul 28, 2023
  2. ghost added a commit that references this issue on Jul 28, 2023
    25c2f43
  3. ghost reopened this on Jul 28, 2023
  4. added a commit that references this issue on Aug 1, 2023
    100275f
  5. slusarz commented on Aug 11, 2023

    @slusarz
    1. This affects more than just arrays. (I am able to reproduce if the base level of the JSON object consists solely of additionalProperties, for example).
    2. This fix didn't work for me because my content-type of the response was 'application/vnd.version.information.v1+json'. Changing line 68 in strict-interface.tmpl to this fixed things (sorry, typing on the road and don't have access to do git MR):
    {{if .IsJSON | and $fixedStatusCode $isRef -}}
    
  6. Nikscorp commented on Aug 17, 2023

    @Nikscorp

    I am able to reproduce if the base level of the JSON object consists solely of additionalProperties, for example

    I have the same problem for additionalProperties-only handlers, I guess it is because of this json.Marshal behaviour: https://go.dev/play/p/IXI8tyWLhc0

    spec to repro

    openapi: 3.0.0
    servers:
      - url: 'http://localhost:8080'
    info:
      description: Hello World specification
      version: 1.0.0
      title: Hello World API
    tags:
      - name: Demo
    
    paths:
      /hello_world:
        get:
          description: Hello world
          tags:
            - Hello world
          operationId: HelloWorld
          responses:
            200:
              $ref: '#/components/responses/HelloWorldResponse'
            400:
              $ref: '#/components/responses/ErrorResponse'
            500:
              $ref: '#/components/responses/ErrorResponse'
    components:
      responses:
        HelloWorldResponse:
          description: Hello world response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HelloWorld'
    
        ErrorResponse:
          description: Error
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                properties:
                  code:
                    type: integer
                  message:
                    type: string
      schemas:
        HelloWorld:
          description: Hello world
          type: object
          additionalProperties: true

    config:

    package: hello
    generate:
      chi-server: true
      strict-server: true
      models: true
    output: hello.gen.go

    Command: oapi-codegen --config api/config.yaml api/hello.yaml
    Version: v1.13.4

  7. Nikscorp commented on Aug 17, 2023

    @Nikscorp

    And for such spec that has additionalProperties and strict properties either — additional properties won't be marshalled:

    openapi: 3.0.0
    servers:
      - url: 'http://localhost:8080'
    info:
      description: Hello World specification
      version: 1.0.0
      title: Hello World API
    tags:
      - name: Demo
    
    paths:
      /hello_world:
        get:
          description: Hello world
          tags:
            - Hello world
          operationId: HelloWorld
          responses:
            200:
              $ref: '#/components/responses/HelloWorldResponse'
            400:
              $ref: '#/components/responses/ErrorResponse'
            500:
              $ref: '#/components/responses/ErrorResponse'
    components:
      responses:
        HelloWorldResponse:
          description: Hello world response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HelloWorld'
    
        ErrorResponse:
          description: Error
          content:
            application/json:
              schema:
                type: object
                required:
                  - code
                  - message
                properties:
                  code:
                    type: integer
                  message:
                    type: string
      schemas:
        HelloWorld:
          description: Hello world
          type: object
          additionalProperties: true
          properties:
            bla:
              type: string
              example: bla
          required:
            - bla

    I guess in codegen it should be type HelloWorldResponseJSONResponse HelloWorld -> type HelloWorldResponseJSONResponse = HelloWorld for func (a HelloWorld) MarshalJSON() ([]byte, error) to handle marshal

    Command: oapi-codegen --config api/config.yaml api/hello.yaml
    Version: v1.13.4

  8. added 2 commits that reference this issue on Sep 18, 2023
    a83c9c9
    9d324e3
  9. added a commit that references this issue on Feb 14, 2024
    e393d91
  10. added 2 commits that reference this issue on Aug 31, 2024
    590ff6e
    41526c9
  11. dastein1 commented on Jul 11, 2025

    @dastein1

    The issue is quite old. I encountered the issue today while upgrading. Is there any resolution in sight?

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