-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implicit python import error (#981)
- Loading branch information
1 parent
13eec21
commit 2fd0a13
Showing
10 changed files
with
233 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Python models | ||
|
||
A basic example of complete model generation. | ||
|
||
## How to run this example | ||
|
||
Run this example using: | ||
|
||
```sh | ||
npm i && npm run start | ||
``` | ||
|
||
If you are on Windows, use the `start:windows` script instead: | ||
|
||
```sh | ||
npm i && npm run start:windows | ||
``` |
113 changes: 113 additions & 0 deletions
113
examples/generate-python-complete-models/__snapshots__/index.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to render python models and should generate \`implicit\` or \`explicit\` imports for models implementing referenced types: nested-model 1`] = ` | ||
Array [ | ||
" | ||
class ObjProperty: | ||
def __init__(self, input): | ||
if hasattr(input, 'number'): | ||
self._number = input.number | ||
if hasattr(input, 'additionalProperties'): | ||
self._additionalProperties = input.additionalProperties | ||
@property | ||
def number(self): | ||
return self._number | ||
@number.setter | ||
def number(self, number): | ||
self._number = number | ||
@property | ||
def additionalProperties(self): | ||
return self._additionalProperties | ||
@additionalProperties.setter | ||
def additionalProperties(self, additionalProperties): | ||
self._additionalProperties = additionalProperties | ||
", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render python models and should generate \`implicit\` or \`explicit\` imports for models implementing referenced types: nested-model 2`] = ` | ||
Array [ | ||
" | ||
class ObjProperty: | ||
def __init__(self, input): | ||
if hasattr(input, 'number'): | ||
self._number = input.number | ||
if hasattr(input, 'additionalProperties'): | ||
self._additionalProperties = input.additionalProperties | ||
@property | ||
def number(self): | ||
return self._number | ||
@number.setter | ||
def number(self, number): | ||
self._number = number | ||
@property | ||
def additionalProperties(self): | ||
return self._additionalProperties | ||
@additionalProperties.setter | ||
def additionalProperties(self, additionalProperties): | ||
self._additionalProperties = additionalProperties | ||
", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render python models and should generate \`implicit\` or \`explicit\` imports for models implementing referenced types: root-model-explicit-import 1`] = ` | ||
Array [ | ||
"from .ObjProperty import ObjProperty | ||
class Root: | ||
def __init__(self, input): | ||
if hasattr(input, 'email'): | ||
self._email = input.email | ||
if hasattr(input, 'objProperty'): | ||
self._objProperty = input.objProperty | ||
@property | ||
def email(self): | ||
return self._email | ||
@email.setter | ||
def email(self, email): | ||
self._email = email | ||
@property | ||
def objProperty(self): | ||
return self._objProperty | ||
@objProperty.setter | ||
def objProperty(self, objProperty): | ||
self._objProperty = objProperty | ||
", | ||
] | ||
`; | ||
|
||
exports[`Should be able to render python models and should generate \`implicit\` or \`explicit\` imports for models implementing referenced types: root-model-implicit-import 1`] = ` | ||
Array [ | ||
"from ObjProperty import ObjProperty | ||
class Root: | ||
def __init__(self, input): | ||
if hasattr(input, 'email'): | ||
self._email = input.email | ||
if hasattr(input, 'objProperty'): | ||
self._objProperty = input.objProperty | ||
@property | ||
def email(self): | ||
return self._email | ||
@email.setter | ||
def email(self, email): | ||
self._email = email | ||
@property | ||
def objProperty(self): | ||
return self._objProperty | ||
@objProperty.setter | ||
def objProperty(self, objProperty): | ||
self._objProperty = objProperty | ||
", | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {generate} from './index'; | ||
|
||
const createLogMock = () => jest.spyOn(global.console, 'log').mockImplementation(() => { return; }); | ||
|
||
describe('Should be able to render python models', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should generate `implicit` or `explicit` imports for models implementing referenced types', async () => { | ||
const spy = createLogMock(); | ||
|
||
await generate(); | ||
|
||
expect(spy.mock.calls.length).toEqual(4); | ||
|
||
expect(spy.mock.calls[0]).toMatchSnapshot('root-model-implicit-import'); | ||
expect(spy.mock.calls[1]).toMatchSnapshot('nested-model'); | ||
|
||
expect(spy.mock.calls[2]).toMatchSnapshot('root-model-explicit-import'); | ||
expect(spy.mock.calls[3]).toMatchSnapshot('nested-model'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { PythonGenerator } from '../../src'; | ||
|
||
export const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
email: { | ||
type: 'string', | ||
format: 'email' | ||
}, | ||
objProperty: { | ||
type: 'object', | ||
properties: { | ||
number: { | ||
type: 'number' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
export async function generate(): Promise<void> { | ||
const generator = new PythonGenerator(); | ||
|
||
let models = await generator.generateCompleteModels(jsonSchemaDraft7, { | ||
importsStyle: 'implicit' | ||
}); | ||
|
||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
|
||
models = await generator.generateCompleteModels(jsonSchemaDraft7, { | ||
importsStyle: 'explicit' | ||
}); | ||
|
||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
generate(); | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/generate-python-complete-models/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "generate-python-complete-models" | ||
}, | ||
"scripts": { | ||
"install": "cd ../.. && npm i", | ||
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts", | ||
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts", | ||
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts", | ||
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters