Skip to content

Commit f54b2aa

Browse files
committed
update samples to fix python tests
1 parent 036ddb8 commit f54b2aa

File tree

12 files changed

+399
-149
lines changed

12 files changed

+399
-149
lines changed

bin/configs/python-pydantic-v1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ additionalProperties:
1313
nameMappings:
1414
_type: underscore_type
1515
type_: type_with_underscore
16+
openapiNormalizer:
17+
SIMPLIFY_ONEOF_ANYOF_ENUM: false

bin/configs/python.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ nameMappings:
1313
modelNameMappings:
1414
# The OpenAPI spec ApiResponse conflicts with the internal ApiResponse
1515
ApiResponse: ModelApiResponse
16+
openapiNormalizer:
17+
SIMPLIFY_ONEOF_ANYOF_ENUM: false

samples/client/petstore/csharp/generichost/latest/ComposedEnum/api/openapi.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ servers:
88
components:
99
schemas:
1010
AreaCode:
11-
oneOf:
12-
- $ref: "#/components/schemas/StateTerritoryCode"
13-
- $ref: "#/components/schemas/MarineAreaCode"
11+
enum:
12+
- AL
13+
- AK
14+
- AM
15+
- AN
16+
type: string
1417
StateTerritoryCode:
1518
enum:
1619
- AL

