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

orval/core - generating factory method for schema interfaces #1334

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mironbalcerzak
Copy link
Contributor

@mironbalcerzak mironbalcerzak commented Apr 27, 2024

Status

WIP

Description

Fix #1331

This PR is providing simple implementation.
Opening for discussion.
Bugs? Possible - definitely "stackoverflow" in case of self referencing or cycle

Todos

  • Tests
  • Documentation
  • Changelog Entry (unreleased)

@@ -53,6 +53,7 @@ export const generateInterface = ({
} else {
model += `export type ${name} = ${scalar.value};\n`;
}
model += `export function create${name}(): ${name} ${scalar.factoryMethodValue}\n`;
Copy link
Collaborator

Choose a reason for hiding this comment

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

You will add this in the model file here. It's a bit weird no?

Copy link
Contributor Author

@mironbalcerzak mironbalcerzak Apr 30, 2024

Choose a reason for hiding this comment

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

Yeah... was thinking whether it should be in separate file or not. I've decided to leave it with model. Anyone who has imported a model, can either instantiate it through "new", or right away call a factory method - which increases the chances that random dev will learn about it and use it.

People are lazy and do not read the code - especially the one that's generated.

@soartec-lab
Copy link
Member

Hi, @mironbalcerzak
I'll review it. Let me know when you change the status to Ready.

@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch 4 times, most recently from 64df5ba to 76f3ec5 Compare May 4, 2024 14:22
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch from 76f3ec5 to 9e5e347 Compare May 11, 2024 19:23
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch from 9e5e347 to 32e6fab Compare May 22, 2024 05:12
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch 2 times, most recently from 0fb9a7d to 27dbc05 Compare June 5, 2024 19:51
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch from 27dbc05 to e68e30d Compare June 15, 2024 17:31
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch from e68e30d to 5e5ece4 Compare July 14, 2024 10:03
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch 2 times, most recently from e0fde50 to b77a14c Compare September 22, 2024 10:11
@mironbalcerzak
Copy link
Contributor Author

@soartec-lab - ive been using this branch in few projects of mine over last months - seems ok.
How can we progress so it ends in "master"?

@melloware
Copy link
Collaborator

It looks like the tests are failing?

@mironbalcerzak
Copy link
Contributor Author

@melloware, yeah, i am aware and that will be fixed shortly - i meant on a broader scale - should i implement/change something in PR?

@melloware
Copy link
Collaborator

i think once its fixed and passing we will re-review.

@soartec-lab soartec-lab self-assigned this Sep 23, 2024
@soartec-lab
Copy link
Member

@mironbalcerzak
Thank you for mada good enhance. I'll review this !

Copy link
Member

@soartec-lab soartec-lab left a comment

Choose a reason for hiding this comment

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

@mironbalcerzak
I made some comments 👍

@@ -47,7 +47,7 @@ export const generateImports = ({
} } from \'./${upath.join(path, camel(name))}\';`;
}

return `import ${!values ? 'type ' : ''}{ ${name}${
return `import { create${name}, ${name}${
Copy link
Member

Choose a reason for hiding this comment

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

Since create exists in the http status, I want to change the function name to the new prefix to avoid confusion.

Suggested change
return `import { create${name}, ${name}${
return `import { new${name}, ${name}${

@@ -53,6 +53,7 @@ export const generateInterface = ({
} else {
model += `export type ${name} = ${scalar.value};\n`;
}
model += `export function create${name}(): ${name} ${scalar.factoryMethodValue}\n`;
Copy link
Member

Choose a reason for hiding this comment

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

Factory methods are just noise for people who don't need them, so I'd like to be able to choose whether to generate them with a property.
For example, generateFactoryMethod: boolean.

And by making it a property, people who want to use it will be aware that it will be generated. Therefore, you will be able to notice it even if you separate the file like .factory.ts, right?

@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch 5 times, most recently from 2cc176f to c9e3bb3 Compare November 17, 2024 00:42
@mironbalcerzak mironbalcerzak force-pushed the feature/factory-method branch 4 times, most recently from c20568d to 91101bf Compare November 24, 2024 00:27
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.

Feature Request: create factory method for instantiating OpenAPI models
4 participants