Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for subschemas #99

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Conversation

omissis
Copy link
Owner

@omissis omissis commented Jul 21, 2023

Open Source Saturday

This PR introduces support for anyOf and allOf subschemas.

@omissis omissis added the enhancement New feature or request label Jul 21, 2023
@omissis omissis self-assigned this Jul 21, 2023
@omissis omissis force-pushed the feature/implement-subschemas branch 2 times, most recently from 1c44284 to 4684bc6 Compare October 7, 2023 13:56
@codecov
Copy link

codecov bot commented Oct 7, 2023

Codecov Report

Attention: Patch coverage is 70.50147% with 100 lines in your changes are missing coverage. Please review.

Project coverage is 75.25%. Comparing base (d963216) to head (7bc36c4).
Report is 29 commits behind head on main.

❗ Current head 7bc36c4 differs from pull request most recent head 66cf143. Consider uploading reports for the commit 66cf143 to get more accurate results

Files Patch % Lines
pkg/generator/schema_generator.go 71.11% 17 Missing and 9 partials ⚠️
pkg/schemas/model.go 70.37% 12 Missing and 4 partials ⚠️
tests/data/validation/maxLength/maxLength.go 37.50% 13 Missing and 2 partials ⚠️
tests/data/validation/minLength/minLength.go 37.50% 13 Missing and 2 partials ⚠️
pkg/generator/output.go 54.16% 9 Missing and 2 partials ⚠️
internal/x/text/cases.go 0.00% 2 Missing and 1 partial ⚠️
pkg/generator/generate.go 62.50% 2 Missing and 1 partial ⚠️
pkg/generator/json_formatter.go 81.25% 2 Missing and 1 partial ⚠️
pkg/generator/yaml_formatter.go 76.92% 2 Missing and 1 partial ⚠️
tests/data/extraImports/gopkgYAMLv3/gopkgYAMLv3.go 0.00% 3 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #99      +/-   ##
==========================================
- Coverage   76.58%   75.25%   -1.33%     
==========================================
  Files          24       25       +1     
  Lines        1892     2138     +246     
==========================================
+ Hits         1449     1609     +160     
- Misses        354      425      +71     
- Partials       89      104      +15     

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

@omissis omissis force-pushed the feature/implement-subschemas branch from 4684bc6 to 43dd923 Compare November 8, 2023 00:57
@omissis omissis marked this pull request as ready for review November 8, 2023 01:04
@omissis omissis added this to the v0.16.0 milestone Jan 17, 2024
@omissis omissis force-pushed the feature/implement-subschemas branch from 046ba39 to 7bc36c4 Compare January 18, 2024 23:29
@omissis omissis modified the milestones: v0.16.0, v0.17.0 Apr 20, 2024
@omissis omissis force-pushed the feature/implement-subschemas branch 2 times, most recently from 18e8e38 to 827b886 Compare April 20, 2024 23:41
- fix: add guard against loading non-json files when reading references in schemas
- feat: introduce basic allOf support
- fix: handle struct slices prop merging for allOf types
- feat: introduce basic anyOf support, fix default value issue.
- fix: make anyOf properties of primitive types dump interface instead of first type
- fix: reduce duplicate types
- fix: furhter reduce duplicate types
- chore: refactor cmp Opts utility
- fix: add graceful handling of some edge cases in code generation
    - empty enum type name
    - missing unmarshal methods for map types
    - "Plain" type naming collisions
    - schema.Type new properties potential (de)serialization
- chore: rename test files after rebase
- chore: cleanup go deps
- chore: fix go linting issues
- chore: integrate new formatter changes after rebase
- fix: set consistent var names in unmarshallers, introduce support for both yaml and json in validators
- chore: remove dead code
- fix: use '>' instead of '>=' to perform max length validation.
- chore: remove 'two' constant
- chore: disable gomnd linter
@omissis omissis force-pushed the feature/implement-subschemas branch from cb3f1ad to 66cf143 Compare May 16, 2024 06:28
@@ -373,7 +415,7 @@ func (g *schemaGenerator) generateType(
return codegen.EmptyInterfaceType{}, nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for maintaining this great tool!
I have been trying to use it for a schema with anyOf and I have a comment on this PR.

When using anyOf, the type may not be set, and only be defined in the items which are part of the anyOf itself - see for instance https://json-schema.org/understanding-json-schema/reference/combining#anyOf.

Specifically, I tried running go-jsonschema from the lastest release as well as from this PR for this schema, and in both cases, I hit the EmptyInterfaceType.

type EmbeddedlinksarrayJson []interface{}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! will look into it :)

@joepjoosten
Copy link

It breaks with this testcase:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$defs": {
        "foo": {
            "type": "object",
            "properties": {
                "foo": {
                    "type": "string"
                }
            },
            "required": [
                "foo"
            ]
        }
    },
    "id": "https://example.com/objectAllOf",
    "type": "object",
    "properties": {
        "flags": {
            "anyOf": [
                {
                    "type": "string"
                },
                {
                    "type": "boolean"
                }
            ]
        },
        "configurations": {
            "type": "array",
            "items": {
                "anyOf": [
                    {
                        "type": "object",
                        "properties": {
                            "bar": {
                                "type": "number"
                            }
                        },
                        "required": [
                            "bar"
                        ]
                    },
                    {
                        "type": "object",
                        "properties": {
                            "baz": {
                                "type": "boolean"
                            }
                        }
                    },
                    {
                        "$ref": "#/$defs/foo"
                    }
                ]
            }
        }
    }
}

@omissis
Copy link
Owner Author

omissis commented May 24, 2024

@joepjoosten thanks for providing it! I will look at it as soon as I can.

@l0nedigit
Copy link

Any updates on the status of this PR? The fixes in this branch would be helpful to a project I am working on as well. Thanks

@omissis omissis removed this from the v0.17.0 milestone Sep 18, 2024
@LarsStegman
Copy link

LarsStegman commented Nov 1, 2024

Hi @omissis any updates on when you think this is ready for use? I am using allOf and anyOf quite extensively in one of my project and I would really like to use this tooling. Is there anything I might do to help get it ready?

@iiacoban42
Copy link

iiacoban42 commented Nov 5, 2024

Seems to break when there is an allOf or anyOf at root level:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "id": "https://example.com/objectAllOf",
    "type": "object",
    "allOf": [
      {
        "type": "object",
        "properties": {
          "foo": {
            "type": "string"
          }
        },
        "required": ["foo"]
      },
      {
        "type": "object",
        "properties": {
          "bar": {
            "type": "number"
          }
        },
        "required": ["bar"]
      },
      {
        "type": "object",
        "properties": {
          "configurations": {
            "type": "array",
            "items": {}
          }
        }
      }
    ]
  }

@omissis
Copy link
Owner Author

omissis commented Nov 17, 2024

A quick update for all those who are waiting: I have just finished bringing this one up to date with all the things I have merged on main, and fixed all the broken tests code that I could find. This puts me in a position where I can look at all the feedback above and try to fix the issues. This is now the top priority for this repo to merge, and save for very small and trivial fixes and improvements, I will not merge anything else until this is done. Thank you for your patience.

@ptodev
Copy link

ptodev commented Nov 20, 2024

Thank you, @omissis! If you are looking for more inspiration for testing, it might be worth looking into what other json schema tools use in their repos:

If I find time in the coming weeks, I could also take a look to see if there are any interesting test cases we could add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants