asyncapi / modelina

A library for generating typed models based on inputs such as AsyncAPI, OpenAPI, and JSON Schema documents with high customization
https://modelina.org
Apache License 2.0
302 stars 175 forks source link

[BUG] Modelina is creating nested models for pattern properties #2034

Closed Souvikns closed 3 days ago

Souvikns commented 3 months ago

Describe the bug.

Modelina is creating nested properties for patternProperties.

Example scheme, I have tried to made it as small as possible to isolate the bug

{
  "type": "object",
  "patternProperties": {
    "^[\\w\\d\\.\\-_]+$": {
      "oneOf": [
        {
          "$ref": "#/definitions/Reference"
        },
        {
          "$ref": "#/definitions/messageObject"
        }
      ]
    }
  },
  "definitions": {
    "Reference": {
      "type": "object",
      "description": "A simple object to allow referencing other components in the specification, internally and externally.",
      "required": ["$ref"],
      "properties": {
        "$ref": {
          "description": "The reference string.",
          "$ref": "#/definitions/ReferenceObject"
        }
      },
      "examples": [
        {
          "$ref": "#/components/schemas/Pet"
        }
      ]
    },
    "ReferenceObject": {
      "type": "string",
      "format": "uri-reference"
    },
    "messageObject": {
      "type": "object",
      "description": "Describes a message received on a given channel and operation.",
      "additionalProperties": false,
      "properties": {
        "contentType": {
          "type": "string",
          "description": "The content type to use when encoding/decoding a message's payload. The value MUST be a specific media type (e.g. application/json). When omitted, the value MUST be the one specified on the defaultContentType field."
        }
      }
    }
  }
}

This scheme is generating this root object


package models

// Root represents a Root model.
type Root struct {
  ModelinaAnyType interface{}
  PatternProperty_0
}
// pattern_property_0.go
package models

// PatternProperty_0 represents a PatternProperty_0 model.
type PatternProperty_0 struct {
  Reference
  MessageObject
}

Expected behavior

Should not have ModelinaAnyType interface{} in the root object and use pattern_property_0 in root itsef.

Screenshots

Screenshot 2024-06-11 at 1 36 07 PM

I have pushed my code on github, if anyone wanna check it out https://github.com/Souvikns/modelina-bug

How to Reproduce

To reproduce this we have install "@asyncapi/modelina": "v4.0.0-next.45" and create a script to use go file generator to use GO_COMMON_PRESET.

import { GoFileGenerator, GO_COMMON_PRESET } from '@asyncapi/modelina'

import * as path from 'path'
import * as fs from 'fs'

async function defaultGenerateModels(input: any) {
  const generator = new GoFileGenerator({
    presets: [
      {
        preset: GO_COMMON_PRESET,
        options: {
          addJsonTag: true,
        },
      },
    ],
  })
  await generator.generateToFiles(input, './models', { packageName: 'models' })
}

async function generate() {
  const content = fs.readFileSync(path.resolve('./scheme.json'), 'utf-8')

  await defaultGenerateModels(JSON.parse(content))
}

generate().catch((e) => console.error(e))

installed

🥦 Browser

Mozilla Firefox

👀 Have you checked for similar open issues?

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

asyncapi-bot commented 3 months ago

:tada: This issue has been resolved in version 4.0.0-next.50 :tada:

The release is available on:

Your semantic-release bot :package::rocket: