Skip to content

Conversation

@agnesesterhuizen
Copy link

Issue

When allOf was used in combination with direct properties and additionalProperties, the code generator would only process the allOf references but ignore any properties or additionalProperties defined directly on the same schema object.

schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "BaseObject": {
      "type": "object",
      "properties": {
        "BaseField": {
          "type": "string"
        }
      },
      "required": ["BaseField"]
    },
    "ComposedObject": {
      "allOf": [
        {
          "$ref": "#/definitions/BaseObject"
        }
      ],
      "type": "object",
      "additionalProperties": true,
      "properties": {
        "MissingField": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "uniqueItems": true
        }
      }
    }
  }
}

current output:

type BaseObject struct {
	// BaseField corresponds to the JSON schema field "BaseField".
	BaseField string `json:"BaseField" yaml:"BaseField" mapstructure:"BaseField"`
}

type ComposedObject struct {
	// BaseField corresponds to the JSON schema field "BaseField".
	BaseField string `json:"BaseField" yaml:"BaseField" mapstructure:"BaseField"`
}

The MissingField property and additionalProperties were ignored.

Fix

The fix adds logic to merge allOf schemas with any direct properties and additionalProperties before generating the struct so all fields from both sources are included.

fixed output:

type BaseObject struct {
	// BaseField corresponds to the JSON schema field "BaseField".
	BaseField string `json:"BaseField" yaml:"BaseField" mapstructure:"BaseField"`
}

type ComposedObject struct {
	// BaseField corresponds to the JSON schema field "BaseField".
	BaseField string `json:"BaseField" yaml:"BaseField" mapstructure:"BaseField"`

	// MissingField corresponds to the JSON schema field "MissingField".
	MissingField []string `json:"MissingField,omitempty" yaml:"MissingField,omitempty" mapstructure:"MissingField,omitempty"`

	AdditionalProperties interface{} `mapstructure:",remain"`
}

The generated struct now includes:

  • Fields from the allOf references (BaseField from BaseObject)
  • Direct properties (MissingField)
  • Additional properties support (AdditionalProperties field)

@codecov
Copy link

codecov bot commented Jul 31, 2025

Codecov Report

❌ Patch coverage is 14.46809% with 201 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@ba09651). Learn more about missing BASE report.

Files with missing lines Patch % Lines
tests/data/core/allOf/allOfMultipleRequired.go 0.00% 118 Missing ⚠️
tests/data/core/allOf/allOfWithDirectProperties.go 0.00% 76 Missing ⚠️
pkg/generator/schema_generator.go 82.92% 2 Missing and 5 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #464   +/-   ##
=======================================
  Coverage        ?   41.78%           
=======================================
  Files           ?       62           
  Lines           ?     6615           
  Branches        ?        0           
=======================================
  Hits            ?     2764           
  Misses          ?     3576           
  Partials        ?      275           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@perher
Copy link
Contributor

perher commented Aug 4, 2025

fyi @agnesesterhuizen I'm not a maintainer, but also have this issue and solved this in slightly similar way in #465. That MR also supports the anyOf case, I added your tests to that one as well.

@agnesesterhuizen
Copy link
Author

fyi @agnesesterhuizen I'm not a maintainer, but also have this issue and solved this in slightly similar way in #465. That MR also supports the anyOf case, I added your tests to that one as well.

Hey @perher! apologies for the slow response here. I'm happy to close this PR in favour of #473 which should solve my issue.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants