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

Handle DocumentArray in FlattenMaps #13346

Merged

Conversation

Jokero
Copy link
Contributor

@Jokero Jokero commented Apr 30, 2023

Closes #13345

Summary

Since 7.1.0, lean() replaces Map in documents with their Record<string, V> equivalent by using FlattenMaps. Currently, FlattenMaps returns a type incompatible with InferSchemaType type when there is a property of DocumentArray type. This PR fixes it.

Examples

This won't produce compilation error:

import { model, Schema, InferSchemaType } from 'mongoose';
import { expectType } from 'tsd';

async function test() {
    const imageSchema = new Schema({
        url: { required: true, type: String }
    });

    const placeSchema = new Schema({
        images: { required: true, type: [imageSchema] },
    });

    type Place = InferSchemaType<typeof placeSchema>;

    const PlaceModel = model('Place', placeSchema);

    const place = await PlaceModel.findOne().lean().orFail().exec();
    expectAssignable<Place>(place);
}

@Jokero Jokero force-pushed the handleDocumentArrayInFlattenMaps branch 4 times, most recently from dffdd3d to 8f7e9cc Compare May 1, 2023 10:51
@Jokero Jokero force-pushed the handleDocumentArrayInFlattenMaps branch from 8f7e9cc to 4055c64 Compare May 1, 2023 10:55
Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

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

Thanks 👍

@vkarpov15 vkarpov15 merged commit aaacf46 into Automattic:master May 2, 2023
3 checks passed
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

Successfully merging this pull request may close these issues.

FlattenMaps doesn't handle DocumentArray properly
2 participants