samples/client/petstore/csharp/generichost/latest/ComposedEnum/src/Org.OpenAPITools/Client/ClientUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public static bool TryDeserialize<T>(ref Utf8JsonReader reader, JsonSerializerOp
111111
return boolean
112112
? "true"
113113
: "false";
114+
if (obj is AreaCode areaCode)
115+
return AreaCodeValueConverter.ToJsonValue(areaCode);
114116
if (obj is MarineAreaCode marineAreaCode)
115117
return MarineAreaCodeValueConverter.ToJsonValue(marineAreaCode);
116118
if (obj is StateTerritoryCode stateTerritoryCode)

samples/client/petstore/csharp/generichost/latest/ComposedEnum/src/Org.OpenAPITools/Client/HostConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public HostConfiguration(IServiceCollection services)
4444
_jsonOptions.Converters.Add(new DateOnlyJsonConverter());
4545
_jsonOptions.Converters.Add(new DateOnlyNullableJsonConverter());
4646
_jsonOptions.Converters.Add(new AreaCodeJsonConverter());
47+
_jsonOptions.Converters.Add(new AreaCodeNullableJsonConverter());
4748
_jsonOptions.Converters.Add(new MarineAreaCodeJsonConverter());
4849
_jsonOptions.Converters.Add(new MarineAreaCodeNullableJsonConverter());
4950
_jsonOptions.Converters.Add(new StateTerritoryCodeJsonConverter());

samples/client/petstore/csharp/generichost/latest/ComposedEnum/src/Org.OpenAPITools/Model/AreaCode.cs

Lines changed: 118 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -26,164 +26,178 @@
2626
namespace Org.OpenAPITools.Model
2727
{
2828
/// <summary>
29-
/// AreaCode
29+
/// Defines AreaCode
3030
/// </summary>
31-
public partial class AreaCode : IValidatableObject
31+
public enum AreaCode
3232
{
3333
/// <summary>
34-
/// Initializes a new instance of the <see cref="AreaCode" /> class.
34+
/// Enum AL for value: AL
3535
/// </summary>
36-
/// <param name="stateTerritoryCode"></param>
37-
internal AreaCode(StateTerritoryCode stateTerritoryCode)
38-
{
39-
StateTerritoryCode = stateTerritoryCode;
40-
OnCreated();
41-
}
36+
AL = 1,
4237

4338
/// <summary>
44-
/// Initializes a new instance of the <see cref="AreaCode" /> class.
39+
/// Enum AK for value: AK
4540
/// </summary>
46-
/// <param name="marineAreaCode"></param>
47-
internal AreaCode(MarineAreaCode marineAreaCode)
48-
{
49-
MarineAreaCode = marineAreaCode;
50-
OnCreated();
51-
}
41+
AK = 2,
5242

53-
partial void OnCreated();
43+
/// <summary>
44+
/// Enum AM for value: AM
45+
/// </summary>
46+
AM = 3,
5447

5548
/// <summary>
56-
/// Gets or Sets StateTerritoryCode
49+
/// Enum AN for value: AN
5750
/// </summary>
58-
public StateTerritoryCode? StateTerritoryCode { get; set; }
51+
AN = 4
52+
}
5953

54+
/// <summary>
55+
/// Converts <see cref="AreaCode"/> to and from the JSON value
56+
/// </summary>
57+
public static class AreaCodeValueConverter
58+
{
6059
/// <summary>
61-
/// Gets or Sets MarineAreaCode
60+
/// Parses a given value to <see cref="AreaCode"/>
6261
/// </summary>
63-
public MarineAreaCode? MarineAreaCode { get; set; }
62+
/// <param name="value"></param>
63+
/// <returns></returns>
64+
public static AreaCode FromString(string value)
65+
{
66+
if (value.Equals("AL"))
67+
return AreaCode.AL;
68+
69+
if (value.Equals("AK"))
70+
return AreaCode.AK;
71+
72+
if (value.Equals("AM"))
73+
return AreaCode.AM;
74+
75+
if (value.Equals("AN"))
76+
return AreaCode.AN;
77+
78+
throw new NotImplementedException($"Could not convert value to type AreaCode: '{value}'");
79+
}
6480

6581
/// <summary>
66-
/// Returns the string presentation of the object
82+
/// Parses a given value to <see cref="AreaCode"/>
6783
/// </summary>
68-
/// <returns>String presentation of the object</returns>
69-
public override string ToString()
84+
/// <param name="value"></param>
85+
/// <returns></returns>
86+
public static AreaCode? FromStringOrDefault(string value)
7087
{
71-
StringBuilder sb = new StringBuilder();
72-
sb.Append("class AreaCode {\n");
73-
sb.Append("}\n");
74-
return sb.ToString();
88+
if (value.Equals("AL"))
89+
return AreaCode.AL;
90+
91+
if (value.Equals("AK"))
92+
return AreaCode.AK;
93+
94+
if (value.Equals("AM"))
95+
return AreaCode.AM;
96+
97+
if (value.Equals("AN"))
98+
return AreaCode.AN;
99+
100+
return null;
75101
}
76102

77103
/// <summary>
78-
/// To validate all properties of the instance
104+
/// Converts the <see cref="AreaCode"/> to the json value
79105
/// </summary>
80-
/// <param name="validationContext">Validation context</param>
81-
/// <returns>Validation Result</returns>
82-
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
106+
/// <param name="value"></param>
107+
/// <returns></returns>
108+
/// <exception cref="NotImplementedException"></exception>
109+
public static string ToJsonValue(AreaCode value)
83110
{
84-
yield break;
111+
if (value == AreaCode.AL)
112+
return "AL";
113+
114+
if (value == AreaCode.AK)
115+
return "AK";
116+
117+
if (value == AreaCode.AM)
118+
return "AM";
119+
120+
if (value == AreaCode.AN)
121+
return "AN";
122+
123+
throw new NotImplementedException($"Value could not be handled: '{value}'");
85124
}
86125
}
87126

88127
/// <summary>
89-
/// A Json converter for type <see cref="AreaCode" />
128+
/// A Json converter for type <see cref="AreaCode"/>
90129
/// </summary>
130+
/// <exception cref="NotImplementedException"></exception>
91131
public class AreaCodeJsonConverter : JsonConverter<AreaCode>
92132
{
93133
/// <summary>
94-
/// Deserializes json to <see cref="AreaCode" />
134+
/// Returns a from the Json object
95135
/// </summary>
96-
/// <param name="utf8JsonReader"></param>
136+
/// <param name="reader"></param>
97137
/// <param name="typeToConvert"></param>
98-
/// <param name="jsonSerializerOptions"></param>
138+
/// <param name="options"></param>
99139
/// <returns></returns>
100-
/// <exception cref="JsonException"></exception>
101-
public override AreaCode Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert, JsonSerializerOptions jsonSerializerOptions)
140+
public override AreaCode Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
102141
{
103-
int currentDepth = utf8JsonReader.CurrentDepth;
142+
string? rawValue = reader.GetString();
104143

105-
if (utf8JsonReader.TokenType != JsonTokenType.StartObject && utf8JsonReader.TokenType != JsonTokenType.StartArray)
106-
throw new JsonException();
144+
AreaCode? result = rawValue == null
145+
? null
146+
: AreaCodeValueConverter.FromStringOrDefault(rawValue);
107147

108-
JsonTokenType startingTokenType = utf8JsonReader.TokenType;
109-
110-
StateTerritoryCode? stateTerritoryCode = default;
111-
MarineAreaCode? marineAreaCode = default;
112-
113-
Utf8JsonReader utf8JsonReaderOneOf = utf8JsonReader;
114-
while (utf8JsonReaderOneOf.Read())
115-
{
116-
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReaderOneOf.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReaderOneOf.CurrentDepth)
117-
break;
118-
119-
if (startingTokenType == JsonTokenType.StartArray && utf8JsonReaderOneOf.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReaderOneOf.CurrentDepth)
120-
break;
121-
122-
if (utf8JsonReaderOneOf.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReaderOneOf.CurrentDepth - 1)
123-
{
124-
Utf8JsonReader utf8JsonReaderStateTerritoryCode = utf8JsonReader;
125-
ClientUtils.TryDeserialize<StateTerritoryCode?>(ref utf8JsonReaderStateTerritoryCode, jsonSerializerOptions, out stateTerritoryCode);
126-
127-
Utf8JsonReader utf8JsonReaderMarineAreaCode = utf8JsonReader;
128-
ClientUtils.TryDeserialize<MarineAreaCode?>(ref utf8JsonReaderMarineAreaCode, jsonSerializerOptions, out marineAreaCode);
129-
}
130-
}
131-
132-
while (utf8JsonReader.Read())
133-
{
134-
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReader.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReader.CurrentDepth)
135-
break;
136-
137-
if (startingTokenType == JsonTokenType.StartArray && utf8JsonReader.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReader.CurrentDepth)
138-
break;
139-
140-
if (utf8JsonReader.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReader.CurrentDepth - 1)
141-
{
142-
string? localVarJsonPropertyName = utf8JsonReader.GetString();
143-
utf8JsonReader.Read();
144-
145-
switch (localVarJsonPropertyName)
146-
{
147-
default:
148-
break;
149-
}
150-
}
151-
}
152-
153-
if (stateTerritoryCode != null)
154-
return new AreaCode(stateTerritoryCode.Value);
155-
156-
if (marineAreaCode != null)
157-
return new AreaCode(marineAreaCode.Value);
148+
if (result != null)
149+
return result.Value;
158150

159151
throw new JsonException();
160152
}
161153

