Repository: dbt-labs/dbt-core

Issue:
[CT-3316] [Bug] Invalid JSON schema for manifest v11
### Is this a new bug in dbt-core?

- [X] I believe this is a new bug in dbt-core
- [X] I have searched the existing issues, and I could not find an existing issue for this bug

### Current Behavior

Manifest v11 [JSON schema](https://schemas.getdbt.com/dbt/manifest/v11.json ) has been released with dbt 1.7.0. However, it's not a valid JSON schema: 

```json
{
  "$ref": "#/$defs/WritableManifest",
  "$defs": {
    "ManifestMetadata": {
      "type": "object",
      "title": "ManifestMetadata",
      ...
  }
}
```

Seems like `$defs.ManifestMetadata` contains the actual JSON schema that's supposed to be published.

### Expected Behavior

A valid JSON schema to be published.

### Steps To Reproduce

1. Copy https://schemas.getdbt.com/dbt/manifest/v11.json
2. Paste it into a JSON schema validator, e.g. https://www.jsonschemavalidator.net/

### Relevant log output

_No response_

### Environment

```markdown
- OS:
- Python:
- dbt:
```


### Which database adapter are you using with dbt?

_No response_

### Additional Context

_No response_


Discussion:
@mars-lan I see what you are saying about the `$defs.ManifestMetadata` addition in [v11](https://schemas.getdbt.com/dbt/manifest/v11.json) versus [v10](https://schemas.getdbt.com/dbt/manifest/v10.json).

I'll check if that change was intentional or accidental.
@mars-lan thank you for surfacing this and thank you to Metaphor Data for sponsoring Coalesce! I hope the conference went well for you all. Wanted to update you and let you know that engineers are looking into this and will update shortly.
@mars-lan Thanks for opening this issue. We have reproduced the behavior you discovered using https://www.jsonschemavalidator.net/, and we find it concerning, but we are curious whether you have encountered problems with the schema with any other tools, or other parts of your workflow. In other validators we have tried, and with the libraries we use internally for testing, the schema performed as we expected.

We'll follow this up regardless, but knowing the above would help us better understand the scope of the issue.
> @mars-lan Thanks for opening this issue. We have reproduced the behavior you discovered using https://www.jsonschemavalidator.net/, and we find it concerning, but we are curious whether you have encountered problems with the schema with any other tools, or other parts of your workflow. In other validators we have tried, and with the libraries we use internally for testing, the schema performed as we expected.
> 
> We'll follow this up regardless, but knowing the above would help us better understand the scope of the issue.

We used https://github.com/koxudaxi/datamodel-code-generator to generate Python classes from the JSON schema and it also had issues parsing the schema.
@emmyoop can you throw some point on this one? thank you!
Ping @emmyoop?
@mars-lan I am currently working on this
@mars-lan I'm currently unable to reproduce the issue 🤔 Last week I thought I had reproduced it, but I think I was doing so incorrectly. I'm not sure if the manifest v11.json has changed since this issue was opened, perhaps it has, but how we produce it has not. I'm gonna see what issues I run into with [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator) y'all use. Are you still experiencing issues? Below is a recording of me trying to reproduce the issue.

<a href="https://www.loom.com/share/a044dd1535e540c2a97842981c9b3b02">
      <img style="max-width:300px;" src="https://cdn.loom.com/sessions/thumbnails/a044dd1535e540c2a97842981c9b3b02-with-play.gif">
</a>
As a followup, I used the currently supplied v11.json manifest artifact with the [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator) tool, and it seems to be working as expected. Below is a loom of what I did
<div>
  <a href="https://www.loom.com/share/9efd9941400b42a48de9effc01c57727">
      <img style="max-width:300px;" src="https://cdn.loom.com/sessions/thumbnails/9efd9941400b42a48de9effc01c57727-with-play.gif">
  </a>
</div>
@QMalcolm I was able to reproduce with [catalog/v1.json](https://schemas.getdbt.com/dbt/catalog/v1.json). The original poster reported issues with the schema for `manifest.json`, but I had a small `catalog.json` handy, and it exhibits the same underlying issue with https://www.jsonschemavalidator.net/.

### Reprex

1. Copy the contents of [https://schemas.getdbt.com/dbt/catalog/v1.json](https://schemas.getdbt.com/dbt/catalog/v1.json) (or [here](https://github.com/dbt-labs/dbt-core/blob/f063e4e01c3faeba9ba559fcf153c2dd6148c8dd/schemas/dbt/catalog/v1.json)) into the **Select schema** text area on the left side of https://www.jsonschemavalidator.net/
    - Select "Custom" from the drop-down
2. Copy the following contents into the **Input JSON** text area on the right side
    ```json
    {
      "metadata": {
        "dbt_schema_version": "https://schemas.getdbt.com/dbt/catalog/v1.json",
        "dbt_version": "1.7.0b2",
        "generated_at": "2023-10-07T13:34:04.627535Z",
        "invocation_id": "c09b4da4-35c5-418d-8a4b-447d2925f075",
        "env": {}
      },
      "nodes": {},
      "sources": {},
      "errors": null
    }
    ```
3. You should see an error like the following:
    <img width="400" alt="image" src="https://github.com/dbt-labs/dbt-core/assets/44704949/d7a85b98-448a-4e25-8450-32a185256c16">


But if you replace the **Select schema** with the contents from [here](https://github.com/dbt-labs/dbt-core/blob/a77d325c8ab15c3444de3509617279ee5119f9f6/schemas/dbt/catalog/v1.json), it should validate:

<img width="400" alt="image" src="https://github.com/dbt-labs/dbt-core/assets/44704949/2c4f7bad-3b72-4a2f-921e-b6a537d6224c">

I synced with @dbeatty10. Although the v11.json manifest artifact can be used to produce models with the [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator). What is currently broken is using the artifact as custom schema to validate input json (which it used to do). In my demo, I put the schema in the right hand box on https://www.jsonschemavalidator.net/ which verified that it was valid jsonschema. What doesn't work is putting it in the left hand box of https://www.jsonschemavalidator.net/ to show that it can be used as a custom schema to validate json.

Resolving patch:
diff --git a/core/dbt/contracts/util.py b/core/dbt/contracts/util.py
--- a/core/dbt/contracts/util.py
+++ b/core/dbt/contracts/util.py
@@ -18,6 +18,7 @@
     ValidationError,
 )
 from mashumaro.jsonschema import build_json_schema
+from mashumaro.jsonschema.dialects import DRAFT_2020_12
 import functools
 
 
@@ -197,7 +198,7 @@ class VersionedSchema(dbtClassMixin):
     @classmethod
     @functools.lru_cache
     def json_schema(cls) -> Dict[str, Any]:
-        json_schema_obj = build_json_schema(cls, all_refs=True)
+        json_schema_obj = build_json_schema(cls, dialect=DRAFT_2020_12, with_dialect_uri=True)
         json_schema = json_schema_obj.to_dict()
         json_schema["$id"] = str(cls.dbt_schema_version)
         return json_schema


Test patch: