OpenAPITools / OpenAPITools/openapi-generator

[BUG] Go client generator unable to serialize deepObjects that are anyOf

Open
#19,085 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The go client code generator does support deepObjects, but it is unable to do that when the schema is a anyOf type. It seems to be falling back to sending the type name as string only.

openapi-generator version

7.8.0-SNAPSHOT

OpenAPI declaration file content or url

Example spec:

openapi: 3.0.0
info:
  title: Test
  version: 1.0.0
servers:
- url: /v1
paths:
  /test1:
    get:
      operationId: my_list
      parameters:
      - in: query
        name: filter
        explode: false
        required: false
        schema:
          $ref: '#/components/schemas/FilterAny'
        style: deepObject
      responses:
        "200":
          description: success
components:
  schemas:
    FilterTypeRegex:
      properties:
        type:
          enum:
          - set
          - range
          type: string
        regex:
          type: string
      required:
      - type
      type: object
    FilterTypeRange:
      properties:
        type:
          enum:
          - set
          - range
          type: string
        data:
          type: array
          items:
            type: string
      required:
      - type
      type: object
    FilterAny:
      anyOf:
      - $ref: '#/components/schemas/FilterTypeRegex'
      - $ref: '#/components/schemas/FilterTypeRange'
      discriminator:
        mapping:
          set: '#/components/schemas/FilterTypeRegex'
          range: '#/components/schemas/FilterTypeRange'
        propertyName: type

Test function:

func CheckDeepObjectSerialize(t *testing.T) {

	// create filters to be sent as params
	rangeFilter := NewFilterTypeRange("range")
	rangeFilter.SetData([]string{"1", "3"})
	filterAny := FilterAny{}
	filterAny.FilterTypeRange = rangeFilter

	// prepare the query params
	localVarQueryParams := url.Values{}
	// filter1 is set to an object that is of anyOf type
	parameterAddToHeaderOrQuery(localVarQueryParams, "filter1", filterAny, "")
	// filter2 is set to an object that is a regular (non anyOf) type
	parameterAddToHeaderOrQuery(localVarQueryParams, "filter2", rangeFilter, "")

	localVarFormParams := url.Values{}
	localVarHeaderParams := make(map[string]string)
	localVarFormFiles := []formFile{}

	// prepare the request
	configuration := NewConfiguration()
	apiclient := NewAPIClient(configuration)
	ctx := context.Background()
	req, err := apiclient.prepareRequest(ctx, "/", "GET", nil, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFiles)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// print the request url with query string parameters
	log.Println("req.URL", req.URL.String())
}
Generation Details
$ java -jar openapi-generator-cli.jar generate -i openapi.yaml -g go -o client
Steps to reproduce

After code generation, add the test code, and a main.go similar to the snippet below:

package main

import (
	openapi "client"
	"log"
	"testing"
)

func main() {
	log.Println("starting")

	t := testing.T{}

	// call the CheckDeepObjectSerialize function
	openapi.CheckDeepObjectSerialize(&t)
}

Run test using go run main.go.

It should result in an output that looks something like:

/?filter1=openapi.FilterAny+value&filter2[data]=1&filter2[data]=3&filter2[type]=range

What is seen is that while filter2 was serialized properly, filter1 is not. It seems to just do a plain string type name.

Related issues/PRs
Suggest a fix

Seems like, having the generated struct for anyOf types conform to MappedNullable interface would help here. It could simply call the ToMap method of the underlying object.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Generate the provided OpenAPI YAML with the Go generator, then inspect the generated anyOf model and parameterAddToHeaderOrQuery helper. Compare serialization of the FilterAny and FilterTypeRange values using the supplied Go test and request output. Done means the anyOf deepObject produces the same nested query parameters as the regular object.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.