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

Native functions #2309

Merged
merged 15 commits into from
Nov 10, 2021
Merged

Conversation

gabrielsimongianotti
Copy link
Contributor

Description

i make two alters of #2187 (comment), removed lodash/isPlainObject, lodash/isObject and replace per native function

How Has This Been Tested?

I used your tests

Types of changes

  • remove lodash/isPlainObject
  • remove lodash/isObject

Checklist:

  • isObject
  • sPlainObject

@@ -76,7 +75,14 @@ export function execute({
...extras,
});

if (request.body && (isPlainObject(request.body) || Array.isArray(request.body))) {
Copy link
Member

@char0n char0n Nov 2, 2021

Choose a reason for hiding this comment

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

Comparing this change with original, I'd say we'll stick with isPlainObject for the time being. Determining if something is POJO is a hard job, and having a well tested utility function that abstract the complexity just make sense.

I think we have two options with isPlainObject
1.) remove the isPlainObject from the list and keep using lodash version
2.) create our own utility function that does exactly the same thing as isPlainObject from lodash

The scope of #2187 (as I understood it) is to replace lodash functions with modern & native simple javascript constructs. IMHO this doesn't fall within that scope.

// cc @jimmywarting

Copy link
Contributor

Choose a reason for hiding this comment

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

What @char0n have mention about isPlainObject not being equivalent to just a simple typeof is true... need to check more things such as function, null etc

in some senarios it's fine to use just typeof x === object but we need to know for sure if it's okey to use it.

the point of my issue is not to use any lodash at all, even if you just include one simple function then lodash will at least load some internal core functions that are pretty big in itself and is deeply dependent on many other stuff to make it compatible with es3 or 5 (whatever it is).

Right now there is so few lodash fn that we use that i don't see any point in keeping it, so i suggest # 2
create our own utility function that does the same thing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

where can i create function isPlainObject? In which project folder?

Copy link
Member

Choose a reason for hiding this comment

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

@gabrielsimongianotti src/helpers.js contains already two helper functions at the top of the file. Le'ts put isPlainObject

Copy link
Member

@char0n char0n Nov 5, 2021

Choose a reason for hiding this comment

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

Another options is to use https://www.npmjs.com/package/is-plain-object

Contains the logic that we need and handles corner cases. Has no dependencies as well.

If there are no strong objections to this proposal let's consider this package as a replacement for lodash.isPlainObject

src/helpers.js Outdated
@@ -136,7 +134,7 @@ export function normalizeSwagger(parsedSpec) {
for (const pathName in paths) {
const path = paths[pathName];

if (!isObject(path)) {
if (typeof path !== 'object') {
Copy link
Member

Choose a reason for hiding this comment

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

isObject cannot be simply replaced with typeof path === 'object'; this is not functionally equivalent.

The source code of isObject is as follows:

    function isObject(value) {
      var type = typeof value;
      return value != null && (type == 'object' || type == 'function');
    }

Which means that anything that reports typeof object || function except null and undefined.

src/helpers.js Outdated
@@ -136,7 +134,7 @@ export function normalizeSwagger(parsedSpec) {
for (const pathName in paths) {
const path = paths[pathName];

if (!isObject(path)) {
if (typeof path !== 'object') {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (typeof path !== 'object') {
if (path == null || !['object', 'function'].includes(typeof path)) {

src/helpers.js Outdated
@@ -145,7 +143,7 @@ export function normalizeSwagger(parsedSpec) {
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const method in path) {
const operation = path[method];
if (!isObject(operation)) {
if (typeof path !== 'object') {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (typeof path !== 'object') {
if (path == null || !['object', 'function'].includes(typeof path)) {

Copy link
Member

@char0n char0n left a comment

Choose a reason for hiding this comment

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

Thanks for contributing! I've added couple of code review comments. Please have a look at them.

@char0n
Copy link
Member

char0n commented Nov 5, 2021

Made one change, but the PR is missing changes in package-lock.json file, after we've introduced new library.

@gabrielsimongianotti
Copy link
Contributor Author

Made one change, but the PR is missing changes in package-lock.json file, after we've introduced new library.

thank you to remember 😅

Copy link
Member

@char0n char0n left a comment

Choose a reason for hiding this comment

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

As we've introduced new library, we need the package-lock.json file change in this PR as well.

@gabrielsimongianotti
Copy link
Contributor Author

As we've introduced new library, we need the package-lock.json file change in this PR as well.

I maked merge in 'upstream/master' , is something missing? you can make approve?

@char0n
Copy link
Member

char0n commented Nov 9, 2021

As we've introduced new library, we need the package-lock.json file change in this PR as well.

I maked merge in 'upstream/master' , is something missing? you can make approve?

You need to run following command in repo fork:

 $ npm i --save is-plain-object

This will modify two files: package.json and package-lock.json. These files needs to be part of this PR. We have changes in package.json but you didn't submitted changes in package-lock.json.

More info about package-lock files here: https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json

@gabrielsimongianotti
Copy link
Contributor Author

As we've introduced new library, we need the package-lock.json file change in this PR as well.

I maked merge in 'upstream/master' , is something missing? you can make approve?

You need to run following command in repo fork:

 $ npm i --save is-plain-object

This will modify two files: package.json and package-lock.json. These files needs to be part of this PR. We have changes in package.json but you didn't submitted changed in package-lock.json.

More info about package-lock files here: https://docs.npmjs.com/cli/v7/configuring-npm/package-lock-json

sorry i don't speak english very well, now i understand what you were trying to say

@char0n
Copy link
Member

char0n commented Nov 10, 2021

I've made some final changes to the PR, regarding package-lock.json file. We're using lockfileVersion: 1, you've generated it for lockfileVersion: 2. No big deal, changed it back to v1.

@char0n
Copy link
Member

char0n commented Nov 10, 2021

Merging and thanks for contributing!

@char0n char0n merged commit 067229e into swagger-api:master Nov 10, 2021
@swagger-bot
Copy link
Contributor

🎉 This PR is included in version 3.18.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants