Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

v2 class and method name cleanup #112

Merged
merged 29 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1d880c6
from_openapi_data_oapg -> from_openapi_data_
spacether Dec 30, 2022
3edd3d7
_verify_typed_dict_inputs_oapg -> _verify_typed_dict_inputs
spacether Dec 30, 2022
87a9072
_get_host_oapg -> _get_host
spacether Dec 30, 2022
6a37963
_{{operationId}}_oapg -> _{{operationId}}
spacether Dec 30, 2022
ef966d0
_raise_validation_error_message_oapg -> _raise_validation_error_message
spacether Dec 30, 2022
81e6830
is_true_oapg -> is_true_
spacether Dec 30, 2022
2628505
is_false_oapg -> is_false_
spacether Dec 30, 2022
2b614e2
is_none_oapg -> is_none_
spacether Dec 30, 2022
0be47b3
as_date_oapg -> as_date_
spacether Dec 30, 2022
481d27c
as_datetime_oapg -> as_datetime_
spacether Dec 30, 2022
5c1098c
as_decimal_oapg -> as_decimal_
spacether Dec 30, 2022
188d606
as_uuid_oapg -> as_uuid_
spacether Dec 30, 2022
1d11035
as_float_oapg -> as_float_
spacether Dec 30, 2022
cad009a
as_int_oapg -> as_int_
spacether Dec 30, 2022
34ee883
_get_class_oapg -> _get_class
spacether Dec 30, 2022
c27d143
get_item_oapg -> get_item_
spacether Dec 30, 2022
f3fe447
_validate_oapg -> _validate
spacether Dec 30, 2022
411e55d
_process_schema_classes_oapg -> _process_schema_classes
spacether Dec 30, 2022
ddacb81
_get_new_instance_without_conversion_oapg -> _get_new_instance_withou…
spacether Dec 30, 2022
85b1981
as_str_oapg -> as_str_
spacether Dec 30, 2022
1015cc8
_get_items_oapg -> _get_items
spacether Dec 30, 2022
4a34112
_get_properties_oapg -> _get_properties
spacether Dec 30, 2022
05322ef
Samples regenerated
spacether Dec 30, 2022
f36bedb
Readme update
spacether Dec 30, 2022
f5dca6b
MetaOapg -> Schema_
spacether Dec 30, 2022
a6618a9
_configuration -> configuration_
spacether Dec 30, 2022
a89409f
_arg -> arg_
spacether Dec 30, 2022
6d45597
_args -> args_
spacether Dec 30, 2022
392a5e1
JsonSchema -> OpenApiSchema, OpenapiComponent -> OpenApiComponent
spacether Dec 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Currently, the following languages/frameworks are supported:
- Openapi spec inline schemas supported at any depth
- If needed, validation of some json schema keywords can be deactivated via a configuration class
- Payload values are not coerced when validated, so a datetime value can pass other validations that describe the payload only as type string
- String transmission of numbers supported with type: string, format: number, value can be accessed as a Decimal with inst.as_decimal_oapg
- String transmission of numbers supported with type: string, format: number, value can be accessed as a Decimal with inst.as_decimal_
- Multiple content types supported for request and response bodies
- Endpoint response always also includes the urllib3.HTTPResponse
- Endpoint response deserialization can be skipped with the skip_deserialization argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.openapitools.codegen;

