Repository navigation
Closed
Description
Hi,
I am using oapi-codegen to generate go code from OAS schemas. Please take a look at the issue below.
Here is an example schema for my scenario:
test.yaml
openapi: 3.0.0
info:
title: test schema
paths:
components:
schemas:
Person:
allOf:
# common fields
- type: object
additionalProperties: true
required:
- metadata
properties:
metadata:
type: string
# person specific fields
- type: object
additionalProperties: true
properties:
name:
type: string
age:
type: numberWhen I run oapi-codegen like this
oapi-codegen -generate types,skip-prune test.yamlThe resulting code below does NOT compile as it did not generate AdditionalProperties field in Person struct. However, it correctly generated Set and Get for AdditionalProperties, but not the AdditionalProperties itself.
// Package Test provides primitives to interact the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen DO NOT EDIT.
package Test
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// Person defines model for Person.
type Person struct {
// Embedded fields due to inline allOf schema
Metadata string `json:"metadata"`
// Embedded fields due to inline allOf schema
Age *float32 `json:"age,omitempty"`
Name *string `json:"name,omitempty"`
}
// Getter for additional properties for Person. Returns the specified
// element and whether it was found
func (a Person) Get(fieldName string) (value interface{}, found bool) {
if a.AdditionalProperties != nil {
value, found = a.AdditionalProperties[fieldName]
}
return
}
// Setter for additional properties for Person
func (a *Person) Set(fieldName string, value interface{}) {
if a.AdditionalProperties == nil {
a.AdditionalProperties = make(map[string]interface{})
}
a.AdditionalProperties[fieldName] = value
}
// Override default JSON handling for Person to handle AdditionalProperties
func (a *Person) UnmarshalJSON(b []byte) error {
// ... removed for brevity
}
// Override default JSON handling for Person to handle AdditionalProperties
func (a Person) MarshalJSON() ([]byte, error) {
// ... removed for brevity
}Any reasons why this is the case?
Metadata
Metadata
Assignees
Labels
No labels
Activity
j--wong commentedon Jun 17, 2020
Upon looking further, it may be that
GenStructFromAllOfinhttps://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/schema.go#L394
might need to handle additional properties before closing struct like in
GenStructFromSchemahere:https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/schema.go#L325-L333
May also need to check duplicate
AdditionalPropertiesbefore adding.j--wong commentedon Jun 17, 2020
Since I was already looking at the code, I might as well fix it.. 😂
So here is the draft PR for it: #194
If it's acceptable or there's another / more elegant way to fix the issue, feel free to let me know. I am keen to get a fix in for this so I can continue using this tool in my project. Cheers
fix oapi-codegen#193
fix oapi-codegen#193
chore: add test case for `additionalProperties` bug
chore: add test case for `additionalProperties` bug
jamietanna commentedon Mar 30, 2024
Thanks so much for reporting this! In the 4(!) years since you've raised this, it's been fixed, somewhere. Sorry it took us a while to confirm it 🙃