Extending base schemas with Pydantic - fastapi

I'm building a FastAPI project, and in it, I'd like all my errors to be structured as such:
errors: {
#... whatever
}
I know I can create a pydantic schema as such:
class Error(BaseModel):
error: Dict
However, for documentation sake, I'd like to avoid having to create two schemas every time I want to set an output response:
class AuthError(BaseModel):
invalid_user = True
class Error(BaseModel):
error: AuthError
Is there a better way to do this?

Related

Kotlin: Json: conflicting annotation schemes for name transcription

I read data from Firebase database into a Kotlin/Android program. The key names in Firebase are different from those of the corresponding Kotlin variables. I test this code with flat JSON files (for good reasons) where I retain the same key names used in Firebase, so I need to transcribe them too.
Firebase wants to annotate variables with #PropertyName; but Gson, which I use to read flat files, wants #SerializedName (which Firebase doesn't understand, unfortunately.)
Through trial and error I found that this happens to work:
#SerializedName("seq")
var id: Int? = null
#PropertyName("seq")
get
#PropertyName("seq")
set
Both Firebase and Gson do their thing and my class gets its data. Am I hanging by a thin thread here? Is there a better way to do this?
Thank you!,
You can probably solve this by using Kotlin's #JvmField to suppress generation of getters and setters. This should allow you to place the #PropertyName annotation directly on the property. You can then implement a Gson FieldNamingStrategy which checks if a #PropertyName annotation is present on the field and in that case uses its value; otherwise it could return the field name. The FieldNamingStrategy has to be set on a GsonBuilder which you then use to create the Gson instance.

self-referential generic types in flowtype

I'm trying to build a better Flowtype definition for Koa library and am kinda stuck.
My idea was to use Generic types to be able to specify customized Context class to Koa, so we can actually typecheck additional fields (populated by middlewares) instead of treating them as any.
so, I have:
declare type Context {…}
declare class Application<T: Context<T>> extends events$EventEmitter {
context: T,
…
}
fine…
but Context has a back-reference to Application, which is a generic dependent on Context. How do I spell this in typelib?
This doesn't look right, as I actually want to use not original Context but the type which was actually used by user
declare type Context {
app: Application<Context>
}

Determine whether an instance is an instance of a data class

Given the following classes
abstract class SomeAbstractClass { abstract val name: String }
data class DataClass( override val name: String ) : SomeAbstractClass()
class NoDataClass( override val name: String ) : SomeAbstractClass()
For any instance of SomeAbstractClass, can I determine whether it is a data class without relying on type checking?
Some background: this seemed the best way of combining inheritance and data classes to me, as suggested in a different answer. Now, within the initializer block of SomeAbstractClass, I want to throw an exception in case the derived type is not a data class to ensure 'correct' (immutable) implementations of derived types.
Using reflection, the Kotlin class description (KClass) can be obtained using the ::class syntax on the instance you want to investigate (in your case, this::class in the initializer block of the abstract class). This gives you access to isData:
true if this class is a data class.
However, as Oliver points out, data classes can still contain var members, so you likely also want to check whether all member variables (and their member variables recursively) are defined as val to ensure immutability of all deriving classes.

Flex/AS3 : Automatically instantiate package classes in an array (plugin classes)

This is my first time here, but I already found some good answers here, so I'd like to thank everyone.
I'm developping a small Flex application and I want to instantiate every class from a package into an array, so I can parse it afterwards. To clarify, I'm trying to ease a plugin management system for my application, with the old canProcess/doProcess routine :
My plugins are all in ONE package, including an abstract plugin class. First, I create one instance of every classes in this package (that's where I need help) and put them in an array. Then, whenever I need a plugin for an item, I parse every plugin class in my array with the canProcess method (the item is the parameter). If one plugin says yes, then I send the item to the doProcess method and stop parsing the array.
I know I could implement by hand every class in my package, but I'd prefer not bothering to do it.
Has anyone an idea ?
Thx
AS3 reflection doesn't allow you to list all classes in a package. You will have to write the class names to an (xml) file at the server, load it and then use getDefinitionByName to get Class objects from those strings and then instantiate them.
Consider the sample xml file:
<root package="boris.ratak">
<className>Plugin1</className>
<className>Plugin2</className>
<className>Plugin3</className>
</root>
load it with URLLoader and parse it like:
import flash.utils.getDefinitionByName;
var pack:String = String(xml.#package) + ".";
for each(var cl:String in xml.className)
{
var name:String = pack + String(cl.text());
var Type:Class = getDefinitionByName(name) as Class;
pluginArray.push(new Type());
}

Flex Builder Import Web service errors in autogen code

I'm trying to import a web service with the Flex builder feature (Data > Import Web Service). The process proceeds as expected and the classes are generated based on the web service description. The code is generated with compile errors in it though.
There are 20 errors of 2 varieties. I've included a sample of the errors below.
It looks like an error in the auto gen. The auto gen code is trying to dispatch an object of ArrayCollection type. As far I know that isn't possible.
Does anyone have advice or has anyone had similar problems?
Error examples:
1067: Implicit coercion of a value of type net.responsys:ListTablesForCampaignResultEvent to an unrelated type flash.events:Event. Responsys/src/net/responsys ResponsysWS57.as
1119: Access of possibly undefined property headers through a reference with static type net.responsys:ListFolderContentsResultEvent. Responsys/src/net/responsys ResponsysWS57.as
If they are both from an "Event" class, it might be a scoping problem. Try qualifying all of the Event instances, e.g. look into the generated code and wherever you see a flash Event, add flash.events before it.
So a declaration of a flash event might look like:
var event:flash.events.Event
instead of
var event:Event

Resources