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

Broken json marshal/unmarshal for nested additional properties

Open johanneswuerbach opened this issue 3 years ago • 3 comments

json marshal/unmarshal is broken for nested additional properties like:

nested:
  description: Has nested additional properties
  type: object
  additionalProperties:
    additionalProperties: true
    type: "object"

I've looked into fixing this issue, but I'm not sure on the best approach. One option would be to modify the https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/templates/additional-properties.tmpl#L63 to handle such nested additionalProperties. Another one to generate named structs also for such cases with their own marshalling / unmarshalling function.

Do you have any preference or a different idea?

johanneswuerbach avatar May 02 '22 15:05 johanneswuerbach

Oh, this is a hard one. AdditionalProperties is very difficult to synthesize.

Can you reformat your schema like this:

components:
  schemas:
    InnerObject:
      additionalProperties: true
    OuterObject:
      type: object
      additionalProperties:
        $ref: "#/components/schemas/InnerObject"

deepmap-marcinr avatar May 19 '22 00:05 deepmap-marcinr

Yes, but I wondered if the autogenerated code could also generate „inner“ structs automatically instead of an anonymous one. This would solve this issue without a need to modify the schema. A naming convention could be the name of the surrounding stuct +Inner for each level. Wdyt?

johanneswuerbach avatar May 19 '22 05:05 johanneswuerbach

That's the right long term fix, it's just going to require some refactoring. Hopefully the workaround is ok for now.

deepmap-marcinr avatar May 19 '22 06:05 deepmap-marcinr

Closing this one for now.

johanneswuerbach avatar Jun 05 '23 18:06 johanneswuerbach