162154
/// <summary>
163-
/// Serializes a <see cref="AreaCode" />
155+
/// Writes the AreaCode to the json writer
164156
/// </summary>
165157
/// <param name="writer"></param>
166158
/// <param name="areaCode"></param>
167-
/// <param name="jsonSerializerOptions"></param>
168-
/// <exception cref="NotImplementedException"></exception>
169-
public override void Write(Utf8JsonWriter writer, AreaCode areaCode, JsonSerializerOptions jsonSerializerOptions)
159+
/// <param name="options"></param>
160+
public override void Write(Utf8JsonWriter writer, AreaCode areaCode, JsonSerializerOptions options)
161+
{
162+
writer.WriteStringValue(AreaCodeValueConverter.ToJsonValue(areaCode).ToString());
163+
}
164+
}
165+
166+
/// <summary>
167+
/// A Json converter for type <see cref="AreaCode"/>
168+
/// </summary>
169+
public class AreaCodeNullableJsonConverter : JsonConverter<AreaCode?>
170+
{
171+
/// <summary>
172+
/// Returns a AreaCode from the Json object
173+
/// </summary>
174+
/// <param name="reader"></param>
175+
/// <param name="typeToConvert"></param>
176+
/// <param name="options"></param>
177+
/// <returns></returns>
178+
public override AreaCode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
170179
{
171-
writer.WriteStartObject();
180+
string? rawValue = reader.GetString();
181+
182+
AreaCode? result = rawValue == null
183+
? null
184+
: AreaCodeValueConverter.FromStringOrDefault(rawValue);
185+
186+
if (result != null)
187+
return result.Value;
172188

173-
WriteProperties(writer, areaCode, jsonSerializerOptions);
174-
writer.WriteEndObject();
189+
throw new JsonException();
175190
}
176191

177192
/// <summary>
178-
/// Serializes the properties of <see cref="AreaCode" />
193+
/// Writes the AreaCode to the json writer
179194
/// </summary>
180195
/// <param name="writer"></param>
181196
/// <param name="areaCode"></param>
182-
/// <param name="jsonSerializerOptions"></param>
183-
/// <exception cref="NotImplementedException"></exception>
184-
public void WriteProperties(Utf8JsonWriter writer, AreaCode areaCode, JsonSerializerOptions jsonSerializerOptions)
197+
/// <param name="options"></param>
198+
public override void Write(Utf8JsonWriter writer, AreaCode? areaCode, JsonSerializerOptions options)
185199
{
186-
200+
writer.WriteStringValue(areaCode.HasValue ? AreaCodeValueConverter.ToJsonValue(areaCode.Value).ToString() : "null");
187201
}
188202
}
189203
}

samples/openapi3/client/petstore/python-pydantic-v1/docs/OneOfEnumString.md

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
oneOf enum strings
44

5-
## Enum
6-
7-
* `A` (value: `'a'`)
8-
9-
* `B` (value: `'b'`)
10-
11-
* `C` (value: `'c'`)
12-
13-
* `D` (value: `'d'`)
14-
5+
## Properties
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
9+
## Example
10+
11+
```python
12+
from petstore_api.models.one_of_enum_string import OneOfEnumString
13+
14+
# TODO update the JSON string below
15+
json = "{}"
16+
# create an instance of OneOfEnumString from a JSON string
17+
one_of_enum_string_instance = OneOfEnumString.from_json(json)
18+
# print the JSON string representation of the object
19+
print OneOfEnumString.to_json()
20+
21+
# convert the object into a dict
22+
one_of_enum_string_dict = one_of_enum_string_instance.to_dict()
23+
# create an instance of OneOfEnumString from a dict
24+
one_of_enum_string_from_dict = OneOfEnumString.from_dict(one_of_enum_string_dict)
25+
```
1526
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
1627

1728

0 commit comments

Comments
 (0)