@@ -5,6 +5,60 @@ import {
55import { PythonOptions } from '../PythonGenerator' ;
66import { ClassPresetType , PythonPreset } from '../PythonPreset' ;
77
8+ function formatPythonConstValue ( constValue : unknown ) : string {
9+ if ( typeof constValue === 'string' ) {
10+ return `'${ constValue } '` ;
11+ }
12+ if ( typeof constValue === 'boolean' ) {
13+ return constValue ? 'True' : 'False' ;
14+ }
15+ return String ( constValue ) ;
16+ }
17+
18+ function formatLiteralType ( constValue : unknown ) : string {
19+ return `Literal[${ formatPythonConstValue ( constValue ) } ]` ;
20+ }
21+
22+ function buildFieldArgs (
23+ property : ConstrainedObjectPropertyModel ,
24+ isOptional : boolean ,
25+ constOptions ?: { originalInput : unknown }
26+ ) : string [ ] {
27+ const decoratorArgs : string [ ] = [ ] ;
28+
29+ if ( property . property . originalInput [ 'description' ] ) {
30+ decoratorArgs . push (
31+ `description='''${ property . property . originalInput [ 'description' ] } '''`
32+ ) ;
33+ }
34+
35+ if ( constOptions ) {
36+ decoratorArgs . push (
37+ `default=${ formatPythonConstValue ( constOptions . originalInput ) } `
38+ ) ;
39+ decoratorArgs . push ( 'frozen=True' ) ;
40+ } else if ( isOptional ) {
41+ decoratorArgs . push ( 'default=None' ) ;
42+ }
43+
44+ const isUnwrappedDict =
45+ property . property instanceof ConstrainedDictionaryModel &&
46+ property . property . serializationType === 'unwrap' ;
47+
48+ if ( isUnwrappedDict ) {
49+ decoratorArgs . push ( 'exclude=True' ) ;
50+ }
51+
52+ if (
53+ property . propertyName !== property . unconstrainedPropertyName &&
54+ ! isUnwrappedDict
55+ ) {
56+ decoratorArgs . push ( `alias='''${ property . unconstrainedPropertyName } '''` ) ;
57+ }
58+
59+ return decoratorArgs ;
60+ }
61+
862const PYTHON_PYDANTIC_CLASS_PRESET : ClassPresetType < PythonOptions > = {
963 async self ( { renderer, model } ) {
1064 renderer . dependencyManager . addDependency (
@@ -22,61 +76,25 @@ const PYTHON_PYDANTIC_CLASS_PRESET: ClassPresetType<PythonOptions> = {
2276 ) ;
2377 } ,
2478 property ( { property, model, renderer } ) {
25- let type = property . property . type ;
2679 const propertyName = property . propertyName ;
27-
80+ const constOptions = property . property . options . const ;
2881 const isOptional =
2982 ! property . required || property . property . options . isNullable === true ;
83+
84+ let type = property . property . type ;
3085 if ( isOptional ) {
3186 type = `Optional[${ type } ]` ;
3287 }
33- if (
34- property . property . options . const &&
35- model . options . discriminator ?. discriminator ===
36- property . unconstrainedPropertyName
37- ) {
88+ if ( constOptions ) {
3889 renderer . dependencyManager . addDependency ( 'from typing import Literal' ) ;
39- type = `Literal[' ${ property . property . options . const . originalInput } ']` ;
90+ type = formatLiteralType ( constOptions . originalInput ) ;
4091 }
4192 type = renderer . renderPropertyType ( {
4293 modelType : model . type ,
4394 propertyType : type
4495 } ) ;
4596
46- const decoratorArgs : string [ ] = [ ] ;
47-
48- if ( property . property . originalInput [ 'description' ] ) {
49- decoratorArgs . push (
50- `description='''${ property . property . originalInput [ 'description' ] } '''`
51- ) ;
52- }
53- if ( isOptional ) {
54- decoratorArgs . push ( 'default=None' ) ;
55- }
56- if ( property . property . options . const ) {
57- let value = property . property . options . const . value ;
58- if (
59- model . options . discriminator ?. discriminator ===
60- property . unconstrainedPropertyName
61- ) {
62- value = property . property . options . const . originalInput ;
63- }
64- decoratorArgs . push ( `default='${ value } '` ) ;
65- decoratorArgs . push ( 'frozen=True' ) ;
66- }
67- if (
68- property . property instanceof ConstrainedDictionaryModel &&
69- property . property . serializationType === 'unwrap'
70- ) {
71- decoratorArgs . push ( 'exclude=True' ) ;
72- }
73- if (
74- property . propertyName !== property . unconstrainedPropertyName &&
75- ( ! ( property . property instanceof ConstrainedDictionaryModel ) ||
76- property . property . serializationType !== 'unwrap' )
77- ) {
78- decoratorArgs . push ( `alias='''${ property . unconstrainedPropertyName } '''` ) ;
79- }
97+ const decoratorArgs = buildFieldArgs ( property , isOptional , constOptions ) ;
8098
8199 return `${ propertyName } : ${ type } = Field(${ decoratorArgs . join ( ', ' ) } )` ;
82100 } ,
0 commit comments