import org.openapitools.codegen.utils.ModelUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand All @@ -31,7 +29,7 @@
* A unique parameter is defined by a combination of a name and location.
* Parameters may be located in a path, query, header or cookie.
*/
public class CodegenRequestBody implements OpenapiComponent {
public class CodegenRequestBody implements OpenApiComponent {
protected String description, unescapedDescription;

protected CodegenKey name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.util.*;

public class CodegenResponse implements OpenapiComponent {
public class CodegenResponse implements OpenApiComponent {
private CodegenKey name;
private Map<String, CodegenHeader> headers;
public String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.util.*;

public class CodegenSchema implements JsonSchema, OpenapiComponent {
public class CodegenSchema implements OpenApiSchema, OpenApiComponent {
// testCases are for autogenerated tests of schemas
public HashMap<String, SchemaTestCase> testCases = new HashMap<>();
private String componentModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2211,7 +2211,7 @@ protected String toComponentModule(String componentName, String priorJsonPathSeg
return packageName + "." + priorJsonPathSegment + "." + componentName;
}

protected void setAddProps(Schema schema, JsonSchema property, String sourceJsonPath, String currentJsonPath) {
protected void setAddProps(Schema schema, OpenApiSchema property, String sourceJsonPath, String currentJsonPath) {
if (schema.getAdditionalProperties() == null) {
return;
}
Expand Down Expand Up @@ -3905,11 +3905,11 @@ protected boolean shouldAddImport(String type) {
/**
* Add variables (properties) to codegen model (list of properties, various flags, etc)
*
* @param m Must be an instance of JsonSchema, may be model or property...
* @param m Must be an instance of OpenApiSchema, may be model or property...
* @param properties a map of properties (schema)
* @param mandatory a set of required properties' name
*/
protected void addProperties(JsonSchema m, Map<String, Schema> properties, Set<String> mandatory, String sourceJsonPath, String currentJsonPath) {
protected void addProperties(OpenApiSchema m, Map<String, Schema> properties, Set<String> mandatory, String sourceJsonPath, String currentJsonPath) {
if (properties == null) {
return;
}
Expand Down Expand Up @@ -5060,7 +5060,7 @@ protected String toRefModule(String ref, String sourceJsonPath, String expectedC
return null;
}

private void setLocationInfo(String ref, OpenapiComponent instance, String sourceJsonPath, String expectedComponentType) {
private void setLocationInfo(String ref, OpenApiComponent instance, String sourceJsonPath, String expectedComponentType) {
if (ref != null) {
instance.setRef(ref);
String refModule = toRefModule(ref, sourceJsonPath, expectedComponentType);
Expand Down Expand Up @@ -5156,7 +5156,7 @@ public CodegenKey getKey(String key) {
return ck;
}

protected void addRequiredProperties(Schema schema, JsonSchema property, String sourceJsonPath, String currentJsonPath) {
protected void addRequiredProperties(Schema schema, OpenApiSchema property, String sourceJsonPath, String currentJsonPath) {
/*
this should be called after vars and additionalProperties are set
Features added by storing codegenProperty values:
Expand Down Expand Up @@ -5211,7 +5211,7 @@ protected void addRequiredProperties(Schema schema, JsonSchema property, String
}
}

protected void addVarsRequiredVarsAdditionalProps(Schema schema, JsonSchema property, String sourceJsonPath, String currentJsonPath) {
protected void addVarsRequiredVarsAdditionalProps(Schema schema, OpenApiSchema property, String sourceJsonPath, String currentJsonPath) {
setAddProps(schema, property, sourceJsonPath, currentJsonPath);
Set<String> mandatory = schema.getRequired() == null ? Collections.emptySet()
: new TreeSet<>(schema.getRequired());
Expand Down Expand Up @@ -5609,7 +5609,7 @@ public String getAdditionalPropertiesName() {
* Used to ensure that null or Schema is returned given an input Boolean/Schema/null
* This will be used in openapi 3.1.0 spec processing to ensure that Booleans become Schemas
* Because our generators only understand Schemas
* Note: use getIsBooleanSchemaTrue or getIsBooleanSchemaFalse on the JsonSchema
* Note: use getIsBooleanSchemaTrue or getIsBooleanSchemaFalse on the OpenApiSchema
* if you need to be able to detect if the original schema's value was true or false
*
* @param schema the input Boolean or Schema data to convert to a Schema
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
package org.openapitools.codegen;

import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.meta.FeatureSet;
import org.openapitools.codegen.meta.features.SchemaSupportFeature;
import org.openapitools.codegen.utils.ModelUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

public interface OpenapiComponent {
public interface OpenApiComponent {
// set only if the instance is at the json path of a component
String getComponentModule();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package org.openapitools.codegen;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.meta.FeatureSet;
import org.openapitools.codegen.meta.features.SchemaSupportFeature;
import org.openapitools.codegen.utils.ModelUtils;

public interface JsonSchema {
public interface OpenApiSchema {
// 3.1.0
CodegenSchema getContains();

Expand Down Expand Up @@ -221,7 +214,7 @@ public interface JsonSchema {
void setExternalDocumentation(ExternalDocumentation externalDocumentation);

/**
* Syncs all the schema's type properties into the JsonSchema instance
* Syncs all the schema's type properties into the OpenApiSchema instance
* for now this only supports types without format information
* TODO: in the future move the format handling in here too
* @param p the schema which contains the type info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import io.swagger.v3.oas.models.servers.Server;

import org.apache.commons.io.FileUtils;
import org.openapitools.codegen.JsonSchema;
import org.openapitools.codegen.OpenApiSchema;
import org.openapitools.codegen.api.TemplatePathLocator;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.ignore.CodegenIgnoreProcessor;
Expand Down Expand Up @@ -540,7 +540,7 @@ are defined in that schema (x.properties). We do this because validation should
they are not.
*/
@Override
protected void addVarsRequiredVarsAdditionalProps(Schema schema, JsonSchema property, String sourceJsonPath, String currentJsonPath){
protected void addVarsRequiredVarsAdditionalProps(Schema schema, OpenApiSchema property, String sourceJsonPath, String currentJsonPath){
setAddProps(schema, property, sourceJsonPath, currentJsonPath);
if (ModelUtils.isAnyType(schema) && supportsAdditionalPropertiesWithComposedSchema) {
// if anyType schema has properties then add them
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
import io.swagger.v3.parser.util.RemoteUrl;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CodegenSchema;
import org.openapitools.codegen.JsonSchema;
import org.openapitools.codegen.OpenApiSchema;
import org.openapitools.codegen.config.GlobalSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -1463,8 +1462,8 @@ public static boolean isAnyType(Schema schema) {
return (schema.get$ref() == null && schema.getType() == null);
}

public static void syncValidationProperties(Schema schema, JsonSchema target) {
// TODO move this method to JsonSchema
public static void syncValidationProperties(Schema schema, OpenApiSchema target) {
// TODO move this method to OpenApiSchema
if (schema != null && target != null) {
if (isNullType(schema) || schema.get$ref() != null || isBooleanSchema(schema)) {
return;
Expand Down Expand Up @@ -1508,24 +1507,24 @@ public static void syncValidationProperties(Schema schema, JsonSchema target) {
}
}

private static void setArrayValidations(Integer minItems, Integer maxItems, Boolean uniqueItems, JsonSchema target) {
private static void setArrayValidations(Integer minItems, Integer maxItems, Boolean uniqueItems, OpenApiSchema target) {
if (minItems != null) target.setMinItems(minItems);
if (maxItems != null) target.setMaxItems(maxItems);
if (uniqueItems != null) target.setUniqueItems(uniqueItems);
}

private static void setObjectValidations(Integer minProperties, Integer maxProperties, JsonSchema target) {
private static void setObjectValidations(Integer minProperties, Integer maxProperties, OpenApiSchema target) {
if (minProperties != null) target.setMinProperties(minProperties);
if (maxProperties != null) target.setMaxProperties(maxProperties);
}

private static void setStringValidations(Integer minLength, Integer maxLength, String pattern, JsonSchema target) {
private static void setStringValidations(Integer minLength, Integer maxLength, String pattern, OpenApiSchema target) {
if (minLength != null) target.setMinLength(minLength);
if (maxLength != null) target.setMaxLength(maxLength);
if (pattern != null) target.setPattern(pattern);
}

private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, Boolean exclusiveMinimum, Boolean exclusiveMaximum, JsonSchema target) {
private static void setNumericValidations(Schema schema, BigDecimal multipleOf, BigDecimal minimum, BigDecimal maximum, Boolean exclusiveMinimum, Boolean exclusiveMaximum, OpenApiSchema target) {
if (multipleOf != null) target.setMultipleOf(multipleOf);
if (minimum != null) {
if (isIntegerSchema(schema)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,25 @@ Python {{generatorLanguageVersion}}
- ingested None will subclass NoneClass
- ingested True will subclass BoolClass
- ingested False will subclass BoolClass
- So if you need to check is True/False/None, instead use instance.is_true_oapg()/.is_false_oapg()/.is_none_oapg()
- So if you need to check is True/False/None, instead use instance.is_true_()/.is_false_()/.is_none_()
5. All validated class instances are immutable except for ones based on io.File
- This is because if properties were changed after validation, that validation would no longer apply
- So no changing values or property values after a class has been instantiated
6. String + Number types with formats
- String type data is stored as a string and if you need to access types based on its format like date,
date-time, uuid, number etc then you will need to use accessor functions on the instance
- type string + format: See .as_date_oapg, .as_datetime_oapg, .as_decimal_oapg, .as_uuid_oapg
- type number + format: See .as_float_oapg, .as_int_oapg
- type string + format: See .as_date_, .as_datetime_, .as_decimal_, .as_uuid_
- type number + format: See .as_float_, .as_int_
- this was done because openapi/json-schema defines constraints. string data may be type string with no format
keyword in one schema, and include a format constraint in another schema
- So if you need to access a string format based type, use as_date_oapg/as_datetime_oapg/as_decimal_oapg/as_uuid_oapg
- So if you need to access a number format based type, use as_int_oapg/as_float_oapg
- So if you need to access a string format based type, use as_date_/as_datetime_/as_decimal_/as_uuid_
- So if you need to access a number format based type, use as_int_/as_float_
7. Property access on AnyType(type unset) or object(dict) schemas
- Only required keys with valid python names are properties like .someProp and have type hints
- All optional keys may not exist, so properties are not defined for them
- One can access optional values with dict_instance['optionalProp'] and KeyError will be raised if it does not exist
- Use get_item_oapg if you need a way to always get a value whether or not the key exists
- If the key does not exist, schemas.unset is returned from calling dict_instance.get_item_oapg('optionalProp')
- Use get_item_ if you need a way to always get a value whether or not the key exists
- If the key does not exist, schemas.unset is returned from calling dict_instance.get_item_('optionalProp')
- All required and optional keys have type hints for this method, and @typing.overload is used
- A type hint is also generated for additionalProperties accessed using this method
- So you will need to update you code to use some_instance['optionalProp'] to access optional property
Expand All @@ -69,19 +69,18 @@ Python {{generatorLanguageVersion}}
- Those apis will only load their needed models, which is less to load than all of the resources needed in a tag api
- So you will need to update your import paths to the api classes

### Why are Oapg and _oapg used in class and method names?
### Why are Leading and Trailing Underscores in class and method names?
Classes can have arbitrarily named properties set on them
Endpoints can have arbitrary operationId method names set
For those reasons, I use the prefix Oapg and _oapg to greatly reduce the likelihood of collisions
For those reasons, I use the prefix and suffix _ to greatly reduce the likelihood of collisions
on protected + public classes/methods.
oapg stands for OpenApi Python Generator.

### Object property spec case
This was done because when payloads are ingested, they can be validated against N number of schemas.
If the input signature used a different property name then that has mutated the payload.
So SchemaA and SchemaB must both see the camelCase spec named variable.
Also it is possible to send in two properties, named camelCase and camel_case in the same payload.
That use case should be support so spec case is used.
That use case should work, so spec case is used.

### Parameter spec case
Parameters can be included in different locations including:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,12 @@ class HeaderParameterWithoutName(ParameterBase, StyleSimpleSerializer):
"""
if cls.style:
extracted_data = cls._deserialize_simple(in_data, name, cls.explode, False)
return schema.from_openapi_data_oapg(extracted_data)
return schema.from_openapi_data_(extracted_data)
# cls.content will be length one
for content_type, schema in cls.content.items():
if cls._content_type_is_json(content_type):
cast_in_data = json.loads(in_data)
return schema.from_openapi_data_oapg(cast_in_data)
return schema.from_openapi_data_(cast_in_data)
raise NotImplementedError('Deserialization of {} has not yet been implemented'.format(content_type))


Expand Down Expand Up @@ -753,7 +753,7 @@ class ApiResponseWithoutDeserialization(ApiResponse):

class TypedDictInputVerifier:
@staticmethod
def _verify_typed_dict_inputs_oapg(cls: typing.Type[typing_extensions.TypedDict], data: typing.Dict[str, typing.Any]):
def _verify_typed_dict_inputs(cls: typing.Type[typing_extensions.TypedDict], data: typing.Dict[str, typing.Any]):
"""
Ensures that:
- required keys are present
Expand Down Expand Up @@ -890,7 +890,7 @@ class OpenApiResponse(JSONDetector, TypedDictInputVerifier, typing.Generic[T]):

deserialized_headers = schemas.unset
if cls.headers is not None:
cls._verify_typed_dict_inputs_oapg(cls.response_cls.headers, response.headers)
cls._verify_typed_dict_inputs(cls.response_cls.headers, response.headers)
deserialized_headers = {}
for header_name, header_param in self.headers.items():
header_value = response.getheader(header_name)
Expand Down Expand Up @@ -923,8 +923,8 @@ class OpenApiResponse(JSONDetector, TypedDictInputVerifier, typing.Generic[T]):
content_type = 'multipart/form-data'
else:
raise NotImplementedError('Deserialization of {} has not yet been implemented'.format(content_type))
deserialized_body = body_schema.from_openapi_data_oapg(
body_data, _configuration=configuration)
deserialized_body = body_schema.from_openapi_data_(
body_data, configuration_=configuration)
elif streamed:
response.release_conn()

Expand Down Expand Up @@ -1259,7 +1259,7 @@ class Api(TypedDictInputVerifier):
api_client = ApiClient()
self.api_client = api_client

def _get_host_oapg(
def _get_host(
self,
operation_id: str,
servers: typing.Tuple[typing.Dict[str, str], ...] = tuple(),
Expand Down
Loading