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

ModelService: Cannot add model because it already exists #10166

Open
junteudjio opened this issue Dec 25, 2024 · 1 comment
Open

ModelService: Cannot add model because it already exists #10166

junteudjio opened this issue Dec 25, 2024 · 1 comment
Assignees

Comments

@junteudjio
Copy link

Describe the Bug

When using Json field with a jsonSchema, it's currently impossible to add multiple items with the same JsonField URI in the admin panel.
For instance, if I have a ui-block (say featuresJsonBlock) containing a json field with a specific jsonSchema, I can only add ONE featuresJsonBlock on my page/post. If I try to add another featuresJsonBlock, monaco editor will throw an error similar to what's described here: atularen/ngx-monaco-editor#48 or microsoft/monaco-editor#926 .

I solved it on my end by creating a custom json field and edited the existing code in @payloadcms/ui/src/fields/JSON/index.tsx slightly.
But perhaps this could be added to payload/ui itself.

  const handleMount = useCallback(
    (editor, monaco) => {
      if (!jsonSchema) {
        return
      }

      const existingSchemas = monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas || []
      // if the schema is already in the existingSchemas, don't add it again
      if (existingSchemas.some(schema => schema.uri === jsonSchema.uri)) {
        return
      }
      const modelUri = monaco.Uri.parse(jsonSchema.uri)

      const model = monaco.editor.createModel(JSON.stringify(value, null, 2), 'json', modelUri)
      monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
        enableSchemaRequest: true,
        schemas: [...existingSchemas, jsonSchema],
        validate: true,
      })

      editor.setModel(model)
    },
    [jsonSchema, value],
  )

Link to the code that reproduces this issue

https://github.com/payloadcms/payload/blob/466f109152bc6bf9a67e7d7bafd38c7d57a881de/packages/ui/src/fields/JSON/index.tsx

Reproduction Steps

  • create a block with a jsonField with a specific jsonSchema
  • try to add that block more than once in a Page/Post
  • monaco editor will throw the error below
    ModelService: Cannot add model because it already exists

Which area(s) are affected? (Select all that apply)

area: ui

Environment Info

Payload
@junteudjio junteudjio added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Dec 25, 2024
@akhrarovsaid
Copy link
Contributor

Hey @junteudjio,

While the solution you describe will indeed work, it is a little bit unsatisfactory because, as a result, it would prevent subsequent JSON fields from having schema validation applied to them. I'm honestly not too sure about this one. If you have multiple JSON fields, a workaround is to simply provide them different uri's/filenames to prevent this from happening. Although, that will not work in something like an Array field.

@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Dec 30, 2024
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

No branches or pull requests

3 participants