Skip to content

Generated Code with additionalProperties: true does not compile #193

Closed

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?

Activity

  1. j--wong commented on Jun 17, 2020

    @j--wong
    ContributorAuthor

    Upon looking further, it may be that GenStructFromAllOf in
    https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/schema.go#L394
    might need to handle additional properties before closing struct like in GenStructFromSchema here:
    https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/schema.go#L325-L333

    May also need to check duplicate AdditionalProperties before adding.

  2. j--wong commented on Jun 17, 2020

    @j--wong
    ContributorAuthor

    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

  3. added 2 commits that reference this issue on Jan 24, 2023
    c3fa136
    5e75dd8
  4. jamietanna commented on Mar 30, 2024

    @jamietanna
    Member

    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 🙃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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