Skip to content

Generated Code with additionalProperties: true does not compile #193

@j--wong

Description

@j--wong

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: number

When I run oapi-codegen like this

oapi-codegen -generate types,skip-prune test.yaml

The 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions