oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

AdditionalProperties behavior differences

Open yishanhe opened this issue 4 years ago • 1 comments

I am not sure if these two should be different?

definitions

    Foo:
      type: object
      additionalProperties: true

    Bar:
      type: object
      additionalProperties:
        type: object

code generated

// Foo defines model for Foo.
type Foo struct {
	AdditionalProperties map[string]interface{} `json:"-"`
}

// Bar defines model for Bar.
type Bar struct {
	AdditionalProperties map[string]map[string]interface{} `json:"-"`
}

Should Bar also be generated as Foo here? Thanks.

yishanhe avatar Feb 19 '21 17:02 yishanhe

For googlers: this is tracking a bug: panic: json: cannot unmarshal string into Go struct field XXX of type map[string]interface {}.

Workaround: Rewrite the OpenAPI specification to Foo (do not use Bar style).

lzap avatar Jun 21 '24 12:06 lzap

I've got the same issue and unable to rewrite the OpenAPI specification I am reading from. Do we know if someone is working on a fix for this? I'm happy to contribute to the project if not.

dwyery avatar Aug 05 '24 06:08 dwyery

A perl did trick for me:

perl -i -pe 'BEGIN{undef $$/;} s/"additionalProperties": {\s*"type": "object"\s*}/"additionalProperties": true/g' pkg/clients/pulp/pulp_openapi.json

Just saying, it is now part of a makefile target that refreshes the OpenAPI clients.

lzap avatar Aug 06 '24 18:08 lzap

Same. I took a note from @lzap and added this perl command to my Makefile, runs against my generated yaml:

docker run --rm -it -w /workdir -v ${PWD}:/workdir perl:latest perl -0777 -i -pe 's/      additionalProperties:\n        type: object/      additionalProperties: true/g' service.yml

or without docker:

perl -0777 -i -pe 's/      additionalProperties:\n        type: object/      additionalProperties: true/g' service.yml

It sucks to have to hack it, but it works for now 🤷

chaseisabelle avatar Mar 05 '25 17:03 chaseisabelle