francescov1 / mongoose-tsgen

A plug-n-play Typescript generator for Mongoose.
105 stars 24 forks source link

Any schema that has arrays that are two layers deep will fail #114

Closed Nik-Novak closed 1 year ago

Nik-Novak commented 1 year ago

Comment and uncomment line 69 to see the issue. The issue occurs because refinement has an array of EbayAspectDistribution(line35), and within that array is another array of EbayAspectValueDistributions(line 10):

`import mongoose, { Schema } from 'mongoose';

const EbayAspectValueDistributions = new Schema({ localized_aspect_value: { type: String }, match_count: { type: Number }, refinement_href: { type: String }, }, { _id: false });

const EbayAspectDistribution = new Schema({ aspect_value_distributions: { type: [EbayAspectValueDistributions], default: [] }, localized_aspect_name: { type: String }, }, { _id: false });

const EbayBuyingOptionDistribution = new Schema({ buying_option: { type: String }, match_count: { type: Number }, refinement_href: { type: String }, }, { _id: false });

const EbayCategoryDistribution = new Schema({ category_id: { type: String }, category_name: { type: String }, match_count: { type: Number }, refinement_href: { type: String }, }, { _id: false });

const EbayConditionDistribution = new Schema({ condition: { type: String }, condition_id: { type: String }, match_count: { type: Number }, refinement_href: { type: String }, }, { _id: false });

const EbayRefinement = new Schema({ aspect_distributions: { type: [EbayAspectDistribution], default: [] }, // buying_option_distributions: { type: [EbayBuyingOptionDistribution], default: [] }, // category_distributions: { type: [EbayCategoryDistribution], default: [] }, // condition_distributions: { type: [EbayConditionDistribution], default: [] }, dominant_category_id: { type: String }, }, { _id: false });

const Parameter = new Schema({ name: { type: String }, value: { type: String }, }, { _id: false });

const EbayWarning = new Schema({ category: { type: String }, domain: { type: String }, error_id: { type: Number }, input_ref_ids: { type: [String], default: [] }, long_message: { type: String }, message: { type: String }, output_ref_ids: { type: [String], default: [] }, parameters: { type: [Parameter], default: [] }, subdomain: { type: String }, }, { _id: false });

// EBAY const EbaySearchSchema = new mongoose.Schema({ auto_corrections: { q: { type: String }, }, href: { type: String }, limit: { type: Number }, next: { type: String }, offset: { type: Number }, prev: { type: String }, // refinement: { type: EbayRefinement, default: {} }, total: { type: Number }, // warnings: { type: [EbayWarning], default: [] } });

export const EbaySearch = mongoose.model('EbaySearch', EbaySearchSchema); `

Nik-Novak commented 1 year ago

It's an amazing library albeit this one completely breaking issue.

Nik-Novak commented 1 year ago

Update:

There is another case that breaks compilation, uncomment and comment this field of: CraigslistAreaSchema:

const CraigslistAreaSchema = new Schema({
    areaUnits: String,
    currency: String,
    distanceUnits: String,
    name: String
}, { _id: false });

const CraigslistSearchSchema = new Schema({
    apiVersion: Number,
    configId: String,
    configTs: Number,
    data: {
      areas: {
          type: Map,
          of: CraigslistAreaSchema
      },
      bundleDups: Number,
      cacheTs: Number,
      canonicalUrl: String,
      categoryAbbr: String,
      // decode: CraigslistDecodeLocationSchema,
      detailsOrder: String,
      filterButtons: [String],
      // filters: [CraigslistFilterSchema],
      hubLinks: [String],
      // humanReadableParams: [CraigslistHumanReadableParamsSchema],
      items: [String],
      lang: String,
      // location: CraigslistLocationSchema,
      // params: CraigslistParamsSchema,
      relatedSearches: [String],
      resultsOrder: String,
      totalResultCount: Number,
      updated: Number,
      vicinity: Schema.Types.Mixed
    },
    // errors: [String]
});
francescov1 commented 1 year ago

Hi @Nik-Novak,

I just tested the first case you reported but was not able to reproduce an error, the generation & compilation works fine for me. What version of mongoose-tsgen & mongoose are you using? It would be great if you can provide a simple repro repo with these issues so I can understand the full env.

I was able to repro the second case so will start looking to fix that ASAP

francescov1 commented 1 year ago

https://github.com/francescov1/mongoose-tsgen/pull/115 resolves the second issue. Let me know if youre able to provide a repro for the first issue. Ill go ahead and deploy this fix for now.

francescov1 commented 1 year ago

I've tracked down the source of the issue with array schemas containing arrays. Turns out the use of the unflatten function here. I assume there's some sort of cyclical JSON that gets built from Mongoose in this case, causing the error.

Ill keep investigating!

francescov1 commented 1 year ago

I have a PR that should fix the last issue, just need to do a bit more testing

francescov1 commented 1 year ago

This fix is live in v9.2.0!