Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: strict server interface doesn't marshal additionalProperties in complex object #1578

Open
reinkrul opened this issue Apr 26, 2024 · 0 comments

Comments

@reinkrul
Copy link
Contributor

reinkrul commented Apr 26, 2024

When generating a Strict Server interface with a type that has fields, as well as additionalProperties, the additionalProperties don't get marshalled.

Failing test
#1579

Cause
For a type with fields and additionalProperties, a custom MarshalJSON() function is generated to handle additionalProperties marshalling. However, for a strict server interface a response type is generated that aliases/subtypes the referenced response type (e.g. type CreateUserResponse becomes CreateUserJSON200Response CreateUserResponse).
But, this strict server interface-specific type does not implement json.Marshaler (to delegate it to the aliased/subtype), thus the custom MarshalJSON() function is never called.

Versions

github.com/oapi-codegen/runtime@v1.1.1
github.com/deepmap/oapi-codegen/v2/cmd/oapi-codegen@v2.1.0

Example

openapi: "3.0.1"
components:
  schemas:
    ComplexObject:
      type: object
      properties:
        field1:
         type: string
      additionalProperties: {}
paths:
  /test:
    get:
      operationId: test
      responses:
        200:
          description: test
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/ComplexObject"

Workaround
There's an ugly workaround, you can create a MarshalJSON() function for the strict server interface type and delegate it to the underlying type, e.g.:

var _ json.Marshaler = Test200JSONResponse{}

func (response Test200JSONResponse) MarshalJSON() ([]byte, error) {
	return json.Marshal(ComplexObject(response))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant